blob: a96af1bed50c726877c594398fdc8267dea0beb8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#!/usr/bin/python
# Reading an analogue sensor with a single GPIO pin
# Basic script from:
# /www.raspberrypi-spy.co.uk/2012/08/reading-analogue-sensors-with-one-gpio-pin/
# Author : Matt Hawkins -- Stephen Pratt (SJP)
# Distribution : Raspbian -- SJP Arch-arm
# Python : 2.7 -- SJP converted to v3.5
# GPIO : RPi.GPIO v3.1.0a
# PIN assignments:
# pin Name Connection
# 1 +3.3V power source
# 7 GPIO 4 read sensor
# 9 Ground Ground
# circuit characteristics
# series resistor = 220 Ohm
# LDR resistance (full sun) = 360 Ohm
# 'dusk' = 3 M Ohm
# capactitor = 10 uF (quite large)
# max current = 3.3/580 = 5.7 mA (well within spec)
import RPi.GPIO as GPIO, time
# set GPIO4 as read pin
sensorPin = 4
# set maximum time to wait (in which case it is dark)
maxWait = 5e6
obsCount = 6 # number of observations
# Tell the GPIO library to use Broadcom GPIO references
GPIO.setmode(GPIO.BCM)
# Define function to measure charge time
def RCtime ():
timer = 0
# Discharge capacitor
GPIO.setup(sensorPin, GPIO.OUT)
GPIO.output(sensorPin, GPIO.LOW)
time.sleep(0.1) # 100 ms
GPIO.setup(sensorPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
while (GPIO.input(sensorPin) == GPIO.LOW):
timer += 1
if timer > maxWait : return -1
return timer
# Main program loop
obs=list() # an empty list
for num in range(0, obsCount):
obs.append(RCtime())
print(obs)
print("sum=", sum(obs))
print("Ave time: ", sum(obs)/obsCount) # Measure timing using GPIO4
|