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

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

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.

Answer from user32264 on Stack Exchange
🌐
GitHub
github.com › sanderjo › GPIO-counter
GitHub - sanderjo/GPIO-counter: Counts pulses on specified GPIO pin and writes to a logfile
October 18, 2014 - @reboot /home/pi/git/GPIO-counter/gpio-counter.py 23 /var/log/gpio23-counter.log & Don't forget the ampersand at the end · Reboot your Raspberry, and check the daemon is running: $ ps -ef | grep -i gpio | grep python root 2136 1 0 12:22 ? 00:00:00 python /home/pi/git/GPIO-counter/gpio-counter.py 23 /var/log/gpio23-counter.log ·
Starred by 8 users
Forked by 8 users
Languages   Python 100.0% | Python 100.0%
Discussions

python - Coin Counter in Raspberry Pi (RPi-GPIO) - Stack Overflow
As the connections of the coin counter. With the python code only we won't be able to help. ... 4 wire outputs: black and red are 12v (font), white is pulse on 23 pin (1 constantly, 0 when drop the coin) and purple is to count coins on separeted led. Font 12v, and pulse (white wire) is 0.19~0.25 volts, and i used 10k resistor in this wire. And i use Raspberry pi B+ ... I edited/updated the question. ... import RPi.GPIO ... More on stackoverflow.com
🌐 stackoverflow.com
python - PIR counter for Raspberry Pi - Stack Overflow
I am trying to write a Python script that counts how many times a PIR sensor is tripped within a given time interval (1 minute). My issue is that my code is only adding +1 to the count in every loop, regardless of how many times the PIR is tripped and my loop is taking longer than the time interval I assign. Any advice is greatly appreciated. from collections import Counter import RPi.GPIO ... More on stackoverflow.com
🌐 stackoverflow.com
Gpio pulse counter pi3 - Raspberry Pi Forums
Hi there, I was use a python script on my pi to readout my pulse counter for my solarsystem. I used a simple script : ... #!/usr/bin/python import RPi.GPIO as GPIO import datetime GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) workfile = '/var/log/gpio23-counter' ... More on raspberrypi.org
🌐 raspberrypi.org
September 6, 2018
python - Update counter for GUI, for every high signal from GPIO - Stack Overflow
I am trying to input a signal from GPIO and I've assigned it to a counter where it will count every high signal input. However, GUI only shows '1'. I am probably confused with the loop, as the coun... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › hardware and peripherals › raspberry pi pico › general
How to count pulses with PIO? - Raspberry Pi Forums
There is some MicroPython sample code for interrupts at https://github.com/raspberrypi/pico-mic ... q.py#L1-L5 ... The PWM module on the chip seems to have a counter that can be configured to count rising/falling edges from a GPIO pin. See page 540 of the RP2040 datasheet. Potentially not worth the effort for low frequencies though. Electronic and Computer Engineer Pi Interests: Home Automation, IOT, Python and Tkinter
Top answer
1 of 2
1
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
counterPin=23
GPIO.setup(counterPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(counterPin)
    if input_state == False:
        print('coin dropped')
2 of 2
0
again=True

Total=0

Wrongs=0

Corrects=0


Total_bags=0

Wrong_bags=0

Correct_bags=0

coins=['1p','2p','5p','10p','20p','50p','£1','£2']

bag_value=[1.00,1.00,5.00,5.00,10.00,10.00,20.00,20.00]

bag_amount=[100,50,100,50,50,20,20,10]

bag_weight=[3.56,7.12,3.25,6.5,5.0,8.0,8.75,12.0]

coins_len=len(coins)

Master_list=list()

CoinCount_list=list()

Tilte_list=['Name','Coin','Weight','Accurate']

Master_list.append(Tilte_list)

with open("Master.txt","w") as f: 
    f.write("{}".format(Master_list))

while True:

    again=True


    what=input("\nMenu\n1)\tAdd a bag\n2)\tStats\n")

    CoinCount_list=list()
    if what=='1':

        name=input("What is your name\n")

        CoinCount_list.append(name)

        with open("CoinCount.txt","w") as f: 
            f.write("{}".format(CoinCount_list))


        with open("CoinCount.txt","w") as f: 
            f.write("{}".format(CoinCount_list))

        Percent=0

        Wrongs=0

        Corrects=0 


while again:

Coin

        coin_input=input("What type of coin do you have\n")
        for i in range (0,coins_len):
            if coin_input==coins[i]:

                Coin=coins[i]
                valid=input("That is valid, is that what you wanted\n")

                if valid=="Yes":


                    CoinCount_list.append(coins[i])

                    with open("CoinCount.txt","w") as f: 
                        f.write("{}".format(CoinCount_list))

                    while True:
                        try:    
                            weight=int(input("What is the weight of the bag\n"))
                            break
                        except ValueError:
                            print("Oops!  That was no valid number.  Try again...\n")
                    amountCoin=weight/bag_weight[i]

Weight

                    CoinCount_list.append(weight)

                    with open("CoinCount.txt","w") as f: 
                        f.write("{}".format(CoinCount_list))

                        if bag_amount[i] == amountCoin:

                            Total_bags=Total_bags+1

                            again=input("Thank you would you like to add another bag\n")

                            CoinCount_list.append('Correct')

                            with open("CoinCount.txt","w") as f: 
                                f.write("{}".format(CoinCount_list))

                            Total=bag_value[i]+Total
                            Correct_bags=Correct_bags+1

                            Corrects=Corrects+1

                            if again== 'Yes':
                                a='b'



                            else:
                                again=False


                                Master_list.append(CoinCount_list)

                                with open("Master.txt","w") as f: 
                                    f.write("{}".format(Master_list))







                        elif bag_amount[i] > amountCoin:
                            print("You have ",amountCoin,"coins, add ",amountCoin-bag_amount[i], "coin\n")

                            Wrong_bags=Wrong_bags+1
                            Total_bags=Total_bags+1

                            Wrongs=Wrongs+1



                        elif bag_amount[i] <amountCoin:
                            print("You have ",amountCoin,"coins, take away ",amountCoin-bag_amount[i], " coin\n")

                            Wrong_bags=Wrong_bags+1
                            Total_bags=Total_bags+1

                            Wrongs=Wrongs+1



if what=='2':
        print("")
        print("  We have check",Total_bags," ")
        print(" ",Correct_bags,"Correctly")
        print(" ",Wrong_bags,"Incorrectly")
        print("  We have raised £",Total," ")
        print("")
        with open("Master.txt") as f:
           rd=f.readlines()
        #print (rd)

        print(*Master_list, sep='\n')

        f = open("CoinCount.txt", "w")
        f.write( str(Master_list) ) 
        f.close()
🌐
Stack Overflow
stackoverflow.com › questions › 65388612 › pir-counter-for-raspberry-pi
python - PIR counter for Raspberry Pi - Stack Overflow
from collections import Counter import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.IN) while True: #this will create an infinite loop BUT the next while argument will make each loop interval 60 seconds * 1 minute counter=0 t_end = time.time() + 60 * 1 #60 seconds * 1 while time.time() < t_end: i=GPIO.input(11) if i==True: counter = counter + 1 print(counter) ... Fix your identation, time.sleep(60) is not correctly idented, it would raise an error. ... Your code right now just checks every 60 seconds. It doesn't check for 60 seconds ... Try copy and pasting the code you wrote here and run it. You will get an error at time.sleep(60). Is time.sleep(60) inside the while loop? is it inside the if statement. Neither python, nor we can tell.
🌐
GitHub
github.com › balena-labs-projects › pulse
GitHub - balena-labs-projects/pulse: A block for counting pulses on a Raspberry Pi GPIO pin. · GitHub
April 5, 2024 - This block utilizes the "raspberry-gpio-python" module, commonly known as RPi.GPIO. Specifically, the event_detected() function is used in a while loop that counts the pulses. A separate thread runs each second to aggregate the pulses counted ...
Author   balena-labs-projects
🌐
Raspberry Pi
raspberrypi.org › forums › viewtopic.php
Gpio pulse counter pi3 - Raspberry Pi Forums
September 6, 2018 - Hi there, I was use a python script on my pi to readout my pulse counter for my solarsystem. I used a simple script : ... #!/usr/bin/python import RPi.GPIO as GPIO import datetime GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) workfile = '/var/log/gpio23-counter' counter = 0 while True: GPIO.wait_for_edge(23, GPIO.RISING) # reading try: f = open(workfile, 'ab+') # open for reading.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 62567515 › update-counter-for-gui-for-every-high-signal-from-gpio
python - Update counter for GUI, for every high signal from GPIO - Stack Overflow
from guizero import App, Text import RPi.GPIO as GPIO import time sensor = 16 global count GPIO.setmode(GPIO.BOARD) GPIO.setup(sensor,GPIO.IN) app = App("Counter") text = Text(app, text="1") #when GPIO is high, however this only stops to 1 if GPIO.input(sensor): print('detected') def counter(): #add counter and change value by adding one text.value = int(text.value) + 1 while GPIO.input(sensor): time.sleep(0.01) app.display() python · user-interface · tkinter · raspberry-pi ·
🌐
Stack Overflow
stackoverflow.com › questions › 48073732 › counter-using-python-and-rpi-does-not-count-consistently
raspberry pi - Counter using Python and RPi does not count consistently - Stack Overflow
def counteryPlus(channel): if GPIO.input(channel): countery += 1 else: countery += 0 def counterzPlus(channel): if GPIO.input(channel): counterz += 1 else: counterz += 0 GPIO.add_event_detect(5, GPIO.FALLING, callback=counteryPlus, bouncetime=800) GPIO.add_event_detect(23, GPIO.FALLING, callback=counterzPlus, bouncetime=800)
🌐
GitHub
github.com › kgbplus › gpiotest
GitHub - RomanMindlin/gpiotest: Raspberry Pi gpio monitoring utility · GitHub
Small console utility to do gpio pin monitor & Raspberry Pi DYI projects debugging · To start, enter sudo python gpiotest.py from console
Starred by 65 users
Forked by 31 users
Languages   Python
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › using the raspberry pi › beginners
GPIO signal counter with GUI - Raspberry Pi Forums
November 16, 2021 - from guizero import App, Text # Action you would like to perform def counter() text.value = int(text.value) + 1 app = App("Hello world") text = Text(app, text="1") text.repeat(1000, counter) # Schedule call to counter() every 1000ms app.display() when i try this method, the counter will just count values to unlimited without stopping ... from guizero import App, Text import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(12,GPIO.IN, pull_up_down = GPIO.PUD_UP) mycount = 0 # Action you would like to perform def counter(btn): global mycount mycount = mycount + 1 text.value = mycount app = App("Hello world") text = Text(app, text="0") GPIO.add_event_detect(12, GPIO.FALLING, callback = counter) app.display()
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Odd counter behaviour with Python and GPIO - Raspberry Pi Forums
When you set up the input pins, you would also need to say what event you want to detect, and you need to register a call back function. The main function then loops doing nothing while waiting for the callback to be triggered. I haven't tested this code, so you might need to do some debugging. ... #!/usr/bin/env python import RPi.GPIO as GPIO def relayEventHandler(pin): if lane1_lap <= race_length | lane2_lap <= race_length: if pin==17: lane1_lap +=1 if pin==21: lane2_lap +=1 def main(): GPIO.setmode(gpio.BCM) GPIO.setup(17,gpio.IN) GPIO.setup(21,gpio.IN) GPIO.add_event_detect(17,GPIO.RISING) GPIO.add_event_callback(17,relayEventHandler,100) GPIO.add_event_detect(21,GPIO.RISING) GPIO.add_event_callback(21,relayEventHandler,100) while True: pass: if __name__=="__main__": main() You can read more here: http://raspberrywebserver.com/gpio/usin ...
🌐
Raspberry Pi
raspberrypi.org › forums › viewtopic.php
Creat counter with RPi.GPIO add_event_detect callback performance - Raspberry Pi Forums
November 2, 2017 - def increaseCounter(channel): global counter last_reading = False while True: # or use a global variable to stop thread on quitting reading = GPIO.input(channel) if reading and not last_reading: counter = counter +1 # something more to do ... last_reading = reading sleep(0.0002) t = threading.Thread(target=increaseCounter, args=(channel, )) t.start() while True: sleep(interval) # Read counter and add time of reading ... PPS see comment from @joan below. pigpio was the first thing that occurred to me when I saw this thread but I forgot to mention it (in my curiosity about the callback issue)!
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Python counter items - Raspberry Pi Forums
So looking up your sensor ( it would have been nice if you had included a link to the sensor data sheet or purchase site ) it looks to be a 5v device, so you can't connect the output of the sensor directly to a gpio pin as the gpio on the pi is not 5v tolerant. You will need to use a potential divider circuit to reduce the output to 3v like this As for code to read the input some thing as simple as this should enable you to test it. ... import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(25, GPIO.IN) count = 0 # wait for sensor trigger loop while True: print (count) # waiting for item while GPIO.input(25) == 0: time.sleep(0.1) count = count + 1 # waiting for item to pass while GPIO.input(25) == 1: time.sleep(0.1) untested code
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
GPIO pins counting pulses - Raspberry Pi Forums
Thu Oct 05, 2017 12:17 pm Just looking for a simple program that I can count pulses per second on the GPIO pins, I have tried looking at RPM counters with no luck. Using an internet search tool with the string "raspberry pi gpio pulse counter" will return some results that will point you in ...