This Experiment Requires:
- BBC micro:bit. x 1
- Miniature LDR. x 1
- 10kΩ Resistor. x 1
- M/F Jumper Wires (If using the Prototyping System). X 3
- Crocodile Leads (If not using the Prototyping System). X 3
The Aims Of This Experiment Are:
- To use a Light Dependent Resistor as a sensor.
- Perform an analog reading from the Light Dependent Resistor via input pin P0.
- To set a light threshold to decide whether to display a sun or a moon on the LED matrix.
Experiment 2 Video Walk Through:
Experiment 2 - Using an LDR & Analog Inputs:An LDR (Light Dependent Resistor) is an electrical component with unique properties. As the name suggests, it is a type of resistor that has its resistance determined by how much light is shining on it. The brighter the light, the less resistance it has. These resistors are used along with normal resistors to form potential dividers. When used in this configuration it gives a voltage that changes depending on the light level. A micro controller such as the BBC micro:bit can read this (analog) voltage, allowing a program to react to different light levels. This experiment will explain how to use the LDR and take an analog reading.
Creating The Code Using The Microsoft MakeCode Editor:Below is an example of how to write the code for the experiment in the Microsoft MakeCode Editor. If you're having difficulty producing your own code for this experiment, we've created the code for you that you can download directly from the embedded editor above. You will also find the links for the code towards the end of this article.
Building This Circuit On The Prototyping System:It is possible to build this circuit using the Prototyping System for the BBC micro:bit and the components listed at the top of the page, you can follow the diagram below.
What Will Happen:The BBC micro:bit should display a sun image. If you cover the LDR with your finger it should then show a moon image. When the LDR is in light the BBC micro:bit should display the 'sun' image. When the LDR is in the dark (by covering it) the BBC micro:bit should display the 'moon' image.
Building This Circuit With Crocodile Leads:If you don't have the Kitronik Inventors Kit for the BBC micro:bit or the Prototyping System you can still build this circuit using the circuit diagram for reference and Crocodile Leads to join the components together. You can see an example of this in the picture below.
What's Going On (How does it work?):The resistor R1 and LDR are in series so they divide the 3V. The proportion of the 3V across each of them depends on their relative resistance. If they both have equal resistance then the voltage in the middle will be half. If the resistor makes up 75% of the total resistance then the voltage in the middle will be 75% of the 3V and so on. As more light shines on the LDR its resistance goes down, meaning resistor R1 makes up a greater percentage of the total resistance. This increases the voltage on P0. The analogue read function measures the voltage on the selected pin and converts it into a value between 0 and 1023. If the voltage on P0 is 0V then this value will be 0. Then If the voltage on this pin is the maximum of 3V then the value will be 1023. If the voltage on P0 is 1.5V then the value will be approximately 512. The pin P0 is read and the value is stored in the variable 'light'. The 'if' statement then checks if the value stored in 'light' is greater than 512. If it is then the LED's in the pattern of a sun is displayed on the LED matrix, otherwise, the moon pattern is displayed.
Code Downloads:The code examples below have been individually zipped and can be downloaded by clicking on your preferred option. Once unzipped you can either open the and edit the code in appropriate editor or place the HEX file onto your microbit*.
MakeCode Editor & Python Code Downloads:
- This code was created with the MakeCode Editor, download the HEX file here.
- And this code was created with the Python Editor, download the HEX file here.
Legacy Editors Code Downloads:NOTE: The legacy editors will disappear at some undisclosed point in the future. The code can still be placed on a microbit and will run regardless but you won't be able to use an editor. When that happens, switch to using one of the currently supported editors. We've already made the switch as the new editors are better and more fully featured.
- This code was created with the Blocks Editor, download the HEX file here.
- And this code was created with the Touch Develop Editor, download the HEX file here.
- On page 19 there is a box describing which blocks you need for this experiment.
- We incorrectly listed an IF DO ELSE block. This should be an IF DO block.
- You then need to click the cog icon to change the 'if do' block into an 'if do else' block by dragging the 'else' into the 'if' loop as shown above.
Kitronik Inventors Kit Resources:
|Exp No#.||Experiment Name.||Resource Type.|
|1||Say Hello to the BBC micro:bit.||Further Help.|
|2 Pre V1.7||Using an LDR and analog inputs.||Full Experiment + Further Help.|
|2 V1.7||Using a Light Sensor & analog inputs.||Full Experiment + Further Help.|
|3||Dimming an LED using a potentiometer.||Further Help.|
|4||Using a transistor to drive a motor.||Full Experiment + Further Help.|
|5||Using the accelerometer to control motor speed.||Further Help.|
|6||Setting the tone with a piezo buzzer.||Further Help.|
|7||Wind Power.||Full Experiment + Further Help.|
|8||Making a game using the compass.||Further Help.|
|9||Capacitor charge circuit.||Further Help.|
|10||Using an RGB LED.||Further Help.|
|11||Making a pedestrian crossing.||Full Experiment + Further Help.|
|12||Making a random dice.||Full Experiment + Further Help.|
Get The Kitronik Inventors Kit For The BBC microbit:We do two versions of the Inventors Kit for the BBC micro:bit, with the BBC micro:bit included and also without. Chose the option that is right for you from the links below. [product id="1696"] [product id="1740"]
©Kitronik Ltd – You may print this page & link to it, but must not copy the page or part thereof without Kitronik's prior written consent.