The gold logo on the front of the micro:bit v2 acts as a capacitive touch sensor connected to pin P1_04. Since there is no dedicated touch peripheral, detection is achieved by measuring the time it takes for the pin to charge via the board's 10 MΩ pull-up resistor.
The measurement process:
- The pin is driven low briefly to drain any existing charge.
- The pin is switched to a floating input.
- The code counts the microseconds elapsed before the pin reaches a logic high.
A finger on the logo increases capacitance, causing the charge time to increase. An untouched pad charges in roughly 15 µs, while a touched pad takes significantly longer.
Hysteresis:
To prevent flickering when a finger is near the threshold, the Logo implementation uses hysteresis. It uses a higher threshold (TOUCH_HIGH) to transition from 'untouched' to 'touched', and a lower threshold (TOUCH_LOW) to transition from 'touched' to 'released'.
# use microbit_common as microbit;
# use microbit::Board;
# use microbit::hal::Timer;
# use microbit::logo::Logo;
let board = Board::take().unwrap();
let mut timer = Timer::new(board.TIMER0);
let mut logo = microbit::logo::Logo::new(board.pins.p1_04);
loop {
if logo.is_touched(&mut timer) {
// the logo is being touched
}
}