2

I'm trying to count pulses using the GPIO pins and when I run this script I get the error (it's written in leafpad then I open it in IDLE)

Traceback (most recent call last): File "/home/Pi/Desktop/revcount.py", line 10, in g.add_event_detect(2, g.RISING, callback = increase rev) RuntimeError: Failed to add edge detection

Here's the code

import RPi.GPIO as g
from time import sleep
g.setmode(g.BCM)
g.setup(2, g.IN)
global revcount
revcount = 0
def increaserev(channel):
    global revcount
    revcount += 1
g.add_event_detect(2, g.RISING, callback=increaserev)
while True:
    sleep(60)
    print "RPM is {0}".format(revcount)
    revcount = 0
  • Does it work if you run with sudo? – joan Dec 6 '15 at 22:25
2

The relationship between GPIO numbers and pins is as follows.

GPIO       pin  pin    GPIO
3V3         1    2      5V
0/2 (SDA)   3    4      5V
1/3 (SCL)   5    6      0V
4           7    8      14 (TXD)
0V          9   10      15 (RXD)
17 (ce1)   11   12      18 (ce0)
21/27      13   14      0V
22         15   16      23
3V3        17   18      24
10 (MOSI)  19   20      0V
9 (MISO)   21   22      25
11 (SCLK)  23   24      8 (CE0)
0V         25   26      7 (CE1)
           .......
0 (ID_SD)  27   28      1 (ID_SC)
5          29   30      0V
6          31   32      12
13         33   34      0V
19 (miso)  35   36      16 (ce2)
26         37   38      20 (mosi)
0V         39   40      21 (sclk)

If the GPIO column has a V it indicates the pin is connected to the power rail and not a GPIO.

Where 2 figures are shown separated by a / the first figure is the GPIO number used on the first revision Pi B boards.

Here is a simple RPM counter.

#!/usr/bin/env python

import time
import pigpio # http://abyz.me.uk/rpi/pigpio/python.html

REV_GPIO=2    # GPIO getting pulses.
SAMPLE_TIME=3 # Recalculate every SAMPLE_TIME seconds.

pi = pigpio.pi()           # Connect to Pi.
cb = pi.callback(REV_GPIO) # Default tally callback.

old_count = cb.tally() # Pulse count.

try:
   while True:
      time.sleep(SAMPLE_TIME)
      new_count = cb.tally() # Pulse count.
      print("{} RPM".format(60.0*(new_count-old_count)/SAMPLE_TIME))
      old_count = new_count

except KeyboardInterrupt:
   cb.cancel() # Cancel callback.
   pi.stop()   # Disconnect from Pi.
  • When I run it in Python 2.7.9 it says import error: No module named PIGPIO (I downloaded it and it's there) then when I run it in Python 3.4.2 it says Name error:name 'pigpio' is not defined (referring to line 9) I tried pigpio.py but still get the same error – Junaid Dec 7 '15 at 18:39
  • To use that pigpio script you would need to download and install the pigpio library (as detailed in abyz.co.uk/rpi/pigpio/download.html) and then start the pigpio daemon (using sudo pigpiod). – joan Dec 7 '15 at 18:43
  • When I try to compile, link and run foorbar.c it gives an error saying No such file or directory – Junaid Dec 7 '15 at 20:15
  • You only need to do the download and install part. I.e. "wget abyz.co.uk/rpi/pigpio/pigpio.zip;unzip pigpio.zip;cd PIGPIO;make;sudo make install". The foobar.c example is a standard way of showing how you would compile your own C code. – joan Dec 7 '15 at 20:24
  • By the way your original script should have worked if you invoked the script with sudo python script.py or sudo idle as you were doing. – joan Dec 7 '15 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.