It works! Here's the program (see below) running on the Pi:
This is a very useful link:
http://www.raspberrypi-spy.co.uk/2012/08/reading-analogue-sensors-with-one-gpio-pin/
as is this:
https://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/basic-photocell-reading
If you just want the code:
- #!/usr/bin/env python
- # 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
- GPIO.setmode(GPIO.BCM)
- def RCtime (RCpin):
- reading = 0
- GPIO.setup(RCpin, GPIO.OUT)
- GPIO.output(RCpin, GPIO.LOW)
- time.sleep(0.1)
- 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(18) # Read RC timing using pin #1
#!/usr/local/bin/python
# Reading an analogue sensor with
# a single GPIO pin
# Author : Matt Hawkins
# Distribution : Raspbian
# 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
No comments:
Post a Comment