← Micro:bit

Chapter 3

Working with Switches and Touch Logo

การใช้งานสวิตช์ A B

  • The micro:bit has two push buttons, A and B (ปุ่ม A และปุ่ม B).
  • micro:bit V2 adds a touch-sensitive gold logo (จุดสัมผัสแบบคาปาซิตีฟ).
  • Your code can react to the buttons and the logo, so the board answers you.

3.1 Button Event Coding โค้ดสำหรับกดปุ่ม A และ B

  • Button A draws one running pose on the 5×5 display, and button B draws the other.
  • Press them in turn and the stickman runs as fast as your fingers.
Waiting nothing to do A is pressed! the event happens on button A pressed handler runs: show leds handler finished: back to waiting ↻
Event-driven code: the board waits, a press wakes the handler, and the handler runs once and then goes back to waiting.

Step-by-Step Exercise: Manual Runner via Buttons

  1. Step 1 — Drag on button: pull an on button [A] pressed block from the Input category onto the workspace.
  2. Step 2 — Draw frame 1: put show leds inside it and light the first running pose (first picture below).
  3. Step 3 — Duplicate the whole block: right-click the on button block and choose Duplicate. The show leds inside comes with it.
  4. Step 4 — Edit frame 2: in the copy, redraw the LEDs as the second running pose (second picture).
  5. Step 5 — Switch it to button B: change the dropdown on the second block from button A to button B.
Step 2: button A shows this
Step 4: button B shows this
Same two poses as text
Frame 1          Frame 2
. . # . .        . . # . .
. # # # .        . . # . .
. . # . .        . # # # .
. # . # .        . . # . .
# . . . #        . # . # .

💻 3.1 One pose per button — the runner you drive by hand

on button A pressed
show leds
on button B pressed
show leds
on button A+B pressed
clear screen
See it in JavaScript
input.onButtonPressed(Button.A, function () {
	basic.showLeds(`
		. . # . .
		. # # # .
		. . # . .
		. # . # .
		# . . . #
		`)
})
input.onButtonPressed(Button.B, function () {
	basic.showLeds(`
		. . # . .
		. . # . .
		. # # # .
		. . # . .
		. # . # .
		`)
})
input.onButtonPressed(Button.AB, function () {
	basic.clearScreen()
})
  • How it works: flash the code, then press A and B in turn.
  • Each press paints its own pose, so the figure runs in time with your fingers.

Three Button Combinations in MakeCode

The on button [...] pressed block has three choices in its dropdown.

A A B shows pose 1 B A B shows pose 2 A+B A B clear screen
The three events. A+B is its own event, with its own handler.
  • A: fires when the left button is pressed.
  • B: fires when the right button is pressed.
  • A+B: fires when both buttons are pressed together. MakeCode's simulator shows a virtual A+B button for it.

What is inside a button? สวิตช์กดติดปล่อยดับ

Released 3.3 V pull-up pin = 1 GND (0 V) switch open: HIGH Pressed 3.3 V pull-up pin = 0 GND (0 V) switch closed: LOW
A button is a switch to ground. The pull-up resistor keeps the pin at 3.3 V (reads 1) until a press connects it to GND (reads 0).
  • Button A is wired to pin P5 and button B to pin P11.
  • MakeCode hides the voltages: you only see the event, or button A is pressed = true or false.

Next: 3.2 Conditionals & Repeat Loops →

3.2 Conditionals & Repeat Loops สร้างเงื่อนไขกับปุ่ม A และ B

  • Besides on button, you can ask a question with button A is pressed.
  • The goal here: when A is pressed, play the running animation 4 times, then clear the screen and wait for A again.

Two ways to read a button

on button A pressed event waiting (nothing runs) press! handler runs once good for: one reaction per press A, B and A+B each get a handler if button A is pressed polling inside forever A? no A? no A? no A? yes! if-body runs good for: "while A is held, keep doing" needs a forever loop to keep asking
Event: the board is told when something happens. Polling: the program keeps asking, and only acts when the answer is yes.

Step-by-Step Assembly of the 4-Cycle Loop Runner

  1. Step 1 — Drag if then: take if then from Logic and drop it inside forever.
  2. Step 2 — Add the question: put button [A] is pressed from Input into the if slot.
  3. Step 3 — Add the loop: drag repeat [4] times from Loops inside if then.
  4. Step 4 — Set the count: type 4 in the repeat block.
  5. Step 5 — Place the poses: put show leds pose 1 and pose 2 inside the loop.
  6. Step 6 — Insert pauses: put pause (ms) 100 after each show leds.
forever button A is pressed? Yes repeat 4 times pose 1, pause, pose 2, pause show icon Happy pause 1000 clear screen No back to the top
Flowchart of the program: forever keeps asking. If the answer is No, it asks again; if Yes, the animation runs and then it asks again.
  • This if has no else, so the No path just goes round again.
  • With if … else, the No path would run its own blocks, for example clear screen.
round 1 of 4 pose 1 + pose 2 counter = 1 round 2 of 4 pose 1 + pose 2 counter = 2 round 3 of 4 pose 1 + pose 2 counter = 3 round 4 of 4 pose 1 + pose 2 counter = 4 loop over every round = 2 × (400 ms show leds + 100 ms pause) = 1000 ms
repeat 4 times: the blocks inside run once per round, and a hidden counter goes 1, 2, 3, 4. When it passes 4 the loop ends.

Behavior Analysis

What the program does

  • Press A: the two running poses alternate 4 times.
  • After the loop it shows the Happy icon, waits, then runs clear screen.
  • Then forever asks again: press A for another sprint.
  • Remove the Happy icon and pause if you want the screen cleared straight after the loop.
4 rounds of pose 1 + pose 2 Happy stays clear 0 4000 ms 5600 ms 600 ms icon + 1000 ms pause
Timeline of one sprint on a real board. show leds waits 400 ms and show icon waits 600 ms by itself; each pause adds to that.

💻 3.2 Hold A to sprint — if … then with repeat 4 times

forever
if button A is pressed then
repeat 4 times
show leds
pause (ms) 100
show leds
pause (ms) 100
show icon
pause (ms) 1000
clear screen
See it in JavaScript
basic.forever(function () {
	if (input.buttonIsPressed(Button.A)) {
		for (let i = 0; i < 4; i++) {
			basic.showLeds(`
				. . # . .
				. # # # .
				. . # . .
				. # . # .
				# . . . #
				`)
			basic.pause(100)
			basic.showLeds(`
				. . # . .
				. . # . .
				. # # # .
				. . # . .
				. # . # .
				`)
			basic.pause(100)
		}
		basic.showIcon(IconNames.Happy)
		basic.pause(1000)
		basic.clearScreen()
	}
})
The same program as text
forever:
    if button A is pressed then:
        repeat 4 times:
            show leds:   (pose 1)
            pause (ms) 100
            show leds:   (pose 2)
            pause (ms) 100
        show icon (Happy)
        pause (ms) 1000
        clear screen

Counting presses with a variable

  • A variable is a named box that remembers a number.
  • A adds 1 to count and shows it. B sets it back to 0 and shows a tick.
count 0 A: +1 count 1 A: +1 count 2 A: +1 count 3 press B: count = 0 and show a tick on start: count = 0 and show 0
The variable count only changes when a handler runs. Its value is still there between presses.

💻 3.2 Counting presses — a variable that remembers

on start
set count to 0
show number 0
on button A pressed
change count by 1
show number count
on button B pressed
set count to 0
show icon
See it in JavaScript
let count = 0
basic.showNumber(count)
input.onButtonPressed(Button.A, function () {
	count += 1
	basic.showNumber(count)
})
input.onButtonPressed(Button.B, function () {
	count = 0
	basic.showIcon(IconNames.Yes)
})

Next: 3.3 Capacitive Touch Logo (V2) →

3.3 The Capacitive Touch Logo ใช้งานจุดสัมผัส Logo เพื่อแสดงผล (เฉพาะ micro:bit V2)

  • micro:bit V2 can sense a finger on its gold logo. This is capacitive touch (คาปาซิตีฟ).
  • micro:bit V1 had no logo sensor. You had to touch pins P0, P1 or P2 while holding GND with the other hand.
  • On V2 you touch only the logo: the sensor uses the small electric charge of your body, so no wire to GND is needed.
1. No finger normal charge gold logo pad 2. Finger touches charge changes a tiny bit field flows into your body 3. Event on logo touched your handler runs lift finger: released (drawing is a simplified idea, not to scale)
Capacitive sensing: a finger near the gold pad slightly changes the charge the board measures. The board turns that change into a touch event.

MakeCode Logo Event Options

The on logo [pressed] block in Input has four choices.

  • touched: runs the moment a finger lands on the gold logo.
  • released: runs the moment the finger lifts off.
  • pressed: runs for a quick tap on the logo.
  • long pressed: runs when the finger stays on for at least 1 second.
no finger finger on touched released touched long pressed released short tap (under 1 s) long hold: 1 s mark, then lift
Timeline of two touches: a short tap, then a long hold. The events fire at the moments marked.

Step-by-Step Project: Interactive Mood Face

  1. Step 1 — Place on logo: drag on logo from Input onto the workspace.
  2. Step 2 — Change to touched: switch its dropdown from pressed to touched.
  3. Step 3 — Draw the neutral face: put show leds from Basic inside and light the face below.
  4. Step 4 — Duplicate: right-click the first block and choose Duplicate.
  5. Step 5 — Change to released: switch the copy from touched to released and redraw it as the smile.
Step 3: touched shows neutral
Step 5: released shows smile
  • Testing the hardware: flash the code and touch the logo: the neutral face appears.
  • Lift your finger: the smile appears straight away.

The full mood-face program

  • The program below uses the preset icons Asleep, Happy and Sad.
  • Each state is a face; each logo event moves the board to the next state.
Asleep Happy Sad on start touched released after about 1.1 s (600 ms icon + 500 ms pause), show Asleep again
State diagram of the mood-face program, drawn with the real icon patterns. A touch wakes it up; a release shows Sad for a moment, then it falls back asleep.

💻 3.3 Mood face — the V2 touch logo, no wires needed

on start
show icon
on logo touched
show icon
on logo released
show icon
pause (ms) 500
show icon
See it in JavaScript
basic.showIcon(IconNames.Asleep)
input.onLogoEvent(TouchButtonEvent.Touched, function () {
	basic.showIcon(IconNames.Happy)
})
input.onLogoEvent(TouchButtonEvent.Released, function () {
	basic.showIcon(IconNames.Sad)
	basic.pause(500)
	basic.showIcon(IconNames.Asleep)
})

Next: Interactive Switches Explorer →

Interactive Switches & Touch Explorer

Hover over or tap the buttons and the touch logo on the real micro:bit V2 photo to see how they work.

🔍

micro:bit V2 Inputs: Buttons A, B & Capacitive Touch Logo

Interactive Map
micro:bit V2 Front View - Buttons and Touch
👉

Hover or tap Button A, Button B, or the Touch Logo to explore how input events are processed.

Next: Test Your Knowledge with the Quiz →

Chapter 3 Review & Self-Check

button A
button B
logo touched
logo released
B resets count

Check what you know about button events, if and repeat, variables and the capacitive touch logo.

1. Which button combinations can trigger an event in the `on button ... pressed` block?
2. What is the main difference between `on button A pressed` and `if button A is pressed`?
3. Which new hardware input on micro:bit V2 allows touch sensing without connecting to GND?
4. Which two event blocks are available specifically for the capacitive touch logo in MakeCode?
5. If you want an animation frame to repeat exactly 4 times in a loop, which block do you use?
6. Which pin is button A wired to inside the board?
7. Which block runs the moment you lift your finger off the gold logo?
8. Why does the V2 touch logo work without holding a GND pin as well?