Jitter in TTL generated by Aurdino UNO

Hi,

I am currently building a device which requires manipulating several TTL pulses, which in turn controls other devices.

I test the ideas, I initially tried to generate a TTL sequence with Aurdino Uno. I wrote a very simple code which I am copying below.

const int ledPin = 7;

void setup() {

pinMode(ledPin, OUTPUT);
}

void loop()
{
digitalWrite(ledPin, HIGH);
delayMicroseconds(500);
digitalWrite(ledPin, LOW);
delayMicroseconds(500);
}

However, when I see the signal on an oscilloscope, I see that the TTL is jittering, which means the rising and/or falling edges of two respective TTL pulses are moving with respect to each other, by about,t 8 -10 microseconds. This will effect the response of my device.

My question is following- is this the limit of Uno board? I read that it has a temporal accuracy of 8 microseconds. Is this what is causing the jitter? It has a 16MHz clock. So in principle, it should be accurate to better than a microsecond. Is there some way to improve this by changing code or accessing the onboard clock signal? Is there someway to access it via software? Or the solution is to buy Due which have a much higher onboard clock?

Thanks for reading

My question is following- is this the limit of Uno board?

No.

I read that it has a temporal accuracy of 8 microseconds.

"Temporal accuracy"? What is that?

Is this what is causing the jitter?

No.

Is there some way to improve this ... ?

By "this" I assume you mean "reduce/eliminate the jitter" instead of all that nonsense about "temporal accuracy". In which case the answer is "yes".

You will be configuring timer 1 or timer 2 to toggle an Output Compare pin when the Output Compare register reaches a certain value. The mode of interest is Clear Timer on Compare (CTC).

10/500 is 2%, how accurate do you need to be?

Creating waveforms by software will always have some jitter, particularly as you get closer to the CPU clock speed, and if there is other processing to do. Jitter can be reduced by careful coding, but that tends to reduce the value of a software solution.

If you want to produce continuous repeating waveforms, the best way is to use the hardware peripherals, i.e. timer or pwm. However, if you want completely arbitrary waveforms, you would need to consider coding in assembler, and possibly dedicating a CPU to waveform generation, or using external hardware.

You want a 1 kHz output? Try this:

const byte OUTPUT_PIN = 3;  // Timer 2 "B" output: OC2B
const byte n = 249;   // 1 kHz (count to 250 with prescaler of 64)
void setup() 
 {
  pinMode (OUTPUT_PIN, OUTPUT);
  TCCR2A = bit (WGM20) | bit (WGM21) | bit (COM2B1); // fast PWM, clear OC2A on compare
  TCCR2B = bit (WGM22) | bit (CS22);         // fast PWM, prescaler of 64
  OCR2A =  n;                                // from table  
  OCR2B = ((n + 1) / 2) - 1;                 // 50% duty cycle
  }  // end of setup
void loop() { }

Rock-steady output on pin 3. This uses a hardware timer, and is not affected by interrupts or other processing.

bobcousins:
10/500 is 2%, how accurate do you need to be?

Creating waveforms by software will always have some jitter, particularly as you get closer to the CPU clock speed, and if there is other processing to do. Jitter can be reduced by careful coding, but that tends to reduce the value of a software solution.

If you want to produce continuous repeating waveforms, the best way is to use the hardware peripherals, i.e. timer or pwm. However, if you want completely arbitrary waveforms, you would need to consider coding in assembler, and possibly dedicating a CPU to waveform generation, or using external hardware.

2% is actually not very god enough for photonics research. Also the final goal is to move down to hundreds of KHz region of frequency and hence I am wondering if Uno is good enough for that.

CPU clock on Uno as far as I understand is 16 MHz so at 1KHz, I am quite far away from the CPU clock frequency. But I think the timer is required for stabilizing the waveform, atleast that is what has to be done on labview based systems.

I do not care about the value of software solution since this will be a driving part of a device. However, if you can provide some example of what you mean by "careful coding" it will be immensely helpful.

thanks

Hi Nick,

This seems like an extremely nice idea. However, in the final form of the implementation, I will have multiple TTL pulses synchronized by a master clock. I am not sure how to use this particular solution in such a setup. But thanks, you program is lighting up some light bulbs :slight_smile:

You can always vary the frequency and duty cycle at runtime. You have 6 hardware timers to play with on the Atmega328.

Can you turn off interrupts? 8 to 10 us sounds like the system timer interrupt that happens every 1024us, so you'll see it happen almost every other loop if your interval is ~1ms. Of course, various other things will break if interrupts are disabled (millis() and serial communications, particularly.)

Hi,

Thanks guys for the response. it does help me to build TTL pulses upto 1MHz without any jitter.

However, this is not the big picture. In the final situation, I need to create a sequence of digital states at repetition rates of atleast a few hundred KHz. These states will be fed into a 12 bit DAC to generate sequence of analog states. To reduce complexity, I will be going for a parallel input, that is, without using the shiftin and shiftout functions.

So my program will have the structure like

{
write(pin,state);
write(pin,state);
write(pin,state);
write(pin,state);

wait

write(pin,state);
write(pin,state);
write(pin,state);
write(pin,state);
.
.
.
}

This structure works perfectly upto 1-5KHz, but then it starts showing distortion beyond that because of the jitter. Any ideas how I can use Timer for such a structure??

I come from a physics background and I am not formally trained in electronics so it is a bit hard to follow the jargon :frowning:

However, this is not the big picture.

I thought it might not be. :slight_smile:

I need to create a sequence of digital states at repetition rates of atleast a few hundred KHz.

Can you describe or illustrate these states? To give you an idea of what is possible I created VGA output streams as described in Arduino Uno output to VGA monitor.

Jitter was certainly an issue at one point, I removed it by stopping Timer 0 interrupts, and also sleeping the processor until a known point (ie. a timer firing). This meant that the sequence came out in a predictable way.

Now this may or may not suit your application, so the more you describe it the more helpful answers can be.

Instead of writing individual pins, you can write bytes to ports.
Other processors, like the '1284P, work better because they have 4 complete 8-bit ports you can use.

Hi,

I have two separate goals in mind tied to this basic problem.

The first goal is to create a predefined sequence of voltages between 0 and 5. To achieve this I generate sequences like 0011010101 and write each individual bit to each digital output and then send it to a r-2r ladder. This will lead to the desired sequence. I need this sequence to keep repeating. Currently I dont have problems with repeatation rates of about 500-1KHz , which is good enough for the present prototype but to reach ultimate goal of the project, I need to be able to do it atleast to rates of few hundred KHz.

The second goal is to develop what is called a Multi Channel Scalar. What is basically does is counts how many TTL pulses come in a given time bin, which can range from few ns to few seconds, and gives the counts over several such bins. I am not looking for the ns second area however, but something which ranges from tens of microseconds to some seconds. To do this I need a clock signal, a TTL counter and a trigger mechanism without jitter.

That is why I was asking if there is a generic architecture for reducing the jitter. For example the first program which I posted, I can put whatever I want between the two microsecond delay commands and hence it allows for a very generic architecture. Or if I have to go for a board with higher onboard clock to improve such generic architecture.

I hope things are a bit more clear now. Please let me know if you have more questions. :slight_smile:

CrossRoads:
Instead of writing individual pins, you can write bytes to ports.
Other processors, like the '1284P, work better because they have 4 complete 8-bit ports you can use.

Thanks for the idea. I have to read about the kind of the processors that you are talking about. Thanks.

I generate sequences like 0011010101 and write each individual bit to each digital output and then send it to a r-2r ladder … at repetition rates of at least a few hundred KHz

You are probably not going to be able to do this on an Arduino using “Standard arduino programming techniques.” The maximum pin toggle rate for a single pin using digitalWrite() is about 100kHz (200KHz write rate.) http://forum.arduino.cc/index.php?topic=4324.0
So if you start calling digitalWrite() on 8 separate pins, you’re going to be down in the 10kHz range. Worse, many of the Arduino functions (digitalRead(), analogWrite(), Serial.read(), the clock interrupt, etc) have this same order of ~5us execution time, so even if you were to do the relatively simple replacements of digitalWrite() (with one of the digitalWriteFast() implementations, for example), you’d still have problems figuring out when to do anything else.
Now, as Nick mentions, you can go much faster - the chip can generate video. But this takes some very careful coding, and is moderately dependent on having time (the video blanking intervals) to set up data. And writing that sort of code is probably a couple of orders of magnitude more difficult than using Arduino-style code. :frowning:
(and I hope you meant “playing” ~200k values/s, rather than playing an N-value waveform sequence at 200kHz!)

The on-chip timers will do most of your second goal for you in hardware. You can set them up to increment on an external pin change, and read them periodically. But again, this would require bypassing the “Aduino” code.

westfw:
So if you start calling digitalWrite() on 8 separate pins, you're going to be down in the 10kHz range. Worse, many of the Arduino functions (digitalRead(), analogWrite(), Serial.read(), the clock interrupt, etc) have this same order of ~5us execution time, so even if you were to do the relatively simple replacements of digitalWrite() (with one of the digitalWriteFast() implementations, for example), you'd still have problems figuring out when to do anything else.
(and I hope you meant "playing" ~200k values/s, rather than playing an N-value waveform sequence at 200kHz!)

I was planning to use two separate boards for the two jobs I mentioned above. One board dedicated to the Waveform generation and one board for the Multi Channel Scalar application. So the time constraints are not that high regarding scheduling the two jobs.

However, is the digitalRead and digitalWrite time contraints related to the onboard clock or to the process by which the software interacts with the microcontroller? In that case I can always use the Due board which has 5 times higher clock speed. I am not sure if this answered in the link that you sent because apparently the link is broken :frowning:

I was talking about N-value waveform at 200kHz. But I guess from your response that might be too much ask from the Uno. I might have to go for hardcore FPGA modules!!! :frowning:

This seems like a very useful idea. I will try and see if I am able to implement this.

One of the ARM chips (Due, Teensy, Tiva Launchpad) with timer-driven DMA to D2A or gpio can probably do it for relatively small values of N. Maybe even an xmega. (What value of N are you looking for?)

You can probably find all sorts of things that will do ~44ksps (audio DAC rate.)

And there are chips like the AD9102 that will do 180M samples/s of arbitrary waveform, so you probably don't need to go all the way to full FPGA (a $25 chip, BTW.)

(The forum SW apparently won't format the link correctly. copy and past the text, and it should work. (But no, it doesn't talk about due.))

westfw:
One of the ARM chips (Due, Teensy, Tiva Launchpad) with timer-driven DMA to D2A or gpio can probably do it for relatively small values of N. Maybe even an xmega. (What value of N are you looking for?)

You can probably find all sorts of things that will do ~44ksps (audio DAC rate.)

And there are chips like the AD9102 that will do 180M samples/s of arbitrary waveform, so you probably don't need to go all the way to full FPGA (a $25 chip, BTW.)

(The forum SW apparently won't format the link correctly. copy and past the text, and it should work. (But no, it doesn't talk about due.))

http://forum.arduino.cc/index.php?topic=4324.0

Thanks for the tips. I will look into these things. Thanks again to both of you nick and west!! :slight_smile:

I was talking about N-value waveform at 200kHz. But I guess from your response that might be too much ask from the Uno. I might have to go for hardcore FPGA modules!!!

It should be possible by writing directly to the I/O ports

That would let you set the value of 8 pins at the same time and 1 MHz+ update rate is possible
BUT the CPU probably won't be able to do much else while doing that

I got rid of jitter on a 6KHz signal using:

SREG &= 127;

SREG is a reserved word for the System Register.
This sets bit 7 of the System Register to zero (0), which turns off global interrupts.

Of course this only works if you don’t need interrupts.