0

To make a long story short I am trying to read frequency on the RPi2's GPIO pins. The goal eventually would be to use this frequency signal to control a PyGame platformer that I have created. However I am having some consistency problems with the RPi.GPIO library. I am running the code below just to test the functionality:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

pin = 4
pull_up_down = GPIO.PUD_OFF
GPIO.setup(pin, GPIO.IN, pull_up_down)

NUM_CYCLES = 10
run = True
while run:
        try:
            start = time.time()
            for impulse_count in range (NUM_CYCLES):
                GPIO.wait_for_edge(sensor, GPIO.FALLING)
            duration = time.time() - start
            frequency = NUM_CYCLES / duration
            print(frequency)

    except KeyboardInterrupt:
            GPIO.cleanup()
            run = False

I can run this code and get a pretty good reading, although the frequency value I get is not the actual frequency. That doesn't matter because it is the change in frequency that is important to me and I can clearly measure that. However, if I do a KeyboardInterrupt the program stops, as it should, and I get the following message in the terminal:

Message from syslogd@raspberrypi at Feb 17 01:34:53 ...

kernel:[ 373.646034] Disabling IRQ #49

From what I can tell the GPIO.cleanup() function is causing this, and if I run the code again it does not work, unless I reboot the Pi.

I have seen the question kernel Disabling IRQ #49 but to be honest I understand maybe half of it is happening while he is running his script. Is there anyone who has run into this problem or knows why this is happening and could maybe explain it to someone who is new to Raspberry and Linux?

1

It may be a bug. May be worth checking at http://sourceforge.net/p/raspberry-gpio-python/tickets/?source=navbar

However I doubt that what you are doing is the intended usage for the wait_for_edge function. I'd try with the RPi.GPIO module's callback function and in the callback simply increment a global counter.

I give an example using my pigpio Python module.

#!/usr/bin/env python

# tally.py
# 2014-01-23
# Public Domain

import time
import pigpio

GPIO=4 # pigpio always uses Broadcom numbering.

pi = pigpio.pi() # connect to local Pi

pi.set_mode(GPIO, pigpio.INPUT)
pi.set_pull_up_down(GPIO, pigpio.PUD_OFF)

# Set up a dummy callback.  It will simply count falling
# edges.  The tally method will return the current count.

cb = pi.callback(GPIO, pigpio.FALLING_EDGE)

start = time.time()

old_count = 0

while (time.time()-start) < 60: # run for 60 seconds

   time.sleep(1.0)

   count = cb.tally() # Get the number of pulses seen so far.

   # Display the number of pulses in the last second.
   print("counted {} pulses".format(count - old_count))

   old_count = count

pi.stop() # disconnect from Pi
  • Thanks for the response. I think this is definitely the way to go but it's a step in the right direction. I skimmed through the pigpio documentation and saw that it has a function pi.clear_bank_1(bits). I might be way off here but could that potentially fix the bug I have been experiencing with the GPIO library? Do you know how it works and if I use it to clear GPIO4 do I write pi.clear_bank_1(0x10)(0x10 = '10000' and according raspberry-projects.com/pi/pi-hardware/… GPIO04 is the fifth bit). Have you used this function? – bjarkijoha May 28 '15 at 6:19
  • clear_bank_1 can be used to clear one or more gpios in bank 1 (gpios 0-31 are in bank 1). It takes one parameter bits. If you want to clear gpiox you set bit x in bits, e.g. to clear gpio4 set bits to (1<<4). To clear gpios 7 and 11 set bits to (1<<7) | (1<<11). – joan May 28 '15 at 7:41

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.