1

So I'm programming a TSL1401R-LF line scan camera module that reads in a a 1 X 128 array of pixels. I've run into a problem with the accuracy of the output. Basically the camera becomes less accurate as the delay times decrease. For debugging I hooked it up to an oscilloscope and monitored the SI and CLK pulses sent to the clock as well as the analog signals coming from it.

That's why I noticed the the duration of the CLK signal appeared to be over 300 microseconds long. This is strange because my code sets the duration to 170 microseconds. Since this is my first electrical project I'm not sure what to make of this so I'm hoping someone can take a look at my code and a screenshot of the oscilloscope and let me know if what I'm seeing is natural or not.

Here is the code:

int delayTime = 170;

void timming()
{

  //The timing for the impluses was found through direct experimentation.
  //(Meaing that I played around with different delayTimes until the code worked)

  digitalWriteFast(SI, HIGH);
  delayMicroseconds(delayTime/2);
  digitalWriteFast(CLK, HIGH);
  delayMicroseconds(delayTime/2);
  digitalWriteFast(SI, LOW);
  delayMicroseconds(delayTime/2);
  digitalWriteFast(CLK, LOW);
  delayMicroseconds(delayTime);

  for(int i = 0; i < 129; i++)
  {
    digitalWriteFast(CLK, HIGH);
    delayMicroseconds(delayTime);
    digitalWriteFast(CLK, LOW);
    delayMicroseconds(delayTime);
  }

}

Here is the screenshot

enter image description here

The yellow Line is the SI pulse, the light blue is the CLK pulse and the purple is the analog output.

Transistor
  • 168,990
  • 12
  • 186
  • 385
sgmm
  • 163
  • 7
  • This may be helpful, especially if you have interrupts running somewhere: [arduino : delaymicroseconds()](http://electronics.stackexchange.com/q/84776/25328). I'm not an arduino user, but you would probably be better off using a hardware timer triggering an interrupt. – Tut Jun 28 '16 at 19:23
  • The period of the clock pulse seems to be different between pulses 1-2 and 2-3. If delaymicroseconds() is a software timer, then its accuracy is suspect depending if anything interrupts it. As @Tut mentions, a hardware timer will provide a very repeatable clock. The AVR has several timer options for different waveforms, although you'll have to match your desired timer with your wiring. – Smith Jun 28 '16 at 19:34
  • @Smith Perhaps the center of the display marks the start of execution. Then it's consistent. SI goes high, then CLK high, then SI low, then CLK low, after which CLK is toggled. – Kaz Jun 28 '16 at 19:42
  • Sounds like the calibration in `delayMicroseconds` is off. Is this dynamically calibrated? If so, are you supposed to call something to get it calibrated? – Kaz Jun 28 '16 at 19:45
  • 1
    as written your code will always be slower, you are doing something that takes time, then waiting some amount of time then doing something that takes time. the things in between timing are not completely free. Likewise the timing routing quite likely has overhead so it is a little longer either due to the code, the function call or the fractions of a time period on either end of the measurement. this is not how to do accurate timing. you want to use a timer and eliminate the cost of the code or average it out. – old_timer Jun 29 '16 at 00:00
  • take a time stamp, do the thing you want to do, ADD (or SUBTRACT) TO THAT TIMESTAMP you took the amount of time you want, and/or use it as a reference, do not take a new timestamp after the thing you wanted to do. when the timer gets to the new timestamp, repeat. – old_timer Jun 29 '16 at 00:01
  • what you are doing is the clock says 8:01, then you do something that takes 1 minute. Then you look at the clock 8:02 and you wait 5 minutes to 8:07, then you do something that takes one minute, 8:08 wait 5, you keep accumulating error. Now in addition to that error you may have some other error which is what you might be looking for with this ticket. – old_timer Jun 29 '16 at 00:03
  • There are some good Arduino libraries that use interrupts to scheduled precisely timed events, code takes time to execute and once the Arduino code is compiled down to assembly each line could easily take tens of assembly instructions. Check out the TaskScheduler library. – Sam Jun 29 '16 at 01:10

1 Answers1

2

The main problem

It looks like the chip is running at a slower clock speed than it should be. If delayMicroseconds assumes a 16MHz clock and the chip is actually using it's 8MHz internal oscillator, for example, the pulses would double in length. You could check that:

  • The crystal oscillator is running and at the correct frequency
  • The atmega's fuse bits are set to use the external oscillator
  • There is no prescaler set

Also

The Arduino delayMicroseconds function is not a good way of making something happen every 85uS, and digitalWrite is not helping your case either. If you go digging into the definition of the two functions (which are provided by the Arduino project, not by Atmel, who make the actual microprocessor) you'll see that they are actually remarkably complicated. Now, the compiler will simplify them quite a lot, but even then you'll be left with a lot of operations, and those operations will lengthen the pulse.

You could rewrite the code to speed things up, but there is a better way. The AVR chip used in the Arduino has several timers on it. These run in parallel with the cpu which runs your code, and they can toggle pins or run short pieces of code at very precise intervals. The downside is that although the timers are a core feature of the AVR chips, they are rarely used in the Arduino world. So you'll have to go diving into some less user-friendly documentation to figure out how to use them.

Jack B
  • 12,167
  • 1
  • 27
  • 46
  • 1
    It also depends on the particular model of Arduino: I have a Chinese Nano clone that can output (using DigitalWrite) a square wave of 5us + the us specified in DigitalWrite like a Swiss clock. The added 5 us is roughy the time required to have the delay and write instruction enacted. On the other hand, a 10x more expensive original Micro is not capable of doing that, with timings all over the place. Perhaps it's because it has USB integrated within the microcontroller and it has to serve that too. Perhaps the OP should specify what Arduino is using. – Sredni Vashtar Jun 28 '16 at 21:40
  • I agree with this answer. The timing being off by almost exactly a factor of two suggests a clock configuration problem. – Adam Haun Jun 29 '16 at 03:27