Getting Started with MicroPython and the SparkFun Inventor's Kit for micro:bit
Experiment 3: Reading a Photoresistor
Introduction
In Experiment 2, you got to use a potentiometer, which varies resistance based on the twisting of a knob and, in turn, changes the voltage being read by the analog input pin. In this circuit you’ll be using a photoresistor, which changes resistance based on how much light the sensor receives. You will read the light value of the room and have an LED turn on if it is dark and turn off if it is bright. That's right; you are going to build a night-light!
Parts Needed
You will need the following parts:
- 1x Breadboard
- 1x micro:bit
- 1X micro:bit Breakout with Headers
- 1x micro-B USB Cable
- 1x LED
- 1x 100Ω Resistor
- 7x Jumper Wires
- 1x Photoresistor
- 1x 10kΩ Resistor
Didn't Get the SIK for micro:bit?
If you are conducting this experiment and didn't get the Inventor's Kit, we suggest using these parts:
Suggested Reading
Before continuing with this experiment, we recommend you be familiar with the concepts in the following tutorial:
Resistors
April 1, 2013
Voltage Dividers
February 8, 2013
Introducing the Photoresistor
The photoresistor changes its resistance based on the light to which it is exposed. To use this with the micro:bit, you will need to build a voltage divider with a 10k ohm resistor, as shown in the wiring diagram for this experiment. The micro:bit cannot read a change in resistance, only a change in voltage. A voltage divider allows you to translate a change in resistance to a corresponding voltage value.
The voltage divider enables the use of resistance-based sensors like the photoresistor in a voltage-based system. As you explore different sensors, you will find more resistance-based sensors that only have two pins like the photoresistor. To use them with your micro:bit, you will need to build a voltage divider like the one in this experiment. To learn more about resistors in general, check out our tutorial on resistors and also our tutorial on voltage dividers.
Hardware Hookup
Ready to start hooking everything up? Check out the wiring diagram below to see how everything is connected.
Polarized Components | Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction. |
Wiring Diagram for the Experiment
Running Your Script
Let's read a photoresistor and turn on an LED when it reaches a certain value. Type (or copy) the program into your Mu editor, or download all the programs from this GitHub Repository and open the Ex3_readLDR.py program. Save it, then click the Flash icon to program your micro:bit.
language:python
# SparkFun Electronics
# Experiment 3.0
# Reading a Photoresistor
from microbit import *
calibrationVal = pin0.read_analog()
sleep(1000)
while True:
lightVal = pin0.read_analog()
if lightVal < calibrationVal-50:
pin16.write_digital(1)
else:
pin16.write_digital(0)
Code to Note
if and else
If the light value variable that is constantly being updated in the forever loop is less than the calibration value minus 50, it is dark and the LED should turn on. The (-50) portion of the if
statement is a sensitivity value. The higher the value, the less sensitive the circuit will be; the lower the value, the more sensitive it will be to lighting conditions.
The if
and else
statements are a simple way to set the control flow of your forever loop. If the logical statement tied to the if statement is True
, then it will execute the code indented under the colon. If that statement is False
, it will skip to the next statement, which is else. In this case, if the statement is True
(the room is dark), then the micro:bit will turn on the LED on pin 16; else (if the room is bright), it will turn the LED off using a write_digital()
command covered earlier.
calibrationval
is a calibration variable. Your micro:bit takes a single reading of the light sensor before entering the forever loop and uses this value to compare against the lightVal
variable in the forever loop. This value doesn't change in the forever loop, as it is set before entering the forver loop. To update this value, you can press the RESET button on the back of your micro:bit or power cycle the board.
What You Should See
When the micro:bit runs the program, it will take a single reading from the light sensor and use that as a calibration value of the "normal" state of the room. When you place your hand over the light sensor or turn the lights off, the LED will turn on. If you turn the lights back on or uncover the light sensor, the LED will turn off.
Troubleshooting
LED Remains Dark
You may have been leaning over the light sensor when the code started. Make sure the light sensor is reading the normal light in the room at startup. Try resetting the micro:bit.
Still Not Quite Working
Double-check your wiring of the signal pin; sometimes you miss a breadboard connection by a row.