Let's get started with the PiicoDev® Capacitive Touch Sensor. In this guide, we'll connect the PiicoDev Capacitive Touch Sensor to our Micro:bit V2 and get it working with some example code to detect touch events. We'll also remix the code to make a simple musical instrument, using the touch sensor like a three-key piano.
We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).
To follow along, it's best to have:
- A Micro:bit V2
- A PiicoDev Capacitive Touch Sensor CAP1203
- A PiicoDev Adapter for Micro:bit
- A PiicoDev Cable
- (Optional) A PiicoDev platform helps secure everything together.
- (Optional) Alligator Clips to use other objects as touch sensors.
Contents
- Introduction
- Connect the Sensor to your Micro:bit
- Download MicroPython Modules
- Example Code - Read Touch Status
- Setup Options - Single/Multi-Touch and Tune Sensitivity
- Remix - Musical Instrument
- Conclusion
- Resources
Introduction
The PiicoDev Capacitive Touch Sensor has three copper pads on it (labelled 1, 2 and 3) that work like three separate buttons. Touching one of the touchpads changes its capacitance and this change is measured by the CAP1203 device which registers it as a touch event. The device is capable of registering multiple touch events at the same time or filtering for just the first event. We can also tune the touch sensitivity which is useful if we want to cover the sensor or connect alligator clips and detect touch with some remote objects
Connect the PiicoDev Capacitive Touch Sensor to your Micro:bit
Plug your Micro:bit V2 into the adapter. Connect your touch sensor to the adapter via the PiicoDev cable, and finally connect your Micro:bit to your computer with a USB lead.
If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.
Download MicroPython modules
We will need three files to easily read data from the Capacitive Touch Sensor:
- Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as").
- Download the device module: PiicoDev_CAP1203.py (right-click, "save link as")
- Download the example script: main.py (right-click, "save link as")
It will be best to keep these files wherever you like to keep your coding projects eg. Documents > PiicoDev
Example Code - Read Touch Status
We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).
Open Thonny, connect to your Micro:bit and navigate to where you stored the files from the last step. Right-click each file and select "Upload to /" to upload them to your Micro:bit (Hint: View the files menu with View > Files)
Restart your Micro:bit (Keyboard shortcut, Ctrl+D) and you should see the script printing "Touch Pad Status" data in the shell. When you touch a pad, the corresponding number should change from zero to one.
Above: Touch Status data streams up the Shell, while the Plot (bottom-right) shows historic data. The plot shows the pads (Blue: 1, Yellow: 2, Red: 3) are pressed and released in sequence, and then I slide my finger slowly across pads #1 and #2. While my finger is touching both pads, they each register a separate touch event and their plotted lines overlap.
In this example, the status of each pad is assigned into a Python dict variable called status, and we access the status for pad 1, 2 or 3 with that key, eg. status[1] retrieves the status of Touchpad #1. Status is zero when the pad is not touched, and one when it is being touched.
Setup options
By default, the PiicoDev Capacitive Touch Sensor is initialised in multi-touch mode with a good all-rounder sensitivity level. You can change these settings depending on how you initialise the device to use single-touch mode or increase/decrease the sensitivity.
Single-Touch mode
The following initialisation will set the sensor up to reject multiple touch events and return only the first touch event.
touchSensor = PiicoDev_CAP1203(touchmode='single')
Sensitivity
Tuning the sensitivity can allow the sensor to detect touch events from behind another object eg. a piece of paper or plastic. This can be useful if you want to put a nice label over your sensor or conceal it behind a (thin-walled) enclosure. Tuning the sensitivity is also necessary if you want to use the alligator clip terminals to connect an external object to the sensor. There are eight sensitivity levels, with 0 being most sensitive, and 7 being least sensitive. The default sensitivity is 3. The initialisation would look something like this:
touchSensor = PiicoDev_CAP1203(sensitivity=6)
It's always best to power-cycle the circuit whenever you change the touch characteristics by eg. covering a touch pad or connecting an object with an alligator clip. On powerup, the CAP1203 performs a self-calibration which will make the best use of your new sensitivity setting.
Above: Using coloured paper to create fancy labels for a touch sensor. You can also connect other objects to the touch sensor using alligator clips - conductive or organic objects like fruit & veg work best. You may need to tune the sensitivity to get other objects to work reliably.
Of course, the initialisation options can be combined to set touchmode and sensitivity at the same time eg.
touchSensor = PiicoDev_CAP1203(touchmode='single', sensitivity=6)
Remix - Musical Instrument
We'll remix the example program to change create a simple musical instrument. We'll assign each touch pad to a musical note, and play that note on the Micro:bit's built-in speaker whenever the pad is touched. Code along with the tutorial video. If you get stuck, the completed project is available to copy below.
Run the remix code and see what tunes you can create with your new musical instrument.
# PiicoDev Capacitive Touch Sensor CAP1203 Musical Instrument # Read the touch sensor buttons and play a corresponding note import music from PiicoDev_CAP1203 import PiicoDev_CAP1203 from PiicoDev_Unified import sleep_ms # cross-platform-compatible sleep touchSensor = PiicoDev_CAP1203(touchmode="single") while True: status = touchSensor.read() # check which key is pressed and play a note if status[1] == 1: music.play("F#") print("F#") if status[2] == 1: music.play("E") print("E") if status[3] == 1: music.play("D") print("D") sleep_ms(100)
Our simple instrument can only play three notes for now. Perhaps you could try modifying the code to play different notes if different combinations of pads are touched. For example, play a different note if both Pad #1 and Pad #2 are touched at the same time.
Conclusion
We can detect touch events using a PiicoDev Capacitive Touch Sensor. By changing the touch mode or sensitivity, we can tune the sensor for different applications like detecting touch through a label, or through an enclosure. We can also trigger different program behaviours like printing different messages or playing musical notes.
If you make something cool with this starter project we'd love for you to share it on our forums! And if you'd like some help with this guide, start the conversation below - We're full-time makers and here to help!