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.
Digital and analog
| Digital (ดิจิตอล) | Analog (อะนาลอก) | |
|---|---|---|
| Values | Only 0 or 1 | 0 to 1023 on input |
| Like | A light switch | A volume knob |
| Block | digital write pin | analog read pin |
On the AX-microBIT+, seven pins have JST sockets: P0, P1, P8, P12, P16, SDA and SCL.
4.1 Make music สร้างเสียงดนตรี
🧪 Lab 4.1 · London Bridge
- Build the code below. play tone is in the Music group.
- Download it to the Micro:bit.
- Set the P0 jumper to PIEZO.
- Press button A on the Micro:bit or the activity board.
💻 Lab 4.1: London Bridge
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))
}
}) | G1 A1 G1 F1 E1 F1 G2 | D1 E1 F2 | E1 F1 G2 |
| G1 A1 G1 F1 E1 F1 G2 | D2 G2 | E2 C2 |
On a V2 board the tune also plays through the built-in speaker, even without the activity board.
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.
| Board | Signal | Result |
|---|---|---|
| ZX-LED (output) | Logic 1 from the Micro:bit | LED on |
| ZX-LED (output) | Logic 0 from the Micro:bit | LED 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
🧪 Lab 4.2.1 · Steps
- Build the code, then download it to the Micro:bit.
- Plug the ZX-LED into P16.
- Press A and the LED lights. Press B and it goes out.
💻 Lab 4.2.1: buttons A and B
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.
💻 Lab 4.2.2: external switches
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.
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.
💻 Lab 4.2.3: the knob on P2
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 (สวิตช์สนธยา).
🔦 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
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
See it in JavaScript
basic.forever(function () {
if (pins.analogReadPin(AnalogPin.P1) > 500) {
pins.digitalWritePin(DigitalPin.P16, 0)
} else {
pins.digitalWritePin(DigitalPin.P16, 1)
}
}) 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.
🧪 Lab 4.2.5 · Build it block by block
- Add on start. Inside it, put set variable to 0 from Variables.
- Make a new variable called LED_Status and select it in that block.
- From Logic, drop false into the block, so the LED starts in the off state.
- Add forever, and put an if … then inside it.
- In the condition, put a comparison from Logic. On its left, put analog read pin set to P1.
- Change the comparison to > and the number to 50.
- Inside then, add set LED_Status. Fill it with not from Logic, holding the LED_Status variable.
- Below that, add an if … then … else that checks LED_Status = true.
- Under then, put digital write pin P16 to 1. Under else, put the same block set to 0.
- After the outer if, add pause (ms) 100.
- Download it, connect the parts, then clap near the microphone. Clap again to turn the LED off.
💻 Lab 4.2.5: clap switch
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.
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.
| Range | Up to +125 °C, typically within 2 °C |
|---|---|
| Supply | 3.1 to 5.5 V, using only 6 µA |
| Output change | 19.5 mV for each °C |
| Response | Within about 2 seconds |
🧪 Lab 4.3 · Build the thermometer
- Create the variables val and temp in Variables.
- In forever, add set val to with analog read pin P1 inside it. That block is under Advanced › Pins.
- Add set temp to. From Math, build the sum one block at a time: val × 16, then that ÷ 100, then that − 20.
- Wrap the sum in round, and add show number holding temp.
- Download it, then plug the probe into P1.
- Warm the probe in your hand and watch the number change.
💻 Lab 4.3: thermometer (simplified)
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.
4.4 Cables and parts อุปกรณ์เสริม
| Part | What it is | Use |
|---|---|---|
| JST3AA-8 | 8-inch, 3-wire cable with a 2 mm JST plug at each end | Board to INEX sensor or output |
| JST3AF-8 | 8-inch, 3-wire cable from JST to 2.54 mm IDC sockets | Board to parts with pin headers |
| IDC1MF / IDC1FF | 10 cm jumper wires, 5 of each, male-female and female-female | Headers to a breadboard or part |
| microB-USB | 1.5 m data cable | Computer to Micro:bit |
| DC adapter | +5 V, up to 2 A, with a barrel plug | Power for the activity board |
The three JST wires are G (ground), S (signal) and V (power).
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
- Change the threshold in the light lab. Find the value that suits your classroom.
- Program a different tune, using the same blocks as London Bridge.
- Fix the clap switch, so that one long clap only flips the light once.
- 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.