Measure RPM with a Hall Sensor

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Jodel
Posts: 5
Joined: Thu Jul 21, 2022 2:18 pm

Measure RPM with a Hall Sensor

Post by Jodel » Thu Jul 21, 2022 2:25 pm

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

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Measure RPM with a Hall Sensor

Post by Roberthh » Thu Jul 21, 2022 3:00 pm

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.

Jodel
Posts: 5
Joined: Thu Jul 21, 2022 2:18 pm

Re: Measure RPM with a Hall Sensor

Post by Jodel » Thu Jul 21, 2022 3:19 pm

Thanks for your help. It gives me some ideas as to how I can go about measuring RPM.

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: Measure RPM with a Hall Sensor

Post by TheSilverBullet » Thu Jul 21, 2022 4:07 pm

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.

Jodel
Posts: 5
Joined: Thu Jul 21, 2022 2:18 pm

Re: Measure RPM with a Hall Sensor

Post by Jodel » Thu Jul 21, 2022 7:12 pm

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Measure RPM with a Hall Sensor

Post by Roberthh » Thu Jul 21, 2022 7:29 pm

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.

Jodel
Posts: 5
Joined: Thu Jul 21, 2022 2:18 pm

Re: Measure RPM with a Hall Sensor

Post by Jodel » Thu Jul 21, 2022 8:09 pm

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

Jodel
Posts: 5
Joined: Thu Jul 21, 2022 2:18 pm

Re: Measure RPM with a Hall Sensor

Post by Jodel » Thu Jul 21, 2022 8:11 pm

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Measure RPM with a Hall Sensor

Post by pythoncoder » Fri Jul 22, 2022 2:43 pm

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.
Peter Hinch
Index to my micropython libraries.

horuable
Posts: 4
Joined: Thu Mar 31, 2022 6:44 pm

Re: Measure RPM with a Hall Sensor

Post by horuable » Tue Jul 26, 2022 4:52 pm

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

Post Reply