0

I'm trying to make a simple program that calculates the frequency at a pin on the board. I'm using Python for the programming. I wrote two different programs and they gave the same (wrong!) results. I expect both of my programs to wait until I attach a voltage source to the pin (to simulate a rising edge) and then disconnect it (to simulate a falling edge) before printing out the value of the frequency (how fast I connected/disconnected). But before I can even get off my chair to handle the wire, the program finishes and prints out a value of ~60. Here are the two programs (they both are meant to do similar things). I wrote the second thinking that I was using the functions in the first incorrectly (first time suing this library and I'm also a novice at Python).

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN)

GPIO.wait_for_edge(pin, GPIO.FALLING)
start = time.time()
GPIO.wait_for_edge(pin, GPIO.RISING)
GPIO.wait_for_edge(pin, GPIO.FALLING)
duration = time.time() - start
frequency = 1 / duration
print frequency

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
pin = 12
GPIO.setup(pin, GPIO.IN)

record = GPIO.input(pin)
while GPIO.input(pin) == record:
    pass
start = time.time()
record = GPIO.input(pin)
while GPIO.input(pin) == record:
    pass
record = GPIO.input(pin)
while GPIO.input(pin) == record:
    pass
duration = time.time() - start
frquency = 1 / duration
print frequency

Is there a fault in my code?

  • 1
    A gpio set as an input will float randomly between off and on until it is driven high or low. – joan Mar 19 '15 at 6:25
  • Thank you! It worked! Is there a way to select your response as the answer. – Max Jacob Mar 19 '15 at 17:22
  • No need, you already have. – joan Mar 19 '15 at 17:27

Your Answer

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

Browse other questions tagged or ask your own question.