Pulse width measurement with Arduino

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

medelec35
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

Post by medelec35 »

Noticed delay is off for some frequencies.
If change delay to 2 seconds will fix that.
Martin

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

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

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

Big praise to the forum here, you both have always helped me quickly, I hope I can return the favor sometime

mnf
Valued Contributor
Valued Contributor
Posts: 1208
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 440 times

Re: Pulse width measurement with Arduino

Post by mnf »

Glad to help..

Let us know how your project goes.

Martin

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

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?
Attachments
Pulse width Measurement v2.fcfx
(17.64 KiB) Downloaded 232 times

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

since i changed to timer2 the hardware is also behaving strange. the read in values are starting to change themselves

medelec35
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

Post by medelec35 »

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
Martin

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

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.

medelec35
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

Post by medelec35 »

ChrisT66 wrote:
Wed Jul 22, 2020 12:23 pm
i thought i have to use timer2 because pwm also uses timer0?
What you did was correct.
As PWM only uses timer 0, you correctly changed timer to timer2
ChrisT66 wrote:
Wed Jul 22, 2020 12:23 pm
noticed that one LED always flashes although it should be off.
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

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

the short blinking of the pwm2 led remains, although it should be off

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

I'll have to add a delay to block out the short milliseconds reset...

medelec35
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

Post by medelec35 »

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.
Martin

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

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?

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

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...

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

now without a code change suddenly also the second one starts blinking when the other one is on

Maybe a poltergeist? :shock:

medelec35
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

Post by medelec35 »

If you post Flowchart, I will run it on my hardware.
Let me know what the frequencies going in D8 are?
Martin

medelec35
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

Post by medelec35 »

ChrisT66 wrote:
Wed Jul 22, 2020 2:15 pm
now without a code change suddenly also the second one starts blinking when the other one is on
Input floating?
Do you have a weak pull-up or pull-down on D8?
Martin

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

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

medelec35
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

Post by medelec35 »

You have not made the suggested modifications of
medelec35 wrote:
Wed Jul 22, 2020 12:49 pm
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.
:
Suggested Modifications..png
Suggested Modifications..png (98.87 KiB) Viewed 7435 times
Martin

medelec35
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

Post by medelec35 »

ChrisT66 wrote:
Wed Jul 22, 2020 2:11 pm
I'm running the PWM at 6KHz, maybe that's the difference. I want to dim the LED using the PWM signal...
On flowchart you posted, PWM is set to 62.5kHz.
Martin

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

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

medelec35
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

Post by medelec35 »

OK,
ChrisT66 wrote:
Wed Jul 22, 2020 2:46 pm
when i go to 2s it runs quite stable. but much too slow for my application.
OK, made changes so updates instantly.
Attachments
Pulse width Measurement v3.fcfx
(17.83 KiB) Downloaded 149 times
Martin

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

v3 does not work with my hardware


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

medelec35
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

Post by medelec35 »

ChrisT66 wrote:
Wed Jul 22, 2020 3:00 pm
v3 does not work with my hardware
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

ChrisT66
Posts: 67
Joined: Mon Jul 13, 2020 3:00 pm

Re: Pulse width measurement with Arduino

Post by ChrisT66 »

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!

Post Reply