To automate this, we can write a short Python program to repeatedly display the analog reading (between 0 and 1) and the voltage that this corresponds to, printing values once per second.

Exit the Python Console by typing:

>>> exit()
This should take you back to the Linux prompt.

Enter the following command to create a new files called light.py
nano light.py
Now paste the code below into the editor window.
import Adafruit_BBIO.ADC as ADC
import time

sensor_pin = 'P9_40'

ADC.setup()

print('Reading\t\tVolts')

while True:
    reading = ADC.read(sensor_pin)
    volts = reading * 1.800
    print('%f\t%f' % (reading, volts))
    time.sleep(1)
Save and exit the editor using CTRL-x and the Y to confirm.

To start the program, enter the command:
python light.py
You will then see a series of readings.
Reading		Volts
0.000556	0.001000
0.000000	0.000000
0.026111	0.047000
0.448889	0.808000
0.449444	0.809000
0.452222	0.814000
0.730000	1.314000
0.738889	1.330000
0.722778	1.301000
0.726111	1.307000
0.445556	0.802000
0.401667	0.723000
0.717778	1.292000
When you want to stop the readings, use CTRL-c.

The readings taken are not in any useful units of light intensity. Photoresistors are not carefully calibrated sensors. If you wanted to make a light meter, with absolute measurement of light intensity in meaningful units, you would need to create a lookup table that related readings with readings taken from a properly calibrated light meter.

Warning: The analog inputs of the BBB operate at 1.8V. So do not be tempted to connect the red lead to a higher voltage.

This guide was first published on Jul 01, 2013. It was last updated on Jul 01, 2013.

This page (Writing a Program) was last updated on Jul 01, 2013.

Text editor powered by tinymce.