2
votes

I have a PWM input in my raspberry pi GPIO. I only need to detect the time while the GPIO reads the PWM or not.

For example if my raspberry pi receive these (|||| => PWM)

_______|||||||_______||||||||______________|||||||_______

I will get the time of each low and PWM

_______|||||||_______||||||||______________|||||||_______
[70 ms][70 ms][70 ms][70 ms][    140 ms   ][70 ms][70 ms]

or at least be able to converted to a Voltage level High like these (---- => HIGH)

_______-------_______--------______________-------_______
[70 ms][70 ms][70 ms][70 ms][    140 ms   ][70 ms][70 ms]

I need it to be like that so I can get the time length of the PWM.

How can I do this in real time in python?

1

1 Answers

0
votes
>>> import time
>>> start_time = time.time()
>>> while input(): #input from RaspberryPi GPIO
...   pass
...

>>> end_time = time.time()-start_time
>>> end_time  #Store this in a list
19.64651846885681
>>>

For each GPIO high or low signal, just reset the start_time and the end_time and calculate the time difference.
From the above code, replace the input() with the PWM signal. And you can calculate the time difference as above. Just use the above in a loop and store the values in a list to get the desired output.