Pico Inventor's Kit Experiment 2 - Using a Light Sensor & Analog Inputs

Experiment 2 from the Inventors Kit for Raspberry Pi Pico, in which we explore how to use a phototransistor and take an analog reading. Included in this resource are code downloads, a description of the experiment and also a video walk-through. This resource provides additional help and is not meant to replace the documentation that ships with the kit.

The Raspberry Pi Pico is a compact microcontroller based on the RP2040 processor - Raspberry Pi’s first in-house designed silicon. The RP2040 on the Pico uses 2 ARM processor cores, and also has 4 programmable IO controllers (PIO). These are like mini processors, and can help the Pico complete more complex tasks than it would otherwise be able to do. The Pico has 28 general purpose IO pins (GPIO), and 3 of these can also be used as Analogue inputs. The Pico can be programmed in several languages. The experiments in these resources are written in the Thonny Editor using MicroPython.

Pico Inventor's Kit Exp' 2 - Using a Light Sensor & Analog Inputs

A phototransistor is an electrical component with unique properties. A phototransistor is a transistor that reacts to light by changing how well it conducts electricity. Particles of light are also known as ‘photons’, hence the name phototransistor. The more light that shines on the phototransistor the more it is able to conduct. In this experiment a phototransistor is used along with a resistor to form a potential divider. When used in this configuration it gives a voltage that changes depending on the light level. A microcontroller such as the Raspberry Pi Pico can read this (analog) voltage allowing a program to react to different light levels. This experiment will explain how to use a phototransistor and take an analog reading.

  • To use a phototransistor as a sensor.
  • To perform an analog reading from the phototransistor via input pin A0.
  • To set a light threshold to decide whether to turn the built in LED on or off.

Video Walk-through:

 

Exp 2 - The Code

All experiments are coded in MicroPython in Thonny. To open in Thonny, copy and past the code below directly into the Thonny editor.

'''
Kitronik Inventor's Kit Exp 2

Turns an LED on and off in response to changing light level.

The Pico has an on-board LED you can control, connected to GP25.

A phototransistor is connected to Analogue-In A0, pulling it up to 3.3V.
As the light level varies the analogue input will vary, with bright light being nearer full scale, and dark being near to 0.
We can then use this to turn on the on-board LED as it gets dark.
'''
import machine

lightSensor = machine.ADC(26) # Setup the analogue (A0) on GP26 with a human-readable name
LED = machine.Pin(25, machine.Pin.OUT) # Setup the onboard LED Pin as an output

lightLevelToSwitchAt = 13000 # Set to about 20 percent of scale - Pico ADC is 0 to 65535

# The loop function runs forever, reading the light level and turning on the LED if it's dark
while True:
  # First read the light value on the analog input
  lightValue = lightSensor.read_u16()
  # Now decide what to do
  if (lightValue > lightLevelToSwitchAt): # Then it is bright, so turn off the LED
    LED.value(0)   # Turn the LED off
  else:
    LED.value(1)   # turn the LED

Inventors Kit Extra Resources:

Each of the ten experiments has been designed to ease you into coding and physical computing for the for the Raspberry Pi Pico. The experiments have been chosen to cover the key concepts of physical computing and they also increase in difficulty as you progress. This resource has been put together to provide additional information for this experiment and has not been designed to replace the booklet. The majority of the information you will need to perform and understand this experiment is contained in the booklet.

If you are new to coding and physical computing, even the least complex examples can be quite challenging. With this in mind, we created walk-through videos for each of the experiments. Our presenter talks you through the circuit in a way that backs up the information given in the booklet but in a style that some might find easier to absorb. As well as having a video walk-through, each page also contains the code. Although it is always good to tackle the code yourself, it can be handy for testing your circuit quickly. The code has been heavily commented as an extra learning resource. To get the most out of the experiment, once you've tested your circuit have a go at coding the experiment from scratch.

Follow the links in the table below:

Exp No. Experiment Name.
1
Digital Inputs & Outputs.
2
Light Sensor & Analog Inputs.
3
Dimming an LED using a potentiometer.
4
Using a transistor to drive a motor.
5
Control a servo with a potentiometer.
6
Setting the tone with a piezo buzzer.
7
Using a seven segment display.
8
Exploring wind power.
9
Capacitor charge circuit.
10
Controlling ZIP LEDs.

Leave a comment

All comments are moderated before being published