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 microbit. 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.

experiment-2-block-intro-870

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:

 

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. experiment-2-block-editor-1-870
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.

Experiment 2 Using an LDR & Analog Inputs-breadboard-870

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.

experiment-2-block-crocodile-leads-870

Circuit Diagram:

Experiment 2 Using an LDR & Analog Inputs-circuit-diagram-870

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:

 

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. * - To place the unzipped HEX file onto your microbit, connect your microbit to your computer via USB and then open File Explorer (Windows). Your microbit should show up as a removable drive. Simply drag and drop your HEX file onto the microbit in file explorer and the file will be transferred. The power light on the back of the microbit will start to flash, once the flashing stops the microbit is ready to run your code.  

Booklet Errata:

 

inventors-kit-experiment-2-errata 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.

The booklet has now been updated to Version 1.5 with all code examples being produced and shown in the Microsoft PXT editor. The image below shows the correct block to use when coding this experiment using the PXT Editor.inventors-kit-experiment-2-errata-2
 

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.

Project shopping list

For this project you will need the following items, you can adjust the quantities (including setting them to zero if you don't need the item), before pressing the "Buy all items" button.

Kitronik Inventor's Kit for the BBC micro:bit
£21.00
Qty
BBC micro:bit Starter Kit
£12.85
Qty

8 comments

Mark Donnison

Mark Donnison

Hi Oscar, at this time we only ship the booklet with the Inventors kit, though we do have several of the experiments online in their entirety. For example, the information on this page is the same as the information in the booklet.

Oscar Moerman

Oscar Moerman

Is there still no PDF of the Inventor's Kit tutorial book?

Mark Donnison

Mark Donnison

Hi Dave, There is an embedded editor with the code for the experiment on the page. At the foot of the editor is a download link and in the top right of the editor is an edit link that will open the MakeCode editor in a new browser tab that will have the code in it. I hope this helps.

dave

dave

I could not complete the code. Is there any additional help available?.I was unsure what order the blocks should be added and I could not find some of the shapes of blocks that were required.

Mark Donnison

Mark Donnison

Hi Chris, not at the moment no.

chris

chris

is there a pdf version of the inventor's kit tutorial book available? Thanks

Mark Donnison

Mark Donnison

Hi Mark, Sorry you are having difficulties. If you drop our admin team an email they can help you get to the bottom of the problem and hopefully help you resolve it. Use the following email address: sales@kitronik.co.uk

Mark Ruskell

Mark Ruskell

I couldn't get the second experiment to work despite following all instructions could the LDR be faulty?

Leave a comment

All comments are moderated before being published