DEBUG = 1
GPIO.setmode(GPIO.BCM)
def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.01)
GPIO.setup(RCpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading
while True:
print RCtime(4) # Read RC timing using pin #18
Sample 2
$ sudo nano analoguesensors2.py
# Python : 2.7
# GPIO : RPi.GPIO v3.1.0a
import RPi.GPIO as GPIO, time
# Tell the GPIO library to use
# Broadcom GPIO references
GPIO.setmode(GPIO.BCM)
# Define function to measure charge time
def RCtime (PiPin):
measurement = 0
# Discharge capacitor
GPIO.setup(PiPin, GPIO.OUT)
GPIO.output(PiPin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(PiPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
while (GPIO.input(PiPin) == GPIO.LOW):
measurement += 1
return measurement
# Main program loop
while True:
print RCtime(4) # Measure timing using GPIO4