YARPF

Raspberry Pi Logo YetAnotherRaspberryPiFanpage

Photo Resistor

Plan

wiring plan

Board

bread board

Code

#!/usr/bin/env python

# http://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi

# 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
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)

def RCtime (RCpin):
        reading = 0

        ### Charge
        GPIO.setup(RCpin, GPIO.OUT)
        GPIO.output(RCpin, GPIO.LOW)
        sleep(0.1)

		### Discharge
        GPIO.setup(RCpin, GPIO.IN)

        # This takes about 1 millisecond per loop cycle
        while (GPIO.input(RCpin) == GPIO.LOW):
                reading += 1

        return reading

try:
    while True:
        # Read RC timing using pin #18
        val = RCtime(18)

        print val
        if val > 1000:
            print("Lights ON")
            GPIO.output(25, GPIO.HIGH)
        else:
            print("OFF")
            GPIO.output(25, GPIO.LOW)

        # Do a reading every three seconds
        sleep(3)

except KeyboardInterrupt:
    pass

GPIO.cleanup()

Scratch

TBD

Download

Comments ?

blog comments powered by Disqus