PiicoDev Capacitive Touch Sensor CAP1203 - Micro:bit Guide

Updated 01 June 2022

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:

Contents

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.

connect-piicodev-colour-sensor-to-micro-bit

Download MicroPython modules

We will need three files to easily read data from the Capacitive Touch Sensor:

  • Download the PiicoDev Unified LibraryPiicoDev_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.

piicodev-capacitive-touch-sensor-1203-example-demo-microbit

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.

make-your-own-labels-piicodev-cap1203

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!

Resources

PiicoDev Unified MicroPython module for CAP1203

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.