Arduino hardware-based pulse generator.

Welcome all. In my project I need to generate periodic pulse with long delay. Duration of pulse is 12.5us and delay between them 1ms. Is there possibility to program PWM to generate waveform that is not continues? In this case one short burst every 1ms. I do not want to use code and delay as been told is not reliable practice.

You mentioned you didn't want to use code and delay to create the pulse, but that's how the arduino works.

besides code and delays, the only way i know of would be using a 555 timer and manually making the PWM, and you can put a couple quality pots to fine tune it.

Yes, you should be able to do it with Timer1. But, you have to use code to configure the timer. After that the hardware will do the rest. See Section 16 of the ATMega328P datasheet.

This tutorial describes a method to generate one-cycle-resolution pulses in hardware. The 1ms PRR should be doable with timer1.

Thank you all for replies.

@ wildcat99s What I meant it was to use hardware rather than loops to generate the pulse. I thought about it as I used 555 for many projects however accuracy of such an arrangement is questionable. For instance, using RC for time keeping. Of course, if there is a way to use 555 with high accuracy, I am all up for it as have more experience with analogue circuits than coding :slight_smile:

@ dougp and gfvalvo sorry should tell before I use timer one for time measurement it is Nick Gammon's arduino frequency counter code that was customized. It might sound very basic, but would I be able to use timer 0? I would imagine that both are same and work independently therefore shouldn’t clash?

I’d stay away from Timer 0 as it’s used by Arduino core functions such as millis(). You can use Timer 2. But, it’s only 8 bits (just like Timer 0). So, assuming your 1 ms repetition rate is a hard requirement, you’ll be stuck with 4 us granularity (pre-scaler = 64).

Gfvalvo thank you for your advice. Ill stay away from timer 0 and use timer 2 instead. Unfortunately settling time of my system is up to 800us therefore generating pulse prior to that would create error in further measurement. Could you explain what do you mean by granularity?

In order to have a period of ~1ms by counting on an 8-bit timer, you’ll have to use pre-scaler=64 resulting in a clock rate of 250 KHz. That’s a clock period of 4us. So, your pulse can’t be 12.5us wide. It can be 12us or 16us.

gfvalvo:
So, your pulse can’t be 12.5us wide. It can be 12us or 16us.

Doesn’t the technique described in the link of post #3 get around this limitation? The way I understand it, the granularity could be as low as 62.5ns - ie., one clock cycle - *assuming * CLKI/O equals 16MHz.

Thank you both the penny dropped

dougp:
Doesn’t the technique described in the link of post #3 get around this limitation? The way I understand it, the granularity could be as low as 62.5ns - ie., one clock cycle - *assuming *CLKI/O equals 16MHz.

That code achieves a one-shot with 62.5ns granularity (pre-scaler=1). But the repetition rate is controlled by the code. OP requested not controlled by code. The only way to achieve ~1ms rate in hardware with Timer0/2 is to use pre-scaler=64. That gets us back to 4us granularity.

gfvalvo:
The only way to achieve ~1ms rate in hardware with Timer0/2 is to use pre-scaler=64. That gets us back to 4us granularity.

Bear with me a bit longer? Parts of this timer/counter business aren't completely clear yet. If the pulse is generated by TC2 that leaves 16-bit TC1 still available to generate the 1ms rate. Could TC1 somehow be set up to start TC2's pulse on a recurring basis, thinking ISR?

A preset of 16000 on TC1 would exactly equal 1ms at a 16MHz clock rate and prescaler of 1. Wouldn't adjusting TC1's preset to account for getting in and out of the ISR etc. allow sub-4us granularity at the desired rate?

No need to use both timers. If you used Timer 1, it could do it all (12.5 us pulse with 62.5ns resolution and 1ms period). But, per Reply #4, OP is using Timer 1 for another purpose.

gfvalvo:
But, per Reply #4, OP is using Timer 1 for another purpose.

Ah yes, forgot that. Thanks.