← Micro:bit

Chapter 4 · Book pp. 45–74

Connecting the Outside World

ตัวอย่างการติดต่ออุปกรณ์ภายนอกของ Micro:bit ผ่านพอร์ตอินพุตเอาต์พุต

So far, the code has stayed on the board. Now the pins let it reach the world. An input (อินพุต) brings a signal in, such as a switch or sensor. An output (เอาต์พุต) sends a signal out, such as an LED or speaker.

The pins ขาพอร์ตอินพุตเอาต์พุต

The edge connector has 80 contacts, 40 on each side, and the back ones are not used. Some pins are free, while others are already busy with the display or the sensors.

🔍 Try it: click a pin

  • Free to use
  • Shared with the display or a button
  • Bus pins
  • Power

Click a pin to see what it does.

Digital and analog

Digital (ดิจิตอล)Analog (อะนาลอก)
ValuesOnly 0 or 10 to 1023 on input
LikeA light switchA volume knob
Blockdigital write pinanalog read pin

On the AX-microBIT+, seven pins have JST sockets: P0, P1, P8, P12, P16, SDA and SCL.

Next: music →

4.1 Make music สร้างเสียงดนตรี

🧪 Lab 4.1 · London Bridge

  1. Build the code below. play tone is in the Music group.
  2. Download it to the Micro:bit.
  3. Set the P0 jumper to PIEZO.
  4. Press button A on the Micro:bit or the activity board.
AX-microBIT+ P0 jumper: PIEZO P0 Piezo on the board nothing to plug in
Lab 4.1 setup. The three wires are power (red), signal (yellow) and ground (grey).

💻 Lab 4.1: London Bridge

forever
if button A is pressed then
play tone Middle G for 1 beat
play tone Middle A for 1 beat
play tone Middle G for 1 beat
… 20 more notes, in the order below …
play tone Middle C for 2 beat
See it in JavaScript
basic.forever(function () {
	if (input.buttonIsPressed(Button.A)) {
		music.playTone(Note.G, music.beat(BeatFraction.Whole))
		music.playTone(Note.A, music.beat(BeatFraction.Whole))
		music.playTone(Note.G, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Whole))
		music.playTone(Note.E, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Whole))
		music.playTone(Note.G, music.beat(BeatFraction.Double))
		music.playTone(Note.D, music.beat(BeatFraction.Whole))
		music.playTone(Note.E, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Double))
		music.playTone(Note.E, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Whole))
		music.playTone(Note.G, music.beat(BeatFraction.Double))
		music.playTone(Note.G, music.beat(BeatFraction.Whole))
		music.playTone(Note.A, music.beat(BeatFraction.Whole))
		music.playTone(Note.G, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Whole))
		music.playTone(Note.E, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Whole))
		music.playTone(Note.G, music.beat(BeatFraction.Double))
		music.playTone(Note.D, music.beat(BeatFraction.Double))
		music.playTone(Note.G, music.beat(BeatFraction.Double))
		music.playTone(Note.E, music.beat(BeatFraction.Double))
		music.playTone(Note.C, music.beat(BeatFraction.Double))
	}
})
The 24 notes in order, all in the Middle octave. A number is the length in beats.
G1 A1 G1 F1 E1 F1 G2D1 E1 F2E1 F1 G2
G1 A1 G1 F1 E1 F1 G2D2 G2E2 C2

On a V2 board the tune also plays through the built-in speaker, even without the activity board.

Next: LEDs and switches →

4.2 Control an LED with switches ควบคุมการเปิดปิด LED

These labs use the ZX-LED, a small board with an 8 mm LED that lights on logic 1. Plug it into P16.

How the two small boards behave
BoardSignalResult
ZX-LED (output)Logic 1 from the Micro:bitLED on
ZX-LED (output)Logic 0 from the Micro:bitLED off
ZX-SWITCH01 (input)Pressed: sends logic 0 (0 V)Red light on
ZX-SWITCH01 (input)Released: sends logic 1 (3.3 V)Red light off

Lab 4.2.1: buttons A and B

AX-microBIT+ P16 ZX-LED
Lab 4.2.1 wiring. The three wires are power (red), signal (yellow) and ground (grey).

🧪 Lab 4.2.1 · Steps

  1. Build the code, then download it to the Micro:bit.
  2. Plug the ZX-LED into P16.
  3. Press A and the LED lights. Press B and it goes out.

💻 Lab 4.2.1: buttons A and B

forever
if button A is pressed then
digital write pin P16 to 1
if button B is pressed then
digital write pin P16 to 0
See it in JavaScript
basic.forever(function () {
	if (input.buttonIsPressed(Button.A)) {
		pins.digitalWritePin(DigitalPin.P16, 1)
	}
	if (input.buttonIsPressed(Button.B)) {
		pins.digitalWritePin(DigitalPin.P16, 0)
	}
})

Lab 4.2.2: external switches

The ZX-SWITCH01 board sends logic 0 (0 V) when pressed, and its red light comes on. Plug one into P0 and one into P1. Set the P0 jumper to Port0.

AX-microBIT+ P0 jumper: Port0 P16 ZX-LED P0 ZX-SWITCH01 turns the LED off P1 ZX-SWITCH01 turns the LED on
Lab 4.2.2 wiring. The three wires are power (red), signal (yellow) and ground (grey).

💻 Lab 4.2.2: external switches

forever
if pin P1 is pressed then
digital write pin P16 to 1
if pin P0 is pressed then
digital write pin P16 to 0
See it in JavaScript
basic.forever(function () {
	if (input.pinIsPressed(TouchPin.P1)) {
		pins.digitalWritePin(DigitalPin.P16, 1)
	}
	if (input.pinIsPressed(TouchPin.P0)) {
		pins.digitalWritePin(DigitalPin.P16, 0)
	}
})

✏️ Book correction

The book’s explanation has these two switches swapped. Its code and its test results agree: P1 turns the LED on, and P0 turns it off.

Next: knob and light →

4.2 Control an LED with a knob and light

Lab 4.2.3: the knob on P2

analog read pin turns the knob’s voltage into a number. Under 500 the LED is off, and from 500 upwards it is on. Move the slider, and wait for the number to finish scrolling.

AX-microBIT+ Knob: on board, P2 P16 ZX-LED
Lab 4.2.3 wiring. The three wires are power (red), signal (yellow) and ground (grey).

💻 Lab 4.2.3: the knob on P2

forever
show number analog read pin P2
if analog read pin P2 < 500 then
digital write pin P16 to 0
else
digital write pin P16 to 1
See it in JavaScript
basic.forever(function () {
	basic.showNumber(pins.analogReadPin(AnalogPin.P2))
	if (pins.analogReadPin(AnalogPin.P2) < 500) {
		pins.digitalWritePin(DigitalPin.P16, 0)
	} else {
		pins.digitalWritePin(DigitalPin.P16, 1)
	}
})

Lab 4.2.4: light, a twilight switch

The ZX-LDR measures light. Connect its + output to P1, so more light gives a bigger number. This code turns the LED on when it gets dark, like a street light (สวิตช์สนธยา).

AX-microBIT+ P16 ZX-LED P1 ZX-LDR use the + output
Lab 4.2.4 wiring. The three wires are power (red), signal (yellow) and ground (grey).

🔦 The ZX-LDR has two outputs

On the + output, more light gives a higher voltage. On the other output, more light gives a lower voltage.

💻 Lab 4.2.4: light on P1

forever
show number analog read pin P1
if analog read pin P1 > 500 then
digital write pin P16 to 0
else
digital write pin P16 to 1
See it in JavaScript
basic.forever(function () {
	basic.showNumber(pins.analogReadPin(AnalogPin.P1))
	if (pins.analogReadPin(AnalogPin.P1) > 500) {
		pins.digitalWritePin(DigitalPin.P16, 0)
	} else {
		pins.digitalWritePin(DigitalPin.P16, 1)
	}
})

✏️ Book correction

Parts of the book’s text say the opposite. Its code is right: over 500 (bright) turns the LED off. Lab 4.2.3 also has a typing error, saying “off” where it means “on”.

Why is it slow?

Code runs one block at a time, and a scrolling number takes a few seconds. While it scrolls, the if block is waiting. Delete show number and compare. Use show number while testing, then remove it when you know the code works.

💻 Lab 4.2.4 without show number

forever
if analog read pin P1 > 500 then
digital write pin P16 to 0
else
digital write pin P16 to 1
See it in JavaScript
basic.forever(function () {
	if (pins.analogReadPin(AnalogPin.P1) > 500) {
		pins.digitalWritePin(DigitalPin.P16, 0)
	} else {
		pins.digitalWritePin(DigitalPin.P16, 1)
	}
})

Next: the clap switch →

Lab 4.2.5: clap switch

The ZX-SOUND board gives about 0 V when quiet, and up to 3.3 V on a loud sound. Connect it to P1 and the ZX-LED to P16.

A variable (ตัวแปร) remembers something. Here LED_Status remembers whether the light should be on. It is a Boolean, so it is only ever true or false. Each clap flips it with not.

AX-microBIT+ P16 ZX-LED P1 ZX-SOUND
Lab 4.2.5 wiring. The three wires are power (red), signal (yellow) and ground (grey).

🧪 Lab 4.2.5 · Build it block by block

  1. Add on start. Inside it, put set variable to 0 from Variables.
  2. Make a new variable called LED_Status and select it in that block.
  3. From Logic, drop false into the block, so the LED starts in the off state.
  4. Add forever, and put an if … then inside it.
  5. In the condition, put a comparison from Logic. On its left, put analog read pin set to P1.
  6. Change the comparison to > and the number to 50.
  7. Inside then, add set LED_Status. Fill it with not from Logic, holding the LED_Status variable.
  8. Below that, add an if … then … else that checks LED_Status = true.
  9. Under then, put digital write pin P16 to 1. Under else, put the same block set to 0.
  10. After the outer if, add pause (ms) 100.
  11. Download it, connect the parts, then clap near the microphone. Clap again to turn the LED off.

💻 Lab 4.2.5: clap switch

on start
set LED_Status to false
forever
if analog read pin P1 > 50 then
set LED_Status to not LED_Status
if LED_Status = true then
digital write pin P16 to 1
else
digital write pin P16 to 0
pause (ms) 100
See it in JavaScript
let LED_Status = false
basic.forever(function () {
	if (pins.analogReadPin(AnalogPin.P1) > 50) {
		LED_Status = !(LED_Status)
		if (LED_Status == true) {
			pins.digitalWritePin(DigitalPin.P16, 1)
		} else {
			pins.digitalWritePin(DigitalPin.P16, 0)
		}
	}
	basic.pause(100)
})

🔎 Think about it

Press and hold the button in the simulator. The light flickers, because the code flips the variable every 100 ms for as long as the sound lasts. How could you fix this? One idea is a longer pause after each flip.

Next: the thermometer →

4.3 Micro:bit thermometer เทอร์โมมิเตอร์

The MCP9701 temperature sensor sits in a metal probe, so it can go into water. Its output voltage rises steadily with temperature, and we turn that into degrees with a formula.

Temp = (adc × 0.16) − 20, where adc is the analog read value.

🧮 Try it: from analog reading to temperature

0.40 V at 0 °C 2.84 V at 125 °C Temperature (°C) Volts
MCP9701: the output rises in a straight line, 19.5 mV for each °C. Drawn from the datasheet values.
MCP9701 at a glance
RangeUp to +125 °C, typically within 2 °C
Supply3.1 to 5.5 V, using only 6 µA
Output change19.5 mV for each °C
ResponseWithin about 2 seconds

🧪 Lab 4.3 · Build the thermometer

  1. Create the variables val and temp in Variables.
  2. In forever, add set val to with analog read pin P1 inside it. That block is under Advanced › Pins.
  3. Add set temp to. From Math, build the sum one block at a time: val × 16, then that ÷ 100, then that − 20.
  4. Wrap the sum in round, and add show number holding temp.
  5. Download it, then plug the probe into P1.
  6. Warm the probe in your hand and watch the number change.
AX-microBIT+ P1 MCP9701 probe
Lab 4.3 wiring. The three wires are power (red), signal (yellow) and ground (grey).

💻 Lab 4.3: thermometer (simplified)

forever
set val to analog read pin P1
set temp to round( val × 16 ÷ 100 − 20 )
show number temp
See it in JavaScript
let val = 0
let temp = 0
basic.forever(function () {
	val = pins.analogReadPin(AnalogPin.P1)
	temp = Math.round(val * 16 / 100 - 20)
	basic.showNumber(temp)
})

✏️ Changes from the book

  • The book sets val to 0, then adds the reading. We simply set val to the reading.
  • Today’s MakeCode can show decimals, so we added round to keep a whole number.
  • The book lists 500 mV at 0 °C. Microchip’s datasheet gives 400 mV for the MCP9701, and that is the value the formula fits.

Next: cables →

4.4 Cables and parts อุปกรณ์เสริม

PartWhat it isUse
JST3AA-88-inch, 3-wire cable with a 2 mm JST plug at each endBoard to INEX sensor or output
JST3AF-88-inch, 3-wire cable from JST to 2.54 mm IDC socketsBoard to parts with pin headers
IDC1MF / IDC1FF10 cm jumper wires, 5 of each, male-female and female-femaleHeaders to a breadboard or part
microB-USB1.5 m data cableComputer to Micro:bit
DC adapter+5 V, up to 2 A, with a barrel plugPower for the activity board

The three JST wires are G (ground), S (signal) and V (power).

Next: review →

Chapter summary

  • Free pins are P0, P1, P2, P8, P12 and P16. Others share work with the display, buttons or sensors.
  • Digital is 0 or 1. Analog input is 0 to 1023.
  • digital write sends a signal out, and analog read measures one coming in.
  • An if block compares a sensor value to a threshold and decides what to do.
  • A variable remembers something, and a formula turns a reading into real units.
  • Slow blocks such as show number delay everything after them.

🛠️ Activity · Build your own

  1. Change the threshold in the light lab. Find the value that suits your classroom.
  2. Program a different tune, using the same blocks as London Bridge.
  3. Fix the clap switch, so that one long clap only flips the light once.
  4. Challenge: make the thermometer light the ZX-LED above 30 °C, as a heat alarm.

Check yourself

The book has no end-of-chapter questions, so these are my own, based on its pages.

1. Why should you usually avoid P3, P4 and P10?
2. Which pins are free digital pins, not shared with anything on the board?
3. What range does analog read pin give?
4. digital write pin P16 to 1 means…
5. The ZX-SWITCH01 output is _____ when pressed.
6. In the light lab, why does removing show number make the LED respond faster?
7. In the clap switch, what does set LED_Status to not LED_Status do?
8. Using Temp = (adc × 0.16) − 20, what is the temperature when adc is 300?
9. Which cable joins the AX-microBIT+ to an INEX sensor board?