Pulse width measurement with Arduino
Moderator: Benj
Pulse width measurement with Arduino
Hello, is there a relatively simple way to measure the length of an external pulse at an input with Flowcode 8?
An example code would be great! Thanks a lot, greetings Chris
An example code would be great! Thanks a lot, greetings Chris
-
- 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
Welcome to the forums.
Approximately how long are the pulses?
There is some code here: viewtopic.php?f=6&t=21641&hilit=j1850&p=98331#p98331 - that measured short pulses very accurately - or you might start a timer on an interrupt (start of pulse) and then stop timer when pulse ends..
Martin
Approximately how long are the pulses?
There is some code here: viewtopic.php?f=6&t=21641&hilit=j1850&p=98331#p98331 - that measured short pulses very accurately - or you might start a timer on an interrupt (start of pulse) and then stop timer when pulse ends..
Martin
Re: Pulse width measurement with Arduino
Hi, Martin,
thank you for the quick answer! The pulse length is about 20ms.
Let me see if I can handle the example
Too bad there is no function to evaluate pulses in Flowcode. Greeting Christian
thank you for the quick answer! The pulse length is about 20ms.
Let me see if I can handle the example

Too bad there is no function to evaluate pulses in Flowcode. Greeting Christian
-
- 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
Hi Christian,
20ms (ages!) - you can use the timer component (the code mentioned above measured microsecond pulses).. I made a simple example to wait for pin d2 to go high (start timing) - d2-> LOW stop timer then output pulse time in microseconds to UART - this assumes that there is time to output between pulses! (ie assuming timing a HIGH pulse)
The interrupt could be used with sleeping the MCU - but could busy wait on the pin going HIGH instead (and use any pin as required)
Note this should work well for a 'digital' signal - but not so for something with 'bounce' (like a switch).
Martin
20ms (ages!) - you can use the timer component (the code mentioned above measured microsecond pulses).. I made a simple example to wait for pin d2 to go high (start timing) - d2-> LOW stop timer then output pulse time in microseconds to UART - this assumes that there is time to output between pulses! (ie assuming timing a HIGH pulse)
The interrupt could be used with sleeping the MCU - but could busy wait on the pin going HIGH instead (and use any pin as required)
Note this should work well for a 'digital' signal - but not so for something with 'bounce' (like a switch).
Martin
-
- 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
.. and to test - I made a small pulse generator running on a second Arduino (in my case a Nano) with an output pin - I used D5 - connected to d2 on the receiver (above). Note that I also connected 5v and Gnd (they must share a common ground here) - so I only need to power one Arduino..
Sender outputs times in ms (between 1 and 30ms) and receiver shows rcvd time in microseconds (this is for interest to see the level of accuracy here) - so for example 4ms gives 4047us (or 4ms:-) )
Martin
Sender outputs times in ms (between 1 and 30ms) and receiver shows rcvd time in microseconds (this is for interest to see the level of accuracy here) - so for example 4ms gives 4047us (or 4ms:-) )
Martin
Re: Pulse width measurement with Arduino
Hi Martin, thanks for the great support! Unfortunately I have no timer and UART. I only have Flowcode with Arduino, Basic I/O, Sensors and Mechatronics 
Many greetings Christian

Many greetings Christian
-
- 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
Okay,
UART is worth getting - makes debug much eeasier.
Timing a pulse is still possible - use a timer interrupt - reset a counter on pulse start - timer increments counter (constantly) - read counter on pulse end. (This is how the J1850 example works - but it uses a very high res timer).
Or just count loops whilst pulse is high.
Might need a little maths to calculate time from counter....
If you need an example I can do one this evening....
Martin
UART is worth getting - makes debug much eeasier.
Timing a pulse is still possible - use a timer interrupt - reset a counter on pulse start - timer increments counter (constantly) - read counter on pulse end. (This is how the J1850 example works - but it uses a very high res timer).
Or just count loops whilst pulse is high.
Might need a little maths to calculate time from counter....
If you need an example I can do one this evening....
Martin
Re: Pulse width measurement with Arduino
Then maybe I'll have to buy Comms and Data, but not for cheap...
Here are some more details about my plan: I want to monitor the pulse on an input pin and depending on the change (if it gets longer or shorter, if it gets faster or slower shorter) I want to control a LED (brightness, blinking etc.).
I try my luck with the timer counter
Thanks a lot!
Here are some more details about my plan: I want to monitor the pulse on an input pin and depending on the change (if it gets longer or shorter, if it gets faster or slower shorter) I want to control a LED (brightness, blinking etc.).
I try my luck with the timer counter

-
- 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
A quick play - and a pulse timer that doesn't use UART component, or Timer...
I wrote a very simple DebugOut component to write to the UART. You'll need to install this....
Save DebugOut.fcpx to a directory (not the FC component directory) - then in FC View - Global Options - Locations - add this directory to the 'look for components in' box. clear component cache. (Might need to restart FC too)
Then there should be a new component DebugOut (you can use search - the magnifier to find it)
I've modified both program to use it. I use putty (https://www.chiark.greenend.org.uk/~sgt ... atest.html) to talk to the Arduino... Alsonow outputs a pulse of 1..100ms
Martin
I wrote a very simple DebugOut component to write to the UART. You'll need to install this....
Save DebugOut.fcpx to a directory (not the FC component directory) - then in FC View - Global Options - Locations - add this directory to the 'look for components in' box. clear component cache. (Might need to restart FC too)
Then there should be a new component DebugOut (you can use search - the magnifier to find it)
I've modified both program to use it. I use putty (https://www.chiark.greenend.org.uk/~sgt ... atest.html) to talk to the Arduino... Alsonow outputs a pulse of 1..100ms
Martin
Re: Pulse width measurement with Arduino
Hello Martin, thanks for your effort! Unfortunately I'm too much of an amateur... do i need a debugout? i just want to control a LED via a digital or analog output depending on the signal length. I don't have to output the data anywhere else.
Is it not possible to use the "simulation external input" component in Flowcode for an input pulse simulation?
Sorry for my banal questions, Christian
Is it not possible to use the "simulation external input" component in Flowcode for an input pulse simulation?
Sorry for my banal questions, Christian
-
- 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
No problem.
DebugOut just gives you a way to communicate with your pc via the usb without purchasing the comms pack - installing components is useful though - so if you can get it to work - it will help when you want to test on hardware..
Flowcode's simulation is very powerful - but I rarely use it, preferring to test things on hardware (I'm new to electronics - so I work under the illusion that if something doesn't work it's because I have the wires in the wrong place (programming - are the bits in the wrong place?)
)
The above examples just show how to time the pulse - and give you a way to test on hardware - use the time received as required.
I'll have a play with the external input - but I'd never noticed it
- to see if I can get it to work under simulation..
Martin
DebugOut just gives you a way to communicate with your pc via the usb without purchasing the comms pack - installing components is useful though - so if you can get it to work - it will help when you want to test on hardware..
Flowcode's simulation is very powerful - but I rarely use it, preferring to test things on hardware (I'm new to electronics - so I work under the illusion that if something doesn't work it's because I have the wires in the wrong place (programming - are the bits in the wrong place?)

The above examples just show how to time the pulse - and give you a way to test on hardware - use the time received as required.
I'll have a play with the external input - but I'd never noticed it

Martin
Re: Pulse width measurement with Arduino
I once put the .time value on a display. With the simulated input. It already counts something, but if you set mark delay to 2ms and space delay to 18ms the same value comes out as with 4/16 ms. But it should count the high level, right?
Re: Pulse width measurement with Arduino
The error becomes worse the higher the frequency is.
Is it the simulator now?
Is there a simple way to average and stabilize the value?
Is it the simulator now?
Is there a simple way to average and stabilize the value?
-
- 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
I'm not sure that is possible to rely on the timings in the simulation? You could record the last 'n' counts (use an array) and take an average - however note that on hardware the time should be (more) accurate (at least with ms pulses)
I'm sure I've seen that is possible to run two simulations - in two instances of FC, either on the same machine or (if you are lucky enough to have two PCs) on the same network - and have them communicate. Then run pulse_generator and pt2 (pulse timer). Running it on hardware may be easier?
Martin
I'm sure I've seen that is possible to run two simulations - in two instances of FC, either on the same machine or (if you are lucky enough to have two PCs) on the same network - and have them communicate. Then run pulse_generator and pt2 (pulse timer). Running it on hardware may be easier?
Martin
-
- 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
Anyone know if it possible to simulate a pin 'toggle' between two FC instances using vNet component in simulations.
I briefly toyed with using the CAL_SPI component and setting the SS pin as D2 - but this didn't seem to trigger an interrupt (int0) on MASTER_ENABLE_CS
from an different FC instance. Managed to get vNet working - transmitting colours to alter an RGB LED.
Is there a correct/neater way to do this?
Martin
I briefly toyed with using the CAL_SPI component and setting the SS pin as D2 - but this didn't seem to trigger an interrupt (int0) on MASTER_ENABLE_CS
from an different FC instance. Managed to get vNet working - transmitting colours to alter an RGB LED.
Is there a correct/neater way to do this?
Martin
Re: Pulse width measurement with Arduino
Hi Martin, I'm a little frustrated at the moment because I can't even manage to output a text on a 16x2 display... I have a standard 16x2 from ebay, do I need a special one if I choose the 16x2 generic from Flowcode or would it have to work with everyone? Greeting Christian
-
- 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
Welcome to the (murky) fascinatinating world of electronics...
Which component you use depends on several factors - which driver chip / connection method etc. Can you post more details about the display you have.
Of course that's what makes it (frustrating) fun
Martin
Which component you use depends on several factors - which driver chip / connection method etc. Can you post more details about the display you have.
Of course that's what makes it (frustrating) fun

Martin
Re: Pulse width measurement with Arduino
I have to check at home which display I have exactly in use. I will contact you when I am stuck again 
How can I write the variables into an array within Flowcode?
Is there a manual for Flowcode somewhere (except the example on the homepage)?
Christian

How can I write the variables into an array within Flowcode?
Is there a manual for Flowcode somewhere (except the example on the homepage)?
Christian
-
- 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
Use square brackets with an index.
For example if you have x[10] defined then you can access the elements as x[0] to x[9] in a calculation block - for example
.val = .x[0]
The index can, of course, be another variable - often a loop index giving code like:
.sum = .sum + .x[.i]
Note that the '.' denotes a local variable - I recommend using them wherever possible - makes for much more readable code..
Martin
For example if you have x[10] defined then you can access the elements as x[0] to x[9] in a calculation block - for example
.val = .x[0]
The index can, of course, be another variable - often a loop index giving code like:
.sum = .sum + .x[.i]
Note that the '.' denotes a local variable - I recommend using them wherever possible - makes for much more readable code..
Martin
-
- 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
Manual - see https://www.matrixtsl.com/wiki/index.ph ... =Main_Page
There are also many examples on this forum - though finding something relevant isn't always easy. Matrix also have videos on YouTube (and one of the users here has just started a series of videos which are very good) for example https://www.youtube.com/watch?v=S72qZu0AG9Q
- but it could do with a reference guide - I always favoured something ringbound....
There are also many examples on this forum - though finding something relevant isn't always easy. Matrix also have videos on YouTube (and one of the users here has just started a series of videos which are very good) for example https://www.youtube.com/watch?v=S72qZu0AG9Q
- but it could do with a reference guide - I always favoured something ringbound....
Re: Pulse width measurement with Arduino
Thank you! I already know the help page and some videos. Unfortunately these are often very simple examples and not what you are really looking for...
I would have thought that many more people use Flowcode, but when you see how few are in the forum during the day, I don't think that this tool is used very often. What a pity.
I would have thought that many more people use Flowcode, but when you see how few are in the forum during the day, I don't think that this tool is used very often. What a pity.
-
- 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
I'd agree - it is a very powerful environment - but sometimes it is (very) frustrating trying to work out how to use it.
For a set up largely aimed at beginners/education - good documentation is really a must. Unfortunately, it is a modern trend - documents only online, Flowcode is by no means the worst I've encountered. Intended to fuel an industry of how-to guides?
It's worth persevering through the initial learning phase though - FC is fun to use - the Matrix team are extremely responsive and helpful, and there are some experienced users on here who will go out of there way to help too - often with some very technical info - if you get stuck..
More users, and especially more user generated content would help the cause.
Martin
For a set up largely aimed at beginners/education - good documentation is really a must. Unfortunately, it is a modern trend - documents only online, Flowcode is by no means the worst I've encountered. Intended to fuel an industry of how-to guides?
It's worth persevering through the initial learning phase though - FC is fun to use - the Matrix team are extremely responsive and helpful, and there are some experienced users on here who will go out of there way to help too - often with some very technical info - if you get stuck..
More users, and especially more user generated content would help the cause.
Martin
Re: Pulse width measurement with Arduino
Hello Martin, I have solved my display problem. In the Flowcode Display the data lines D0-D3 are shown under the properties as connection to the display. But D4-D7 is connected. Really useful such a specification...
I have now tested the pulse counting on the UNO, it works fine. I don't understand why a simulation incl. display etc. in Flowcode should be useful if only nonsense comes out of it. Which causes time and trouble. Then better no simulation possibility at all. I will also tell the Flowcode Support my opinion about this.
But thanks again for your support!
Greetings Christian
I have now tested the pulse counting on the UNO, it works fine. I don't understand why a simulation incl. display etc. in Flowcode should be useful if only nonsense comes out of it. Which causes time and trouble. Then better no simulation possibility at all. I will also tell the Flowcode Support my opinion about this.
But thanks again for your support!
Greetings Christian
-
- 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
Hi Christian,
Glad to hear you got it working. Wires in the wrong place rather than bits? - If you upload your code I'll have a look and see if we work out why the simulation didn't work for you. It should certainly be possible to get a display working with 'timings' too - though it won't be possible to get accurate pulses (esp for short pulses), which I would guess would be very machine dependent (and what else Windows is working on at the time)
Martin
Glad to hear you got it working. Wires in the wrong place rather than bits? - If you upload your code I'll have a look and see if we work out why the simulation didn't work for you. It should certainly be possible to get a display working with 'timings' too - though it won't be possible to get accurate pulses (esp for short pulses), which I would guess would be very machine dependent (and what else Windows is working on at the time)
Martin