User avatar
TR4Aman
Posts: 21
Joined: Sat Nov 23, 2013 7:31 pm
Location: Spain

Hall effect and GPIO

Sat Nov 23, 2013 7:59 pm

Hi All, relatively new to RPI and python but have read alot of tutorials on GPIO interrupts etc.
I am setting up a carputer on a classic car and have written something in python to read 3 temperature sensors (DS18B20). Now I want to use two hall effect sensor (http://www.ebay.co.uk/itm/190823658126? ... 1497.l2649) to get an accurate measure of RPM and speed. I have written something in python so I can print out when the magnet passes the sensor which all works OK. However, I am stuck on collating the switch counting which will be no more than 100hz for rpm and 30hz for the speed. So i need to count the signals/second or time lapse between signals to calculate RPM and speed. Cant seem to find and easy solution. Any suggestions much appreciated

thanks

TR4Aman import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)
import time
for i in range(2000000):
if GPIO.input(11):
print "."
else:
print "touching!"
time.sleep(.01)[/code]

User avatar
DougieLawson
Posts: 36897
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall effect and GPIO

Sat Nov 23, 2013 11:10 pm

Why does a Triumph TR4A need a computer? It's a thing of beauty (give or take the Lucas electrics).
I wouldn't dream of adding anything to my Slough built '52 Citroën TA (which also has Lucas electrics).

You probably want to use the interrupt driven form of GPIO to wait for a falling edge: http://raspi.tv/2013/how-to-use-interru ... d-rpi-gpio
http://raspi.tv/2013/how-to-use-interru ... pio-part-2

You're also going to need some protection between your hall effect sensor and the RPi, since it works at 4V5-24V and the RPi GPIO pins are all 3V3 (and don't react well to over voltage).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Hall effect and GPIO

Sun Nov 24, 2013 12:19 am

Check the specs. My 3144s were open collector outputs. I just connected them direct to my Pi and counted rising edges (i.e. the transitions from 0 to 1).

User avatar
TR4Aman
Posts: 21
Joined: Sat Nov 23, 2013 7:31 pm
Location: Spain

Re: Hall effect and GPIO

Sun Nov 24, 2013 11:38 am

Hi Dougie,
Thanks for the quick respons. I wasn’t expecting my first reply on this forum to be from a classic car purist, I was relying on no TR/RPi forum crosstalk as if word gets out of what I am doing, and the TR register will string me up. What I really want to do is control some LED front daylights and do away with the original art deco sidelight and fit a front and rear parking cameras….only kidding.
The real reason I have started this as a RPi project is I think the old mechanical gauges running of worm drives and cables are now 50 years old so despite there being little needle wobble I doubt their accuracy. I will make no permanent changes to the car and hope to transfer my data over blue tooth to a nexus 7 using something like Torque (https://play.google.com/store/apps/deta ... e&hl=en_GB) but not using OBD 2. Thus when I meet other TR owner I can hide away any evidence of my evil secret.
Returning to the question regarding GPIO I have read the tutorials you have linked and agree that I will need GPIO.wait_for_edge(17, GPIO.FALLING), although I don’t fully understand why GPIO.INPUT which recognises the switching won’t do. What I am stuck on is how to either count each switch or measure time gap between switches and then do some calculations on the collated data RPM/MPH.
I am running the A3114 sensors at 3.3V currently so the RPi pins should be safe; however I am not sure if upping the voltage to 5v will increase sensitivity to the magnetic field.
Hi Joan,
My A3144s are reading High normally and Low when in front of magnet and I am running them at 3v3 from the RPi. My problem is I cannot find any command for counting the transitions from 1 to 0.

John

User avatar
DougieLawson
Posts: 36897
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall effect and GPIO

Sun Nov 24, 2013 12:21 pm

I love the speedo wobble and can get a good guess of what speed I'm really doing from the cars that pass me and the lorries that are restricted to 90Km/h (56mph). I'm considering adding a cigarette lighter socket (my car is 12V) then I could use a TomTom for an accurate speedo and mileometer. You could do GPS with a RPi.

The main reason for running GPIO with a falling edge trigger is that your program is better behaved.

If we constantly poll a GPIO pin in a loop there's two problems
  1. You will then to run 100% cpu busy
  2. You may miss a signal because the processor is doing something else when it arrives (remember that multitasking is a myth, with one cpu (and nothing farmed out to the GPU) you really are running one thing at a time (with some smoke and mirrors (in the Linux kernel) to make it look like it's doing more than one thing).
With GPIO.wait_for_edge() your program will sit in a wait state until the interrupt posts it as ready. You're less likely to miss a signal (until you get close to CPU frequency).

http://www.raspberrypi.org/phpBB3/viewt ... 24#p265624
http://www.raspberrypi.org/phpBB3/viewt ... 72&t=33113
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Hall effect and GPIO

Sun Nov 24, 2013 12:55 pm

A hall effect device acts like a switch.

Code: Select all

#include <stdio.h>

#include <pigpio.h>

#define HALL 14

void alert(int gpio, int level, uint32_t tick)
{
   static uint32_t lastTick=0;

   if (lastTick) printf("%d %.2f\n", level, (float)(tick-lastTick)/1000000.0);
   else          printf("%d 0.00\n", level);

   lastTick = tick;
}

int main(int argc, char *argv[])
{
   int secs=60;

   if (argc>1) secs = atoi(argv[1]); /* program run seconds */

   if ((secs<1) || (secs>3600)) secs = 3600;

   if (gpioInitialise()<0) return 1;

   gpioSetMode(HALL, PI_INPUT);

   gpioSetPullUpDown(HALL, PI_PUD_UP);

   gpioSetAlertFunc(HALL, alert);

   sleep(secs);

   gpioTerminate();
}
The above code is C.

Recent Raspbians come with Python modules pre-installed to handle gpios.

User avatar
DougieLawson
Posts: 36897
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall effect and GPIO

Sun Nov 24, 2013 1:13 pm

Here's my crude modification to the code from Raspi.tv

Code: Select all

#!/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv/
# http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

# GPIO 17 set up as input. It is pulled up to stop false signals
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

print "Waiting for falling edge on port 17"
# now the program will do nothing until the signal on port 17
# starts to fall towards zero. This is why we used the pullup
# to keep the signal high and prevent a false interrupt

print "During this waiting time, your computer is not"
print "wasting resources by polling for a button press.\n"
print "Press your button when ready to initiate a falling edge interrupt."
try:
    count = 1
    while (count < 20):
      print "Waiting"
      GPIO.wait_for_edge(17, GPIO.FALLING)
      print "Found one"
      count += 1
    print "20 presses detected"
    print "\nFalling edge detected. Now your program can continue with"
    print "whatever was waiting for a button press."
except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit
The best part of doing this was I found an ancient version of RPi.GPIO was installed ahead of the latest version. So that's fixed today's bug for me.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
TR4Aman
Posts: 21
Joined: Sat Nov 23, 2013 7:31 pm
Location: Spain

Re: Hall effect and GPIO

Sun Nov 24, 2013 1:50 pm

Thanks Dougie/Joan, that will get me started nicely. I naively thought the answer was to be found in GPIO tutorials but really I should have been looking at Python basic commands. I better go back to basics.
By the way GPS wouldn't help with RPM and with speed there is a lag........not a major issue for tractor drives like ourselves :lol:

User avatar
DougieLawson
Posts: 36897
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall effect and GPIO

Sun Nov 24, 2013 5:30 pm

joan wrote: The above code is C.

Recent Raspbians come with Python modules pre-installed to handle gpios.
Can't get that to compile.

Code: Select all

[email protected] ~/picode # gcc -lpigpio -o piGPIO piGPIO.c
/tmp/ccLXJWKm.o: In function `main':
piGPIO.c:(.text+0xfc): undefined reference to `gpioInitialise'
piGPIO.c:(.text+0x11c): undefined reference to `gpioSetMode'
piGPIO.c:(.text+0x128): undefined reference to `gpioSetPullUpDown'
piGPIO.c:(.text+0x134): undefined reference to `gpioSetAlertFunc'
piGPIO.c:(.text+0x140): undefined reference to `gpioTerminate'
collect2: ld returned 1 exit status
[email protected] ~/picode #
demolib and checklib both worked OK.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Hall effect and GPIO

Sun Nov 24, 2013 5:59 pm

DougieLawson wrote:
joan wrote: The above code is C.

Recent Raspbians come with Python modules pre-installed to handle gpios.
Can't get that to compile.

Code: Select all

[email protected] ~/picode # gcc -lpigpio -o piGPIO piGPIO.c
/tmp/ccLXJWKm.o: In function `main':
piGPIO.c:(.text+0xfc): undefined reference to `gpioInitialise'
piGPIO.c:(.text+0x11c): undefined reference to `gpioSetMode'
piGPIO.c:(.text+0x128): undefined reference to `gpioSetPullUpDown'
piGPIO.c:(.text+0x134): undefined reference to `gpioSetAlertFunc'
piGPIO.c:(.text+0x140): undefined reference to `gpioTerminate'
collect2: ld returned 1 exit status
[email protected] ~/picode #
demolib and checklib both worked OK.
I was being cheeky. You need the library. http://abyz.co.uk/rpi/pigpio/index.html

User avatar
DougieLawson
Posts: 36897
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall effect and GPIO

Sun Nov 24, 2013 6:04 pm

joan wrote: I was being cheeky. You need the library. http://abyz.co.uk/rpi/pigpio/index.html
I worked that out from reading the code. I've got your library and had already done the make && make install for it.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Hall effect and GPIO

Sun Nov 24, 2013 6:31 pm

Not sure if that's because the library is static rather than dynamic.

You need to specify the library after the code using symbols from the library.

gcc -o piGPIO piGPIO.c -lpigpio

User avatar
DougieLawson
Posts: 36897
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall effect and GPIO

Sun Nov 24, 2013 7:07 pm

joan wrote:Not sure if that's because the library is static rather than dynamic.

You need to specify the library after the code using symbols from the library.

gcc -o piGPIO piGPIO.c -lpigpio
That made a difference. But I think it's given me the clue to fix it.

Code: Select all

[email protected] ~/picode # gcc -o piGPIO dugPIO.c -lpigpio
/usr/local/lib/libpigpio.a(pigpio.o): In function `myGpioDelay':
pigpio.c:(.text+0x51c): undefined reference to `clock_nanosleep'
/usr/local/lib/libpigpio.a(pigpio.o): In function `pthTimerTick':
pigpio.c:(.text+0x580): undefined reference to `clock_gettime'
pigpio.c:(.text+0x598): undefined reference to `clock_gettime'
/usr/local/lib/libpigpio.a(pigpio.o): In function `initClock':
pigpio.c:(.text+0x1618): undefined reference to `clock_nanosleep'
pigpio.c:(.text+0x16fc): undefined reference to `clock_nanosleep'
pigpio.c:(.text+0x1768): undefined reference to `clock_nanosleep'
pigpio.c:(.text+0x17dc): undefined reference to `clock_nanosleep'
/usr/local/lib/libpigpio.a(pigpio.o): In function `gpioTerminate':
pigpio.c:(.text+0x25a8): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x25b4): undefined reference to `pthread_join'
pigpio.c:(.text+0x25c8): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x25d4): undefined reference to `pthread_join'
pigpio.c:(.text+0x25e8): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x25f4): undefined reference to `pthread_join'
pigpio.c:(.text+0x2608): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x2614): undefined reference to `pthread_join'
pigpio.c:(.text+0x2628): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x2634): undefined reference to `pthread_join'
pigpio.c:(.text+0x2648): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x2654): undefined reference to `pthread_join'
pigpio.c:(.text+0x2668): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x2674): undefined reference to `pthread_join'
pigpio.c:(.text+0x2688): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x2694): undefined reference to `pthread_join'
pigpio.c:(.text+0x26a8): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x26b4): undefined reference to `pthread_join'
pigpio.c:(.text+0x26c8): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x26d4): undefined reference to `pthread_join'
pigpio.c:(.text+0x26e8): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x26f4): undefined reference to `pthread_join'
pigpio.c:(.text+0x2708): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x2714): undefined reference to `pthread_join'
pigpio.c:(.text+0x2728): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x2734): undefined reference to `pthread_join'
/usr/local/lib/libpigpio.a(pigpio.o): In function `pthSocketThread':
pigpio.c:(.text+0x2934): undefined reference to `pthread_attr_setstacksize'
pigpio.c:(.text+0x2a08): undefined reference to `pthread_create'
/usr/local/lib/libpigpio.a(pigpio.o): In function `intGpioSetTimerFunc':
pigpio.c:(.text+0x2d84): undefined reference to `pthread_create'
pigpio.c:(.text+0x2eac): undefined reference to `pthread_cancel'
pigpio.c:(.text+0x2ec0): undefined reference to `pthread_join'
/usr/local/lib/libpigpio.a(pigpio.o): In function `gpioInitialise':
pigpio.c:(.text+0x3254): undefined reference to `clock_gettime'
pigpio.c:(.text+0x4920): undefined reference to `pthread_attr_setstacksize'
pigpio.c:(.text+0x493c): undefined reference to `pthread_create'
pigpio.c:(.text+0x4a90): undefined reference to `clock_nanosleep'
pigpio.c:(.text+0x4cfc): undefined reference to `pthread_create'
pigpio.c:(.text+0x4d28): undefined reference to `pthread_create'
/usr/local/lib/libpigpio.a(pigpio.o): In function `gpioTime':
pigpio.c:(.text+0xc9ac): undefined reference to `clock_gettime'
pigpio.c:(.text+0xca70): undefined reference to `clock_gettime'
/usr/local/lib/libpigpio.a(pigpio.o): In function `gpioSleep':
pigpio.c:(.text+0xcc38): undefined reference to `clock_nanosleep'
pigpio.c:(.text+0xcc5c): undefined reference to `clock_nanosleep'
pigpio.c:(.text+0xcc78): undefined reference to `clock_nanosleep'
/usr/local/lib/libpigpio.a(pigpio.o): In function `gpioDelay':
pigpio.c:(.text+0xd070): undefined reference to `clock_nanosleep'
collect2: ld returned 1 exit status
[email protected] ~/picode #
It compiles OK with

Code: Select all

gcc -o piGPIO piGPIO.c -lpigpio -lpthread -lrt
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Beginners”