Inventors Kit Experiment 2 Full Experiment
In this tutorial you will find Experiment 2 Using an LDR & Analog Inputs from the Kitronik Inventors Kit for the BBC micro:bit. We've included the entire experiment as a free example of the great practical experiments that are contained within the Inventors Kit. Learn how to build the circuit and how to code the micro:bit to control the circuit.
You can complete this experiment with or without the Inventors Kit. If you do not have the Kitronik Inventors Kit you will have to source all of the required components separately, a full list of the things you will need can be found below.
This Experiment Requires:
- 1 x BBC micro:bit.
- 1 x Miniature LDR.
- 1 x 10kΩ Resistor.
- 3 x M/F Jumper Wires (If using the Prototyping System).
- 3 x Crocodile Leads (If not using the Prototyping System).
The Aims Of This Experiment Are:
- To use a Light Dependent Resistor as a sensor.
- To 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 Block Editor:
Below is an example of how to write the code for the experiment in the Microsoft Block Editor.
If you're having difficulty producing your own code for this experiment, we've created the code for you that you can compile, download and place onto your BBC micro:bit. To get the code, follow this link.
To use the code; Follow the link above and select Edit. Press the Compile button and the code will be compiled and will automatically download via your browser. Plug your BBC micro:bit into a USB slot on your computer, your computer will now see the micro:bit as a removable drive. You can drag the downloaded hex file directly onto your micro bit. On some systems the hex file may not show up as being on the micro:bit, but it is there.
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 analog 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. 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.
Experiment 2 additional resources:
The current version of the BBC micro:bit booklet is 1.3. If you have version 1.1 then please see below for corrections made to the current version.
- 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||Using an LDR and 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 or without the BBC micro:bit included. Chose the option that is right for you from the links below.