Page 1 of 1

Measure RPM with a Hall Sensor

Posted: Thu Jul 21, 2022 2:25 pm
by Jodel
I have a Pico W and I hope to use it to measure RPM of a motor using a hall effect sensor. I have been searching on line and have found some examples of Python code on the Raspberry Pi to do this, but I can't seem to find anything specific for the Pico and MicroPython. Any suggestions as to how I could proceed?
Jodel

Re: Measure RPM with a Hall Sensor

Posted: Thu Jul 21, 2022 3:00 pm
by Roberthh
If the hall sensor has a good logic output, like e.g. the A11xx series, the you can connect it to a pin input and use pin.irq() to time the distance between two pulses with µs resolution, or use the PIO to measure the the time between pulses, with 8 ns resolution.
I have made a few examples for the PIO a while ago. https://github.com/robert-hh/RP2040-Exa ... ter/pulses
But there are more made by other people.
Once you have the duration of a high/low pulse pair, you have the frequency.

Re: Measure RPM with a Hall Sensor

Posted: Thu Jul 21, 2022 3:19 pm
by Jodel
Thanks for your help. It gives me some ideas as to how I can go about measuring RPM.

Re: Measure RPM with a Hall Sensor

Posted: Thu Jul 21, 2022 4:07 pm
by TheSilverBullet
Just keep in mind that there are unipolar and bipolar ones.
Also some are linear (analogue output) some are digital output.
You need to find the specific data-sheet for the one you're using.

Re: Measure RPM with a Hall Sensor

Posted: Thu Jul 21, 2022 7:12 pm
by Jodel
I have two sensors available to me. One is described as "A3144 Hall Effect Magnetic Field Sensor Arduino" Which is just a single small component with 3 wires coming out of it.
The other is called a "3144 Hall Sensor Module - KY-003 - Switching - 3-pin"
This has a similar sensor attached to a small PCB with 2 components on it.

Re: Measure RPM with a Hall Sensor

Posted: Thu Jul 21, 2022 7:29 pm
by Roberthh
These are the old versions of A1104. Unipolar & digital. Pretty robust. The middle pin is GND, the left (looking from the front, pins down) is Vcc, the right the open collector output. Supply range 4.5-25V. Switch point somewhere between 70 and 350 Gauss. Not very precise. The other model of the family are trimmed for a more precise switch field strength.
It needs a pull-up resistor at the output, like 1k Ohm.

Re: Measure RPM with a Hall Sensor

Posted: Thu Jul 21, 2022 8:09 pm
by Jodel
The two components on the circuit board appear to be the pull down resistor and a smoothing capacitor.
I now have it connected to the Pico and it is detecting when the magnet is present or not. I am using some micro-python I found on a Waveshare site. This is the code that is working for me :

Code: Select all

from machine import Pin,ADC
import utime

#Select ADC input 0 (GPIO26)
ADC_ConvertedValue = machine.ADC(0)
DIN = Pin(21,Pin.IN)
conversion_factor = 3.3 / (65535)


while True :
    if(DIN.value() == 1) :
        print("The Magnet is far!!!")
    else :
        print("The Magnet is near!!!")
        AD_value = ADC_ConvertedValue.read_u16() * conversion_factor
        print("The current Gas AD value = ",AD_value ,"V")
    utime.sleep(0.5)
    
Is it too simple to think that If I count the number of times the magnet is near for a fixed time, say 2 seconds, and then multiply by 30 I would have revs per minute?
Jodel

Re: Measure RPM with a Hall Sensor

Posted: Thu Jul 21, 2022 8:11 pm
by Jodel
The two components on the circuit board appear to be the pull down resistor and a capacitor.
I now have it connected to the Pico and it is detecting when the magnet is present or not. I am using some micro-python I found on a Waveshare site. This is the code that is working for me :

Code: Select all

from machine import Pin,ADC
import utime

#Select ADC input 0 (GPIO26)
ADC_ConvertedValue = machine.ADC(0)
DIN = Pin(21,Pin.IN)
conversion_factor = 3.3 / (65535)


while True :
    if(DIN.value() == 1) :
        print("The Magnet is far!!!")
    else :
        print("The Magnet is near!!!")
        AD_value = ADC_ConvertedValue.read_u16() * conversion_factor
        print("The current Gas AD value = ",AD_value ,"V")
    utime.sleep(0.5)
    
Is it too simple to think that If I count the number of times the magnet is near for a fixed time, say 2 seconds, and then multiply by 30 I would have revs per minute?
Jodel

Re: Measure RPM with a Hall Sensor

Posted: Fri Jul 22, 2022 2:43 pm
by pythoncoder
The two considerations are accuracy and response time. Counting pulses in a given interval is subject to one pulse of uncertainty. So if you count over 30s you have a 2rpm uncertainty. The shorter the interval, the greater the uncertainty but the less time to achieve a reading.

Timing the interval between consecutive pulses gives an accurate reading in the shortest possible time. However if the mechanism isn't precise you can get errors creeping in. Whereas counting pulses over a period averages out any errors in the edge precision: in this environment it can give a better reading.

Re: Measure RPM with a Hall Sensor

Posted: Tue Jul 26, 2022 4:52 pm
by horuable
I'm kinda late to the party, but maybe someone will find it useful or interesting, so here it goes:

The Pico has another way of counting frequency by using PWM in input mode. It's not directly supported by MicroPython, but I've written a simple module to do just that: https://github.com/phoreglad/pico-MP-mo ... PWMCounter there is an example in the repo on how to read frequency using it. It's a direct measurement method, so its resolution is dependent on sampling time and measured signal frequency. As such it's not really suitable for low frequency signals, because they require relatively long sampling times to be accurate.

A better approach is called the reciprocal frequency counter because its accuracy is tied only to sampling time and the reference clock. That means it works equally well for low and high frequencies for a given sampling time. My implementation using PIO can be found here: https://forums.raspberrypi.com/viewtopic.php?t=306250