pulse frequency measurement

Trying to measure a single pulse, which is bursted out every 50-300microseconds. The frequency of the pulse is 33kHz or period of 33 microseconds (sometimes the pulse might be 25kHz or as high as 40kHz which means the period is 25 microseconds).

We can change the pulse to a square wave and reduce it down to 4V on and zero volts off, but the measurement is still not always correct, it’s about 1 out of every 3 readings correct on the square wave and 1 out of 10 on the sin wave.

timeoutwait=100; // time to wait timeout to read a valid pulse
pauseTime=300; // time to wait until next pulse will be fired. lagtime between pulses

duration = pulseIn(pin, HIGH, timeoutwait);

I’ve played with adjusting the puseTime and timeoutwait but to no available in terms of getting the correct value every time.
I’m bench marketing vs an oscilloscope and we’ve set the signal to 25kHz and 400 microsecond pause, but I’m still getting a one out of three readings correct or even close.

Is there a better way than pulseIn?
Should I have the square wave be higher than 4 volts for the peak on the square wave?


Hi there

Am I right, the original pulses you are trying to measure are each one cycle of a sine wave, 40Vp-p about 0V? These pulses arrive at varying intervals. You are trying to measure the period of each pulse.

Can you show a schematic of the hardware, how the signal is being fed to the Arduino? Both for the sine wave case and square wave case.

Could you overlay on your existing image to show how the 0V to 4V pulse relates to the one cycle of sine wave?

Lastly, please post the full code you are using. This statement "delayMicroseconds(pauseTime);" could well be causing problems. It stops [u]all[/u] processing for pauseTime microseconds. Instead, you probably should loop and call pulseIn as quickly as you can. But pulseIn is for measuring digital pulses, hence important to understand how you are conditioning the original sine wave before connecting it to the Arduino.



BTW: It’s an UNO.

“You are trying to measure the period of each pulse.”
Yes, just the pulse and each additional one.

The signal from the system is black box and I’ve sent to the engineer for the schematics of what’s coming into the arduino’s breadboard. I’ll get a picture shot of the oscilloscope readout too. What I remember is that the amplitude is divided down to 0-4 volts using either an Op-Amp or simple R1/R2 circuit. I’ll update when the engineer returns, he’s out for the US memorial day weekend starting likely right about 5 minutes ago.

After reading the signal, we are trying to output a 0-5v reading back to the system for the frequency. It’s a POC just to see if this is a viable option. After we prove the viability, the engineer is going to embed the solution into the circuit.

I was trying to whip something up quick, it looked pretty simple in docs, figured I could show up, do some Arduino magic and bam have the freq reading going out to the Analog out in about a quick hour. It ended up being like 2-3 hours and still couldn’t get the Arduino to read the signal (either square or sine).


generic.ino (3.07 KB)

Thanks for the information.

the amplitude is divided down to 0-4 volts using either an Op-Amp or simple R1/R2 circuit

It will be good to see what schematics the engineer can provide. Just dividing the sine wave amplitude will give a signal that still goes above 0V and below 0V. The Arduino will try to interpret the positive half cycle as a digital signal but the result of pulseIn will not be the pulse duration you want (which is the duration of the full positive and negative going cycle, from your drawing). And if the sine wave were offset upwards to sit between 0V and 4V, pulseIn will again try to decide when the signal is at a [u]digital[/u] HIGH. This will be less than half (I think) the duration of the sine wave pulse you want.

we are trying to output a 0-5v reading back to the system for the frequency.

Do you mean you want to output an analog DC signal that varies between 0V and 5V in proportion to the [u]duration[/u] of each input pulse? Or is it in proportion to the frequency of the train of pulses?

The Arduino analogWrite() generates a PWM output, which is a 0V to 5V square wave whose duty cycle can be varied. You could filter this to give a true analog output, or you can add external DAC chips to the Arduino.



Hello again

Reading back over all the posts, can I double check something.

If we look at your diagram of the pulses, are you trying to measure the 33 us period of one pulse, or the 66 - 500 us interval between pairs of pulses (and the corresponding frequency of the pulse train)?

If it is the second of these, and assuming you can reliably detect the start of each pulse, one approach would be to count the number of pulses over a fixed period of time (say 100 ms) and hence calculate the frequency. You would then repeat the counting window and repeatedly calculate the frequency.

EDIT: These libraries look to be relevant. The website also has circuits for conditioning input signals which may help with the "sine wave to square wave" aspect.

http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/ http://interface.khm.de/index.php/lab/experiments/arduino-frequency-counter-library/