Reading analog sensors without an ADC

Copy and paste the following Python code into a new file called photosensor.py, chmod +x it and run the script.

!/usr/bin/env python

import RPi.GPIO as GPIO, time, os

DEBUG = 0
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

light = 0
for i in range(1,3):
        light += RCtime(24)

print int(10000 / light) # Read RC timing using pin #24