Here is the full code from the http://learn.adafruit.com/basic-resisto ... ll-reading
Code: Select all
# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!
import RPi.GPIO as GPIO, time, os
DEBUG = 1
def RCtime (RCpin):
reading = 0
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
print RCtime(18) # Read RC timing using pin #18
The tutorial has a huge mistake in it. The code above uses "setmode" to declare BCM references. That means when you feed it "18" you are using GPIO18 ... which is actually physical pin 12. It should not refer to "Pin #18". It should refer to "Pin #12" or "GPIO18".
The process goes something like this :
1) Tell the GPIO library we are using GPIO references.
2) Define a function called RCTime that accepts a number (RCPin)
3) Then repeatedly call the function and feed it "18" as we want to use GPIO18 (pin 12). Print the result.
4) The function sets the pin as an output and sets it low for 0.1 seconds. This drains the capacitor.
5) Then it sets it as an input and loops until it goes high. This time is the time it takes for the capacitor to charge and is proportional to the resistance of the sensor.
So this example only uses one pin but switches it from an Output to an Input as required.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)