Light-dependent Resistor with micro:bit
Light-dependent resistors are electronic components whose resistance changes with light intensity. They are also called LDRs, photoresistors, or photoconductors.
In this guide, you will learn to connect a light-dependent resistor to the micro:bit, and measure the relative brightness of the environment.
Complete this guide to learn how to use a light-dependent resistor. They are used in all sorts of light-sensing circuits; For instance, they could be used as a sensor in cameras or automatic lights that come on when it gets dark enough.
|Parts Used in This Guide|
Step 1 The module
Before we begin, let's take a closer look at the light-dependent resistor module. It has four pins:
- 3.3V : While 'VCC' stands for Voltage Common Collector, we'll connect the VCC pin to 3.3V on the micro:bit
- GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called ground or GND.
- Note: Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to.
- DO: Digital Output
- AO: Analog Output
Step 2 Create the variables!
Open up MakeCode editor and start a new project.
- First, create two variables: 'sensorVal' and 'threshold'.
- As the AO pin of the light-dependent resistor is connected to Pin 1 of the micro:bit, we set sensorVal (short for sensor value) to analog read pin P1.
The third variable, "threshold" is set to 500.
- Next, add a 'show number' block and change its value to 'sensorVal'. Upload this code now, and test it. What values show across the screen of your micro:bit's 5X5 LED matrix?
Step 3 The loop!
- The next step is to create an 'if... then ... else if ... then' loop. So drag and drop that block from the 'Logic' tab into the editor.
- To add an 'else if' block, click on the white plus (with a circular border) icon twice.
- Delete the extra 'else' block by clicking on the white minus (with a circular border) icon next to it.
- We will set two conditions here.
- If sensorVal > threshold, then use a 'show icon' block with the following pattern.
- The second condition is 'sensorVal <= threshold', use another 'show icon' block with a different pattern.
Step 4 Upload the hex file
It's time to upload the code to the micro:bit!
- Connect your computer to the micro:bit by using a microUSB cable
- Click on the 'Download' button on the bottom left corner of the MakeCode editor
- The hex file will be downloaded to your 'Downloads' folder. So open up Finder on a Mac OSX, or in Explorer on Windows and go to your Downloads folder.
- Drag and drop the downloaded hex file to the 'MICRO:BIT' drive
You may want to adjust the threshold level to your liking. Then, test it by covering the light-dependent resistor with your hands. The LED will light up!
Step 5 Next Steps
Now that you've gained some familiarity with using the light-dependent resistor, why not use other components with it? You could create a light-sensitive alarm with the micro:bit, light-dependent resistor and a buzzer module.