micro:bit pins: touch, LED, potentiometer and servo motor

Lesson 12 of 14 · micro:bit

On 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).

Before you start: required prior knowledge – radio communication (SR), functions.
How to learn: build every example in MakeCode and try it in the simulator (or on the device), and solve the tasks at the end on your own first; open the instructions and the solution only after that. The programs are given as blocks (description) and in JavaScript from MakeCode: in the editor click JavaScript, paste the code and go back to Blocks – you will get the same blocks.

Pins and safety

PinPurpose
GNDground (minus) – every circuit is closed through GND
3V3 V power supply for small modules
P0, P1, P2input or output: touch, digital (0/1) and analog (0–1023) signal
small pinsadditional inputs/outputs (partly used by the screen and the buttons)
Caution: a pin gives little current and works at 3 V. An LED is connected through a resistor (e.g. 220 Ω). Motors, pumps and strips with many LEDs are not powered from a pin – they need a separate power source and a module (driver, relay). Never connect 3V directly to GND.

A pin as a button (touch)

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 and analog output: an LED

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 input: potentiometer and soil moisture

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

Servo motor

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

Practice tasks

For every task there is an example, instructions and a solution. The solutions have been checked against the examples in the text.

TASK 01Difficulty: easy

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 light
TASK 02Difficulty: medium

The servo follows the tilt

Let 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°.

Example

Output

micro:bit level – motor at 90°; tilted all the way to the right – 180°
TASK 03Difficulty: medium

Warning: dry soil

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.

Example

Output

dry soil: sad face and the LED on; moist soil: happy face
Common mistakes and tips
  • Every circuit is closed through GND – without a wire to GND neither the LED nor the sensor works.
  • A digital output is 0 or 1, an analog output from 0 to 1023; an analog input also returns 0 to 1023.
  • Do not power motors and pumps from a micro:bit pin – use a driver or a relay and a separate power source.
  • Turn off the power before you change the wires.

Check your knowledge

Open the quiz

Pins and external devices

1. 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°?