I'm trying to build like a rev counter, so I want it to count how many times the circuit is closed every 0,5/1 second and then simply convert that to minutes to it'll be Revs per min. Which GPIO pins would I have to use? (I'm using Rasbian, latest version as of 3/12/15) I'm using a Raspberry Pi 1st Gen Model B Rev 2

  • 1
    Have you tried googling raspberry pi rev counter or raspberry pi pulse counter?
    – joan
    Commented Dec 3, 2015 at 20:19

1 Answer 1


Here is a sample Python(2.7) script that may work:

import RPi.GPIO as g
from time import sleep
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:
    print "RPM is {0}".format(revcount)
    revcount = 0

What this does is setup event detection on channel 2, which has a physical pull-up resistor. Anytime that pin detects a change from LOW to HIGH, it will increase the revcount, then print the current RPM each minute, resetting the counter each time.

  • So which GPIO pins do I have to connect the two wires to? (So then when the circuit is closed it adds to revcount) (sorry this is my first Pi project)
    – Junaid
    Commented Dec 3, 2015 at 22:30
  • Connect the non-ground to BCM GPIO 2 (Board Pin 3), and the ground wire to any GND pin.
    – user32264
    Commented Dec 3, 2015 at 22:45
  • I get quite a few errors dropbox.com/s/4doikeonu1794fo/…
    – Junaid
    Commented Dec 4, 2015 at 18:50
  • The comma in the from time import sleep line was my bad, shouldn't be there. However, the other error has to do with how you defined the function(you didn't). If you copy/paste my script directly into a text-editor (Leafpad, as you are running a GUI), then save it as "revcount.py", you can open it in IDLE or Python CLI (python revcount.py) to run it.
    – user32264
    Commented Dec 4, 2015 at 19:16
  • I get the error -> AttributeError: 'module' object has no attribute 'BCIM' (I copied and pasted into leafpad and then opened in IDLE)
    – Junaid
    Commented Dec 4, 2015 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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