It's easy to read how much light a photocell sees with CircuitPython and its built-in analog input support.  By wiring the photocell to an analog input of your board you can read the voltage from it and see how it changes as the amount of light hitting the sensor changes too.

First wire up a photocell to your board as shown on the previous page for Arduino.  You'll want to setup the same voltage divider with a 10 kilo-ohm resistor circuit and feed the output into any analog input on your board (note the special method of reading photocells without an analog input is not currently supported by CircuitPython).

Here's an example of wiring a photocell to a Feather M0:

  • Board 3.3V to one leg of the photocell (doesn't matter which leg).  Note you want to use the voltage from your board that corresponds to the maximum analog input voltage.  For Feather boards this is 3.3V, but for other boards it might be higher or lower--consult your board documentation to be sure.
  • 10 kilo-ohm resistor to the other leg of the photocell.
  • Board GND to the other leg of the 10 kilo-ohm resistor.
  • Board A1 (or any other analog input) to the junction of the photocell & 10 kilo-ohm resistor.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Now import the board and analogio modules that allow you to read an analog input.  Be sure you've read the CircuitPython analog I/O guide for more background on using analog inputs too!

import board
import analogio

Create an analog input for the A1 pin connected to the photocell & resistor junction:

photocell = analogio.AnalogIn(board.A1)

At this point you can read the value property to get a reading of the light seen by the photocell.  Try it:


Try covering the photocell with your hand to block the light it can see and read the value again:


Notice the value changed!  When the sensor sees less light the value is reduced.  The more light the sensor sees, the higher the value.

You might wonder, what's the range of possible values?  It turns out for an analog input in CircuitPython the maximum values range from 0 to 65535 (or the maximum 16-bit unsigned integer value).  If you shine an extremely bright light on the photocell you might see a value near 65k, and if you completely block the sensor you might see a value down near 0.

If you're curious you can also convert this value into a voltage that's higher or lower depending on how much light is hitting the sensor.  Let's make a function to do this:

def analog_voltage(adc):
    return adc.value / 65535 * adc.reference_voltage
volts = analog_voltage(photocell)
print('Photocell voltage: {0}V'.format(volts))

Cool!  Notice the voltage increases up to near 3.3 volts as the light hitting the photocell increases.  If you cover the photocell up and read the voltage you'll see it falls down near 0 volts.

You can use either the raw value or voltage to check how much light is hitting the photocell.  Both will change proportionally to the amount of light hitting the sensor.

Here's a complete program that reads the photocell value and prints both the value and voltage every second.  Save this as on your board and open the serial output to see the printed values.  Try shining light on the sensor or covering it up to see how the value and voltage change!

import time

import board
import analogio

# Initialize analog input connected to photocell.
photocell = analogio.AnalogIn(board.A1)

# Make a function to convert from analog value to voltage.
def analog_voltage(adc):
    return adc.value / 65535 * adc.reference_voltage

# Main loop reads value and voltage every second and prints them out.
while True:
    # Read the value, then the voltage.
    val = photocell.value
    volts = analog_voltage(photocell)
    # Print the values:
    print('Photocell value: {0} voltage: {1}V'.format(val, volts))
    # Delay for a second and repeat!

That's all there is to reading a photocell using an analog input with CircuitPython!

Last updated on 2017-12-08 at 06.36.50 PM Published on 2012-07-29 at 11.58.38 AM