How can I read Hertz frequency from a LoPy Pin?
-
Hi, I'm using a LoPy 4 mounted on an expansion board.
I need to read the pulses frequency from a water flow sensor, I tried to use PWM library, but I don't really know how that should work.
Here's what I tried:from time import sleep import machine rmt = machine.RMT(channel=3) rmt.init(gpio="P13", rx_idle_threshold=16000) lt = 0 while True: data = rmt.pulses_get(timeout = 5000) print(data) print()This i the output of a single impulse (i think):
((0, 1), (1, 2), (0, 1), (1, 17), (0, 1), (1, 2), (0, 1), (1, 1), (0, 1), (1, 228), (0, 1), (1, 2), (0, 1), (1, 81), (0, 1), (1, 15), (0, 1), (1, 51), (0, 1), (1, 11), (0, 1), (1, 64), (0, 1), (1, 4), (0, 1), (1, 231), (0, 1), (1, 2), (0, 1), (1, 1), (0, 1), (1, 17), (0, 1), (1, 1), (0, 1), (1, 1), (0, 1), (1, 30), (0, 1), (1, 1), (0, 1), (1, 17), (0, 1), (1, 1), (0, 1), (1, 1), (0, 1), (1, 237), (0, 1), (1, 17), (0, 1), (1, 2), (0, 1), (1, 231), (0, 1), (1, 17), (0, 1), (1, 232), (0, 1), (1, 18), (0, 1), (1, 231), (0, 1), (1, 17), (0, 1), (1, 233), (0, 1), (1, 17), (0, 1), (1, 232), (0, 1), (1, 17), (0, 1), (1, 232), (0, 1), (1, 18), (0, 1), (1, 232), (0, 1), (1, 17), (0, 1), (1, 250), (0, 1), (1, 250), (0, 1), (1, 250), (0, 1), (1, 249), (0, 1), (1, 250), (0, 1), (1, 250), (0, 1), (1, 749), (0, 1), (1, 250), (0, 1), (1, 499), (0, 1), (1, 500), (0, 1), (1, 249), (0, 1), (1, 250), (0, 1), (1, 250), (0, 1), (1, 250), (0, 1), (1, 500), (0, 1), (1, 1680), (0, 1), (1, 250), (0, 1))and if I blow in the sensor, the output list result to be void.
Can someone help me? Even with some example codes, if that's possible.Thank you
-
@Lorenzo-Comparini I do not know the signal structure of the sensor, but let's assume it's a digital 0/3.3V signal. Please check also the frequency of the sensor. The lowest frequency you can measure with RMT channel 3 is ~300Hz. If you want to get lower frequencies, you have to select for instance channel 7, going down to ~10 Hz. That may still be too high. If I look into your list, there seem to be too many short transitions. That indicates an oscillation at the input. So you will need a proper signal shaping between the sensor and the lopy, consisting of
- a RC lowpass, with about two time the maximum frequency of the sensor and
- a Schmitt-Trigger gate to ensure fast signal transitions at the LoPy input.
Do you have the chance to measure the signal of the sensor, e.g. with a simple logic analyzer or an oscilloscope. The name & model of the sensor may also help in supporting you.
-
@robert-hh
Here's the link for the sensor's documentation: http://www.mantech.co.za/Datasheets/Products/YF-S201_SEA.pdf, hope this can help support. -
@robert-hh I tried changing channel to 7, and now the length of the output result changes accordingly to the sensor movement, the maximum level I can reach is 128. How can i obtain the frequency in Hz?
-
@Lorenzo-Comparini So th efrequency is between 16 and 90 Hz, meaning that you have to use channel 7. The frequency should be something like 1000/(2 * pulse_length * 3.125)
-
@Lorenzo-Comparini If the maximum number is 128, then y.ou can as well go for channel 4 or 5, which gives a better resolution. Then replace 3.125 by 1.0
-
@robert-hh Please not, that he sonsor runs at 5V, and the input at the Lopy must not exceed 3.3 V.
-
@robert-hh I know, the VCC is joined to an external power source at 5V
-
@robert-hh I just tried both, but the maximum legth of the pulse is still 128
-
@Lorenzo-Comparini What you do is guessing. Get yourself an independent method for displaying the signals. Like a 10 € logic analyzer, available e.g. at Amazon: https://www.amazon.de/AZDelivery-⭐⭐⭐⭐⭐-Logic-Analyzer-gratis/dp/B01MUFRHQ2/ref=sr_1_5?__mk_de_DE=ÅMÅŽÕÑ&crid=1LM4V4HE5L3CG&dchild=1&keywords=logic+analyzer&qid=1623852278&sprefix=Logic+%2Caps%2C181&sr=8-5
-
@robert-hh thanks, guess I'll try that, can you explain me what is that for and what it does? sorry but I'm very noob at this
-
@Lorenzo-Comparini That is the front end of a software, which display the function of signal level over time. SO you can see the signal. In you case some kind of high/low level changes. And you can measure it's timing properties. It supports 8 input channels. The software you need is either 'pulseview' or the Logic software. Links:
https://sigrok.org/wiki/PulseView
https://www.saleae.com/downloads/ -
@robert-hh Thank you very much, hope to solve this soon
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login