Main Site Documentation

Duty cicle of square-wave


someone I can 'help, I need to extract from a square-wave duration of duty cicle.
How can I do?


Hi Scorpion, can you elaborate? You have a square wave signal that you need to use as an input to a netmf device, and from that figure out the duty cycle (which is the ratio of high time against the sum of high and low time). Is that correct?


Exactly, I have to calculate the length of time that the square wave is going high.
I have a square wave which is the input, is generated by an external device, and depending on which button I press change duty cycle (I have five buttons and then 5 different duty cycle).
My purpose is to recognize which button is pressed.
Using a gadgeteer mainboard ethernet


You could use an Interrupt Input and Trigger on both edges.
The time between rising and falling edge is the duty cycle.
I’m not sure if the NETMF Interrupt Event gets an exact timestamp (as paramter).
Depending on the frequency it might be to inaccurate for you to read System time in managed Event method.
If you have a premium board, you could use RLP code and RLP Interrupt to get exact Timing.
A simple example where you can see that is here:
For non premium boards from GHI this should also work using RLPLight


I’m not sure I understand…

Are you INPUTTING the square wave into your Gadgeteer board, or are you OUTPUTTING a square wave from the Gadgeteer board, and the duty cycle will be based on a button press?

You can’t INPUT a signal and change it’s duty cycle - that is created by the device that is sending the signal in the first place. You can only OUTPUT a wave and change the duty cycle.


@ Brett - As I understand scorpion1’s 2nd post its an external device with 5 buttons and a single output.
Depending on which button he press on that device a rect signal with different duty cycle is generated.

If he connects this to an NETMF device dig input and measures the time between rising and falling edge he could say which button is pressed.


ok Reinhard Ostermeir has framed the problem.
I understand that I must use an interrup on the front of uphill and then downhill, but as calculating the time between the two sides?


One of the parameters for the Interrupt handler is the time of the interrupt.

Subtract two and you will get the duration. Get the next rising edge and you will get the period (unless you already know it)


Thanks Architect,
but what is the parameter and the code for the calculation of the time?


Capture 3 events and take their time t1,t2,t3. The duty cycle will be

dutycycle = (t2-t1)/(t3-t1) or

dutycycle % = 100*(t2-t1)/(t3-t1)