Go Down

Topic: Glitch free pulse generator (Read 3327 times) previous topic - next topic


Hello.    I am a rookie to this forum, and I need some advice.   I would like to use the arduino uno to create continuous 5ms wide pulses with a period of 10-1000ms, and be able to continuously vary the period without creating digital glitches in the output.  I'm thinking it would be best to use one of the hardware timers, since I will need to also have an interrupt service running every 1000ms (approximately) to sample an input waveform for the purpose of controlling the pulse train period mentioned above.

Any advice would be appreciated.



Jul 06, 2020, 04:57 pm Last Edit: Jul 06, 2020, 05:00 pm by jremington
Not enough information.   

What are the minimum and maximum allowed increments in the "continuously variable" period?

What are the minimum and maximum allowed increments/values of  the pulse width?

How would you define a "glitch"?


What I meant is that the inactive time between 5ms pulses is to be adjusted in order to change the frequency. The inactive time would be 5ms (for a 100 Hz pulse stream) to 1000ms (for a ~1Hz pulse stream).   I don't want the re-programming of the 'inactive' time to cause a tiny positive pulse, during the inactive state, or a tiny negative pulse during the active state.

Updates to the inactive state duration (hence the output frequency) would not be more often than 10ms.

The application is not super critical.   I'm basically generating an output pulse stream at a rate that is ~20 times faster than an input pulse stream , by measuring the input period and then setting the output frequency appropriately.  The input pulse stream is in the .05 to 5Hz range. 


Jul 06, 2020, 07:03 pm Last Edit: Jul 06, 2020, 07:07 pm by jremington
Sorry, your response doesn't make much sense, and it doesn't answer the question about increments posted in reply #1.

If the "inactive time" can be divided up into 1 millisecond intervals, a simple loop using delay() and digitalWrite() is all you need. If you need finer increments in the inactive time, use delayMicroseconds() instead.

There is no possibility of a negative pulse being emitted by an Arduino.


You have to restrict the rate of variation of period for this to be meaningful - you can't say "10ms", let the pulse
begin and then change your mind to "1000ms" and expect the current pulse to move into the future.

Would it be OK to only sample the input every pulse?  Then you can schedule the entire next pulse ignoring
the input value until its over.  This could be done using one of the timer interrupts to program the period at
the end of each cycle.

The various modes of operation for each timer have explicit rules for when the hardware registers update
from the values you program in software - ie they strive to avoid glitching behaviour for you, but you'd
need to check the particular mode you choose is doing the right thing for your use-case.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


I like your idea Mark. Running an interrupt service routine every ms would allow the pulse-to-pulse spacing to be adjusted, after each pulse finished, based on the last input measurement.   The input would be measured by sampling every 1ms.   

Is there a way to see how long an interrupt service takes to run?


I'm quite sure a timer library could meet your requirements directly.


The PWM hardware timers would be perfect for this

Go Up