Description This is a simple tutorial demonstrating how to use a Kitronik edge connector breakout with the BBC micro:bit. 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. Learn how to: Breakout the BBC micro:bit using a Kitronik Edge Connector. Program your BBC micro:bit to read an external sensor. Control an LED based on light levels. Level of difficulty: Beginner. Parts List In order to complete this tutorial, you will need: 1 x BBC micro:bit. 1 x Edge connector breakout board. 1 x LED (most standard LEDs with a forward voltage no higher than 3V should work). 1 x Miniature LDR (dark resistance 20M, light resistance 5K). 1 x 100K resistor. 1 x 220 Ohm resistor. 1 x USB to Micro USB lead. 2 x AAA battery cage with JST connector. 2 x AAA batteries. 1 x Length of wire. You will also need the following equipment: A computer with a USB port and internet access.
Step-by-step guide to building an edge connector light level detector Step 1 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 Create Code then look for the Microsoft Research blocks banner and click New Project. Pick a name for your project and click create! 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 Initially 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 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 micro:bit s memory. We will then pull data from the pin and store it in that variable. Grab the Set item to block from the variables category. Rename the variable by clicking the dropdown and call it LightLevel. 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 Now 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 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 LightLevel. The brighter the light that shines on the LDR the higher the output will be driven.
Step 6 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 compile* and after a few moments the code should appear as a download in your browser. If you plug your BBC micro:bit 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. The file might not show up on the BBC micro:bit in the file explorer but it is there! Once the file has been transferred (the light on the BBC micro:bit will stop blinking rapidly) remove the BBC micro:bit from your computer. Now power up your BBC micro:bit with the batteries, then press the reset button next to the USB connector. The LED should be on and grow dimmer if you cover the LDR. *If this is the first time you have used the compiler you will be asked to make an account. Ask your teacher for the code required to make an account.
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 analog 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.