Traffic light
Three LEDs (red on P0, yellow on P1, green on P2) work as a traffic light: red 3 s, red and yellow 1 s, green 3 s, yellow 1 s – and again.
Example
Output
the LEDs change like on a real traffic lightOn the bottom edge of the micro:bit there are gold contacts – pins. Through them the micro:bit "talks" to the outside world: it reads touch and sensors, turns on LEDs and moves a servo motor. The large pins 0, 1, 2, 3V and GND have holes for crocodile clips. The blocks for pins are in the Pins palette (under Advanced).
| Pin | Purpose |
|---|---|
| GND | ground (minus) – every circuit is closed through GND |
| 3V | 3 V power supply for small modules |
| P0, P1, P2 | input or output: touch, digital (0/1) and analog (0–1023) signal |
| small pins | additional inputs/outputs (partly used by the screen and the buttons) |
The block on pin P0 pressed (Input palette) reacts when the circuit between the pin and GND is closed – for example when you hold GND with one hand and touch P0 with the other, or when two pieces of foil touch, as in the project "Reaction speed".
let touches = 0
input.onPinPressed(TouchPin.P0, function () {
touches += 1 // we count the touches of pin P0
basic.showNumber(touches)
})
This is how to make a "fruit piano": connect three bananas to P0, P1 and P2 with crocodile clips, and each one plays a different tone with the block play tone.
digital write pin P1 to 1 turns on an LED connected to P1 (1 = voltage, 0 = no voltage). analog write pin P1 to 512 gives "half power" – a value from 0 to 1023 determines the brightness.
// LED (with a resistor) between P1 and GND
basic.forever(function () {
pins.digitalWritePin(DigitalPin.P1, 1) // turn on
basic.pause(500)
pins.digitalWritePin(DigitalPin.P1, 0) // turn off
basic.pause(500)
})
Slowly turning the LED on and off (the "breathing" effect) is done with an analog output in a loop:
basic.forever(function () {
for (let brightness = 0; brightness <= 1023; brightness += 32) {
pins.analogWritePin(AnalogPin.P1, brightness) // brighter and brighter
basic.pause(20)
}
for (let brightness = 1023; brightness >= 0; brightness -= 32) {
pins.analogWritePin(AnalogPin.P1, brightness) // dimmer and dimmer
basic.pause(20)
}
})
analog read pin P2 reads the voltage on the pin and returns a number from 0 (0 V) to 1023 (3 V). This is how a potentiometer (rotary knob), a soil moisture sensor, a light sensor and similar modules are read.
// potentiometer: the ends on 3V and GND, the middle leg on P2
let value = 0
basic.forever(function () {
value = pins.analogReadPin(AnalogPin.P2) // 0 .. 1023
led.plotBarGraph(value, 1023) // graph on the screen
pins.analogWritePin(AnalogPin.P1, value) // the same brightness for the LED on P1
})
This program is a dimmer: turning the potentiometer changes the brightness of the LED. With a soil moisture sensor the same block gives a number that is larger the wetter the soil is – the basis of the project "Watering the garden".
A servo motor turns to a given angle from 0° to 180°. It has three wires: brown/black to GND, red to power, orange/yellow (signal) to a pin. The block is servo write pin P0 to 90.
// button A: 0 degrees, button B: 180 degrees, A+B: middle
input.onButtonPressed(Button.A, function () {
pins.servoWritePin(AnalogPin.P0, 0)
})
input.onButtonPressed(Button.B, function () {
pins.servoWritePin(AnalogPin.P0, 180)
})
input.onButtonPressed(Button.AB, function () {
pins.servoWritePin(AnalogPin.P0, 90)
})
A small servo (SG90) usually works with the micro:bit's battery power too, but if the micro:bit resets when the motor moves – the motor needs a separate power source (with a common GND).
For every task there is an example, instructions and a solution. The solutions have been checked against the examples in the text.
Three LEDs (red on P0, yellow on P1, green on P2) work as a traffic light: red 3 s, red and yellow 1 s, green 3 s, yellow 1 s – and again.
the LEDs change like on a real traffic lightLet the servo motor on P0 turn as you tilt the micro:bit left and right: a tilt of −90° gives a motor angle of 0°, the level position 90°, and a tilt of 90° gives 180°.
micro:bit level – motor at 90°; tilted all the way to the right – 180°The soil moisture sensor is on P1. If the value read is less than 300, the soil is dry: show a sad face and turn on the LED on P2. Otherwise show a happy face and turn off the LED. Repeat the measurement every 2 seconds.
dry soil: sad face and the LED on; moist soil: happy face1. What does analog read pin return?
2. When is on pin P0 pressed executed?
3. Why is a resistor used with an LED?
4. Which block turns a servo motor to 90°?