Touch¶
On this page we will learn
- how capacitive touch works
- how to detect when the logo is touched
- how to use pins 0, 1 and 2 as touch inputs
Terminology
- capacitive touch – a way of sensing touch by detecting the tiny electrical charge in our finger, the same idea used by phone screens.
- pin – a metal connection point on the micro:bit, such as pins 0, 1 and 2, that can be used for inputs and outputs.
The gold logo and pins 0, 1 and 2 can sense touch using capacitive touch, the same idea used by phone screens.
Possible uses:
- extra buttons
- touch-controlled games
- musical instruments
- interactive art
Connect it¶
The touch logo and pins are built into the micro:bit, so we don't need to connect anything.
- Each example is a
main.pyfile. See Your First Program for how to create, upload and run it. - The logo is ready to use. Pins 0, 1 and 2 must be set to capacitive touch mode first.
Set it up¶
The touch logo and pins are part of the microbit library. We import it at the top of every program:
Methods¶
| Method | Parameters | Returns | Description |
|---|---|---|---|
pin_logo.is_touched() |
none | Boolean | True if the logo is being touched |
pin0.set_touch_mode(pin0.CAPACITIVE) |
touch mode | none | Makes pin 0 sense touch like the logo (also pin1, pin2) |
pin0.is_touched() |
none | Boolean | True if pin 0 is being touched (also pin1, pin2) |
pin_logo.is_touched()¶
Checks whether the gold logo is being touched right now.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 4 → starts an endless loop.
- line 5 → checks if the logo is being touched.
- line 6 → if it is, shows a happy face.
- line 7 → if it isn't…
- line 8 → …shows a sad face.
set_touch_mode() and is_touched() on pins¶
set_touch_mode() makes a pin sense touch the same way as the logo. is_touched() then checks whether that pin is being touched.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 4 → sets pin 0 to capacitive touch mode.
- line 7 → starts an endless loop.
- line 8 → checks if pin 0 is being touched.
- line 9 → if it is, shows a happy face.
- line 10 → if it isn't…
- line 11 → …shows a sad face.
Documentation¶
Documentation is the official guide written by the people who made the code library, and we can use it to look up every method and its parameters, including ones that aren't covered on this page.
Exercises¶
PRIMM
Time to modify the code and see what happens.
Starter files are in the touch folder of your tutorial files. Solutions are on the Exercise Solutions page.
Exercise 1¶
Starter: touch/ex1_move_pixel
Can you light the pixel at (2, 2), then move it right when pin 2 is touched and left when pin 0 is touched?