· Web viewThe LDR and 10kΩ resistor are on left of the board. One leg of the resistor is in the...

4
PROM: Primary Robotics On Micro:bits Adrian Oldknow [email protected] March 2018 Section 5. Using the micro:bit’s input and output pins. A very helpful source of ideas and components for connection to the BBC micro:bit is the Kitronik website . Their `Inventor’s Kit for BBC micro:bit’ describes some simple experiments which are easy to make. We will start with their free tutorial on using an analog Light Dependent Resistor (LDR) as an input . This uses just two cheap small electronic devices: an LDR and a 10 kΩ resistor. So already we have some jargon to decode! Devices like switches and buttons have just a few possible different states – maybe just on or off. These can be represented by numbers like 0 and 1. They are called `digital inputs’. Devices like knobs and temperature sensors can change continuously, varying the voltage sent to the micro:bit. These are `analog inputs’. The photograph shows a micro:bit slotted into something called an `edge connector’, so that each of the segments of the gold-coloured bar at the bottom of the face of the micro:bit is led out onto a different pin. The diagram below the micro: bit shows how the LDR and resistor R1 are to be wired up to these pins. The components are pushed into the holes of what is known as a `bread-board’. Yellow, red and black lead connect the appropriate

Transcript of  · Web viewThe LDR and 10kΩ resistor are on left of the board. One leg of the resistor is in the...

Page 1:  · Web viewThe LDR and 10kΩ resistor are on left of the board. One leg of the resistor is in the top row of holes, shown in blue, which is connected by the black wire to the GND

PROM: Primary Robotics On Micro:bits Adrian Oldknow [email protected] March 2018

Section 5. Using the micro:bit’s input and output pins.

A very helpful source of ideas and components for connection to the BBC micro:bit is the Kitronik website. Their `Inventor’s Kit for BBC micro:bit’ describes some simple experiments which are easy to make. We will start with their free tutorial on using an analog Light Dependent Resistor (LDR) as an input. This uses just two cheap small electronic devices: an LDR and a 10 kΩ resistor. So already we have some jargon to decode! Devices like switches and buttons have just a few possible different states – maybe just on or off. These can be represented by numbers like 0 and 1. They are called `digital inputs’. Devices like knobs and temperature sensors can change continuously, varying the voltage sent to the micro:bit. These are `analog inputs’. The photograph shows a micro:bit slotted into something called an `edge connector’, so that each of the segments of the gold-coloured bar at the bottom of the face of the micro:bit is led out onto a different pin. The diagram below the micro: bit shows how the LDR and resistor R1 are to be wired up to these pins.

The components are pushed into the holes of what is known as a `bread-board’. Yellow, red and black lead connect the appropriate column of holes on the board to the micro:bit’s pins P0, 3V and GND. A resistor is used to ensure that only a small current passes through the LDR so that it doesn’t burn out. Resistance is measured in a unit called the `Ohm’, written as the Greek letter `omega’ Ω. We are using a large resistance of 10,000 Ohms. Resistors have patterns of coloured bands round them which identify their resistance. You don’t need a `prototyping’ kit. You can just use crocodile clips to attach the components to the micro:bit as in the photo above. So that’s the hardware sorted. We just need to write some code to see how the LDR can be used. This uses the `Advanced’ section of the toolbox which has a set of tools called `Pins’.

Page 2:  · Web viewThe LDR and 10kΩ resistor are on left of the board. One leg of the resistor is in the top row of holes, shown in blue, which is connected by the black wire to the GND

First use `Variables’ to create a variable called `light’. Then use `Input’ to create an `on button’ structure. Use `Pins’ to select `analog read’. The value returned will be between 0 and 1023. So if we divide the value by 1000 we will normally get a value between 0 and 9. Running the program on the simulator, you can click in the `Pin0’ area and adjust the level up and down. Then press button `A’ to see the corresponding value. When you download this to a micro:bit you change the light intensity falling on the face of the LDR by shining a torch or covering with your hand.

So now we can add an output in the form of a `light emitting diode’ LED. We want the LED to shine brighter when it is darker, and vice versa. So we just need to create another simple circuit using an LED and a 47Ω resistor attached to pin P1. We can read the light intensity using the LDR on Pin P0, then subtract the result from 1023 and output this value to pin P1 to control the brightness of the LCD.

The LDR and 10kΩ resistor are on left of the board. One leg of the resistor is in the top row of holes, shown in blue, which is connected by the black wire to the GND pin. One leg of the LDR is in the second row of holes, shown in red, which is connected by the red wire to the 3V pin. The other legs are in the same column of holes as the yellow wire which is connected to the P0 pin. So that’s the analog input side of the business.

We have to take a little more care with the LED since its shorter leg needs to be connected to GND, using the blue wire. Its longer leg is connected through the 47Ω resistor to

Page 3:  · Web viewThe LDR and 10kΩ resistor are on left of the board. One leg of the resistor is in the top row of holes, shown in blue, which is connected by the black wire to the GND

pin P1 with the green wire. The circuit is shown in the photo above, and the code is really very simple.

Of course if we wanted to make this into a smart lamp we would do away with the `on button’ and use a `forever’ block instead.

Now we have got a green LED in action, we could make our traffic lights on the bread-board using a red and a yellow LED and two more 47Ω resistors. Here is a rough version of the program. As before, the sequence isn’t quite right, and the pauses need to be changed. For testing purposes I have just used a counted loop for four sequences when button A is pressed. On the simulator, the pins P0, P1 and P2 show a sequence of 0s and 1s as the lights are switched on and off.

The circuit is pretty straightforward. The GND pin is connected to the negative, blue track on the bread-board. The shorter leg of each LED is connected to the blue track.

The longer leg is connected through a 47Ω resistor to pins P0 (green), P1 (yellow) or P2 (red) of the break-out board. Can you fix the program to perform the correct sequence?

We will finish this section with a quick glimpse at controlling a motor. For most applications, such as buggies, the 3V power supply is not sufficient. But we can illustrate the theory with low power versions, such as the £5 Motor & Fan from Microbit Accessories, shown connected to a micro:bit with the supplied crocodile clips.

To test this I have connected the motor to the GND, 3V and P0 connections on the micro:bit. Button A turns the motor on, button B turns it off.

See if you can now make a model of a simple heating system which senses the temperature. If it is too hot it turns the fan on until the temperature is cool enough. If it is too cold it turns on one or more LEDs to simulate a heater, which is turned off when the temperature is warm enough.