Pulse width measurement with Arduino
Moderator: Benj
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
Noticed delay is off for some frequencies.
If change delay to 2 seconds will fix that.
If change delay to 2 seconds will fix that.
Martin
Re: Pulse width measurement with Arduino
Hello Martin, incredibly it works fantastic!
And with so little code
Thank you very much! My (probably first) problem is solved
Now I have to monitor the pulse change, I don't care about exact ms, more about a fast or slow change of the high level and whether positive or negative to the origin.
Surely again something very simple...
Greetings from Bavaria Chris
And with so little code

Thank you very much! My (probably first) problem is solved

Now I have to monitor the pulse change, I don't care about exact ms, more about a fast or slow change of the high level and whether positive or negative to the origin.
Surely again something very simple...

Greetings from Bavaria Chris
Re: Pulse width measurement with Arduino
Big praise to the forum here, you both have always helped me quickly, I hope I can return the favor sometime
Re: Pulse width measurement with Arduino
Here comes the next question:
After the display output, I added another decision (see example) to output two PWM signals.
If the value is smaller, one LED lights up as it should be 100% at the PWM outputs. If the value is larger, one LED lights up 100% and the other one flashes slightly, why?
After the display output, I added another decision (see example) to output two PWM signals.
If the value is smaller, one LED lights up as it should be 100% at the PWM outputs. If the value is larger, one LED lights up 100% and the other one flashes slightly, why?
- Attachments
-
- Pulse width Measurement v2.fcfx
- (17.64 KiB) Downloaded 232 times
Re: Pulse width measurement with Arduino
since i changed to timer2 the hardware is also behaving strange. the read in values are starting to change themselves
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
Glad I have helped also.
Looks like there is a bug with Timer2.
The frequency is double to what it should be.
The measure pulse correctly, you will need to have prescaler on 1:256
Bit confused with your pwm settings.
Since period overflow is 255, that means if duty is 255 the output will be near enough high all the time.
If duty is 0 the output will be low all the time.
So PWM is acting like a digital output.
If you want duty of 50% then set duty to 128:
Duty = PeriodRegister/(100/RequiredDuty)
Duty = 255/(100/50) ~128
Looks like there is a bug with Timer2.
The frequency is double to what it should be.
The measure pulse correctly, you will need to have prescaler on 1:256
Bit confused with your pwm settings.
Since period overflow is 255, that means if duty is 255 the output will be near enough high all the time.
If duty is 0 the output will be low all the time.
So PWM is acting like a digital output.
If you want duty of 50% then set duty to 128:
Duty = PeriodRegister/(100/RequiredDuty)
Duty = 255/(100/50) ~128
Martin
Re: Pulse width measurement with Arduino
i thought i have to use timer2 because pwm also uses timer0? can i leave everything on timer0?
The PWM outputs were set to 255/0 to switch the lamps on and off for test purposes only. I noticed that one LED always flashes although it should be off.
The PWM outputs were set to 255/0 to switch the lamps on and off for test purposes only. I noticed that one LED always flashes although it should be off.
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
What you did was correct.
As PWM only uses timer 0, you correctly changed timer to timer2
You are also correct here as well.
The issue you have is because the milliseconds resets to 0 for a new test.
Just place the decision branch containing PWM control, just above the 2 sec delay.
I.e withing the Pause branch that displays values on the display.
Martin
Re: Pulse width measurement with Arduino
the short blinking of the pwm2 led remains, although it should be off
Re: Pulse width measurement with Arduino
I'll have to add a delay to block out the short milliseconds reset...
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
I have just added the 2 LEDs.
Working OK for me.
Freq:11.00Hz
LCD Display showing: 45ms
LED lit: CH2 (D5)
Freq:70.00Hz
LCD Display showing: 7ms
LED lit: CH1 (D6)
For me either one or other LED is solid.
As the tests could be different, giving unpredictable results
Can you let me know what frequencies are causing the issues, along with what the display shows.
The next state would be setting a max expected pulse length, so display could just show 0 for example.
Working OK for me.
Freq:11.00Hz
LCD Display showing: 45ms
LED lit: CH2 (D5)
Freq:70.00Hz
LCD Display showing: 7ms
LED lit: CH1 (D6)
For me either one or other LED is solid.
As the tests could be different, giving unpredictable results
Can you let me know what frequencies are causing the issues, along with what the display shows.
The next state would be setting a max expected pulse length, so display could just show 0 for example.
Martin
Re: Pulse width measurement with Arduino
but not so easy. i think the whole period of time is affected until the value exceeds my threshold value again. is there any way to stabilise the millisecond value in order to hide this counting up phase?
Re: Pulse width measurement with Arduino
really no led blinking on you? weird.
I'm running the PWM at 6KHz, maybe that's the difference. I want to dim the LED using the PWM signal...
I'm running the PWM at 6KHz, maybe that's the difference. I want to dim the LED using the PWM signal...
Re: Pulse width measurement with Arduino
now without a code change suddenly also the second one starts blinking when the other one is on
Maybe a poltergeist?
Maybe a poltergeist?

-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
If you post Flowchart, I will run it on my hardware.
Let me know what the frequencies going in D8 are?
Let me know what the frequencies going in D8 are?
Martin
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
Input floating?
Do you have a weak pull-up or pull-down on D8?
Martin
Re: Pulse width measurement with Arduino
here's the latest code. i'm using a servo tester as a signal source. the signal looks good on the oscilloscope
- Attachments
-
- Pulse width Measurement v2 (1).fcfx
- (17.81 KiB) Downloaded 176 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
but i already had, but it didn't work either. when i go to 2s it runs quite stable. but much too slow for my application. at one second there are mismeasurements (sometimes it counts down to 1ms instead of the smallest value of the servotester of 7ms in my display.
if you go from the smallest value (7ms) to the largest (17ms) the display remains at 2-3ms. only if you slowly adjust up and down the 17ms come back as maximum
if you go from the smallest value (7ms) to the largest (17ms) the display remains at 2-3ms. only if you slowly adjust up and down the 17ms come back as maximum
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
OK,
OK, made changes so updates instantly.
- Attachments
-
- Pulse width Measurement v3.fcfx
- (17.83 KiB) Downloaded 149 times
Martin
Re: Pulse width measurement with Arduino
v3 does not work with my hardware
now the spook is gone. saved milliseconds after counting into the variable result
now the spook is gone. saved milliseconds after counting into the variable result
- Attachments
-
- Pulse width Measurement v4.fcfx
- (17.76 KiB) Downloaded 187 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Pulse width measurement with Arduino
Not sure why does not work for you but works well for me?
Anyway so long as your flowchart works for you, that the main thing.
Martin
Re: Pulse width measurement with Arduino
I gave up understanding these things a long time ago... 
it still works, i'm just a bit optimistic, but only with your help, thanks!

it still works, i'm just a bit optimistic, but only with your help, thanks!