Christmas Information: Kitronik will be closing 24th Dec December 2024, reopening on 2nd of January 2025. See our delivery schedule for more information.

BBC micro:bit Edge Connector Light Level Detector featured image
BBC micro:bit - Kitronik University This Kitronik University resource is part of the BBC micro:bit partnership and is an in depth tutorial into making an edge connector light level detector with the BBC micro:bit.

microbit kitronik_microbit_logo_870

This is a simple tutorial demonstrating how to use a Kitronik edge connector breakout with the BBC microbit. The tutorial will cover measuring ambient light levels with an LDR and dimming an LED correspondingly. In this example we will use the prototyping space on the breakout board with an LDR, two resistors and an LED.

BBC microbit Edge Connector Light Level Detector:

microbit bbc_microbit_light_level_detector_01_560

Learn how to:

  • Program your BBC micro:bit to read an external sensor.
  • Control an LED based on light levels.
  • Breakout the BBC microbit using a Kitronik Edge Connector

Level of difficulty:

  • Beginner.

Parts List

In order to make your BBC micro:bit Edge Connector Light Level Detector you will need:

You will also require the following equipment:

  • A computer with a USB port and internet access.

Step by Step Guide to making your "BBC micro:bit Light Level Detector"

Step 1

microbit etextiles emoji bag

First this tutorial will cover the code for the dimmer; the later steps will cover the hardware side of things. Go to the BBC micro:bit website, click "Let's Code" then open the MakeCode Editor by clicking the button. Let's practice using the block system to build some code. The blocks are organised into categories and the categories are listed in a column down the left hand side of the screen.

Step 2

First we need to create a "forever" loop. This is the block that the rest of our code will sit inside. The "forever" loop runs all the blocks inside it starting from the top and working its way to the bottom. Once it gets to the bottom block it starts again at the top and this process will continue forever. Click on the "Basic" category to open it and then drag the "Forever" block into the workspace. (This is the big white area in the middle of the screen). The forever block is useful as it runs the program again from the beginning once all of the steps have been completed.

Step 3

edge-connector-microbit-light-level-detector-variable-1000

This will require us to store and use data from a pin on the BBC micro:bit. To do this first we will create a variable, this is a location where we can store data in the BBC microbit's memory. We will then pull data from the pin and store it in that variable. Click on Variables, then click the Make a Variable button. Choose a variable name, we chose "light level". We will use this variable to store data from the LDR. The data stored will be a number between 0 and 1024 as this is how the BBC micro:bit sees analogue data. Drop this block into the forever block you just placed and you should find it snaps into position.

Step 4

edge-connector-microbit-light-level-detector-read-1000

Now that we have a variable ready to store temperature information we need to get the information from the sensor and put it into that variable. The way to do this is to perform an "analog read". Attach an "analog read pin" block to the variable you just made. The “analog read pin” block is used to read data from a pin that you can select by clicking the drop-down menu to the right hand side of the block. These blocks, when combined, will read the value from the selected pin and set the variable you created to that value. In this tutorial, we will read from pin P2.

Step 5

edge-connector-microbit-light-level-detector-write-1000

Now we need to do something with the information gathered from the "analog read". Grab an "analog write" block and clip it underneath the first block. Set the pin to P1. Replace the number block from the "analog write" block with our variable "Light Level". The brighter the light that shines on the LDR the higher the output will be driven.

Step 6

microbit bbc_microbit_light_level_detector_01_560

Now would be a good time to put what we’ve just done to the test. First, however, we need to set up the hardware. Take your 100K resistor and solder one leg to the negative strip in the prototyping area (marked with "-") on the edge connector breakout. Solder the other end onto the centre strip at the edge of the board. Next solder the LDR. Put one leg through the positive strip (marked "+"). The other leg should go into the centre strip at the edge of the board. Take a short piece of wire and connect the centre strip at the edge of the board to the pin labelled "2". Next, take your current limit resistor and solder one end into the pin labelled "1". The other end of the resistor should be soldered into the left hand strip at the edge of the board. Finally, take your LED and solder the long positive leg into the left hand strip at the edge of the board, and the short negative leg into the negative strip (marked with "-").

Step 7

Now, let's try that out! Press Download and after a few moments the code should appear as a download in your browser. If you plug your BBC microbit into a USB port it will show up as a storage device. Simply drag and drop the .hex file you just downloaded onto the BBC micro:bit. Once the file has been transferred, the light on the BBC micro:bit will stop blinking rapidly.

Further Tasks

If we wanted to invert the function of the light sensor board we have built so that the LED lights up when the LDR is covered, we can do this in the code. The analogue value read from pin P2 will be between 0 and 1024, so if we want to reverse the reading we can subtract it from 1024. So a reading of 0 will become 1024 and a reading of 1024 will become 0.   Download a pdf version of this page here. pdf_icon.gif

4 comments

Mark Donnison

Mark Donnison

The 220ohm resistor can be found in the following pack: https://www.kitronik.co.uk/c3003-resistor-pack-of-100.html The Kitronik Inventors Kit doesn't have a resistor of that value in it. I hope this helps.

John

John

Can you provide a link to the 1 × 220 Ohm resistor? Is it in the inventor kit? Thanks

Rob Haywood

Rob Haywood

Thanks Steve, that's corrected now. Rob

Steve Smythe

Steve Smythe

You talk about a “thermistor” in Step 3. I think you've muddled up the instructions for another project that measures temperature!

Leave a comment

All comments are moderated before being published