fabianpi
Posts: 13
Joined: Tue Mar 07, 2017 4:01 pm

Hall-Effect Sensor pulse input limits with Python

Sun May 12, 2019 9:22 pm

Hello,

I've been working with a hall-effect sensor in a race car to read rotations from a tire, I've been using a raspberry pi to do this but I've found that I can't receive accurate data from the hall-effect sensor.

I decided to see if the Pi CPU is able to read a limit of pulses from the hall-effect sensor with two scripts using RPI.GPIO.

I setup a PWM script that runs continuously at a fixed frequency (2Hz) and with another script I started a script that simply read rising interrupts and wait for 1 second to print an output with the amount of pulses readed at 1 second.

When I tested at 2Hz the interrupts where successfully readed but when I started to increase the number of Heartz, the number of pulses started to be less and less accurate. I then tried waiting 60 seconds and set it to 4Hz to see if I can get 240 pulses at 1 min, but I realized that the output was only 120...

So looks like I am not able to read accurate amount of pulses with the raspberry pi and want to know if I can look for other tools that could help me to achieve this. I was considering to use pigpio python module or try with another programming language like C with WiringPi.

any update I will share it here too.

This are my current codes for testing.

pwm_gen.py

Code: Select all

import RPi.GPIO as GPIO
import time as time

GPIO.setmode(GPIO.BCM)
GPIO.setup(13, GPIO.OUT)

pwm = GPIO.PWM(13, 4) # GPIO13, frequency=50Hz
pwm.start(0)

try:
    while True:
        pwm.ChangeDutyCycle(1)
        time.sleep(60)

except KeyboardInterrupt:
   GPIO.cleanup()

pwm.stop()
GPIO.cleanup()
and pulse_counter.py

Code: Select all

#!/usr/bin/env python2.7  
import RPi.GPIO as GPIO
import time  

counter_pulses = 0

GPIO.setmode(GPIO.BCM)  
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
  
def my_callback2(channel):  
    global counter_pulses
    counter_pulses += 1
    #print "Rising edge detected on 21" 
 
GPIO.add_event_detect(21, GPIO.RISING, callback=my_callback2, bouncetime=300)  
  
try: 
    time.sleep(1)
    print("amount of pulses in 1 second: \n{} pulses".format(counter_pulses))
    #while True: 
    #    time.sleep(5) 
  
except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit  
GPIO.cleanup()           # clean up GPIO on normal exit  

any help will be grateful!
Thanks

User avatar
joan
Posts: 13915
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Hall-Effect Sensor pulse input limits with Python

Sun May 12, 2019 9:38 pm

pigpio is likely to give the best results (but I'm the author so I would say that).

See viewtopic.php?p=632413

User avatar
MrYsLab
Posts: 312
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Hall-Effect Sensor pulse input limits with Python

Sun May 12, 2019 11:22 pm

I totally agree with Joan - never argue with genius! (Joan, not me). Take a look at https://github.com/MrYsLab/razmq/blob/m ... _pigpio.py. I built a class a while ago that might just do what you want. It uses an early version of the python-banyan framework https://mryslab.github.io/python_banyan/to pass data around, but you can easily ignore that.

The encoder data is being published to the greater system whenever you see the publish_payload method calls. It also has a method to scale the number of ticks published because running the motors at full speed overwhelmed the Pi. This was an early effort for me in dealing with the RPi and pigpio, so there may be a much better way to handle a large number of pulses coming in. Perhaps Joan can provide some guidance if what I am doing is not optimal.

fabianpi
Posts: 13
Joined: Tue Mar 07, 2017 4:01 pm

Re: Hall-Effect Sensor pulse input limits with Python

Mon May 13, 2019 12:20 am

Hey thanks for both replies!!!

I can confirm that I was finally getting better results with this pigpio module. After digging around checking the module and their functions I was able to set two different scripts.

1. Setup a PWM at a desired frequency (let's say 500Hz) and run it continuously.

2. Sets a callback for every rising edge from the pin and counts up a counter.

I was able to read 500 pulses in 1 second, with a margin of error of about +- 1 pulse. I think this could be good enough, I also tried to see how far could go, but looks like the callbacks starts to get less accurate data when you go up to 20kHz.

Thank you so much, I really hope this could solve my hall-effect issue!

pwmgenerator.py

Code: Select all

import pigpio, time

pi = pigpio.pi()    # pi accesses the local Pi's GPIO
pwm_pin = 13 
frq = 500 #24000     # in Hz

try:
    #pi.set_PWM_frequency(pwm_pin, frq)
    while True:
        #pi.set_PWM_dutycycle(pwm_pin, 255)
        pi.hardware_PWM(pwm_pin, frq, 500000)
        time.sleep(1)

except KeyboardInterrupt:
    pi.write(pwm_pin, 0)
    pi.stop()
pulse_counter.py

Code: Select all

import pigpio, time

pi = pigpio.pi()    # pi accesses the local Pi's GPIO
led_pi = 13 
input_pulse = 21

pulse_counter = 0

def my_callback(gpio, level, tick):
    global pulse_counter
    pulse_counter += 1
    #print("Callback is here")
    #print(gpio, level, tick)

cb = pi.callback(input_pulse, pigpio.RISING_EDGE, my_callback)

try:
    time.sleep(1)
    print("number of pulses in 1 second\n{} pulses".format(pulse_counter))

except KeyboardInterrupt:
    pi.write(led_pi, 0)
    pi.stop()

User avatar
joan
Posts: 13915
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Hall-Effect Sensor pulse input limits with Python

Mon May 13, 2019 7:31 am

You should not be losing any pulses at 500Hz.

I haven't tried recently but I'd assume a Pi3B+ could handle 50kHz.

Which Pi are you using? Is anything else going on in the background to take time from the Python script?

User avatar
joan
Posts: 13915
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Hall-Effect Sensor pulse input limits with Python

Mon May 13, 2019 5:17 pm

MrYsLab wrote:
Sun May 12, 2019 11:22 pm
...
Perhaps Joan can provide some guidance if what I am doing is not optimal.
That looks fine to me.

Return to “Python”