The Python Console

Before writing a Python program to measure and report the light level, we can try some experiments in the Python Console.

To launch the Python Console type:
Download: file
# python
Python 2.7.3 (default, Apr  3 2013, 21:37:23) 
[GCC 4.7.3 20130205 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
First, we need to import the library, so enter the command:
Download: file
>>> import Adafruit_BBIO.ADC as ADC
Now enter the command below into the Python Console to setup the ADC (Analog to Digital Convertor).
Download: file
>>> ADC.setup()
You can now read the value at the analog input (between 0 and 1) using the command below. Try it a few times while moving your hand closer to and further away from the photoresistor. The readings should change in response to your hand movements. Cover the photoresistor completely and the reading should drop almost to 0.
Download: file
>>> ADC.read("P9_40")
0.54777777194976807
>>> ADC.read("P9_40")
0.55000001192092896
>>> ADC.read("P9_40")
0.55166667699813843
>>> ADC.read("P9_40")
0.39388889074325562
>>> 
Note: Using the up arrow will repeat the last line that you entered in the Python Console.
This guide was first published on Jul 01, 2013. It was last updated on Jul 01, 2013. This page (The Python Console) was last updated on Nov 25, 2019.