Help Center
< All Topics
Print

Micro:Bit Touch Reaction Tutorial

Tutorial Aim:

The aim of this tutorial is to demonstrate how to generate a sound and display a message when a TTP223 capacitive touch sensor is activated on a Robot:Bit v2, by continuously polling the sensor. This project will teach users how to interface the touch sensor digitally with the micro:bit via the Robot:Bit extension, read its digital output by polling in a loop, detect touch activation events, and respond with audio and visual feedback.

Requirements:

This tutorial makes use of the Micro:bit Electronics Learning Package.

  • Microsoft MakeCode for micro:bit
  • Micro:bit V2
  • Micro:bit Robot:bit Expansion Shield V2
  • 3 FM Jumper Wires
  • TTP223 Touch Sensor

Pin Layout:

Micro:Bit Pins:Sensor Pins:
GNDGND
3.3VVCC
2I/O

Setup:

Please refer to image below for wiring.

  • Use the FM jumper wires to connect the TTP223 touch sensor to pin2 on the Micro:Bit
  • Open Microsoft Makecode
  • Plug in the Micro:bit to the computer
Click to expand

Code Walk Through:

Determining if the touch sensor was activated:

When touch is detected the sensor outputs a reading of 1, otherwise the reading is 0. We can use conditional statements to determine if the touch sensor was activated. The code for this section will go inside the ‘forever’ block.

  • Creating a conditional block: In the Logic tab, select the conditional “if true then” and place it in the forever block.
  • Checking if touch was detected: Select the comparison block “0 = 0” in the Logic tab and change it to “digital read pin P2 = 1”, where “digital read pin (number)” can be found in the Pins tab under the Advanced section.
Click to expand
Displaying message and activating music when the sensor is triggered:

We can indicate when the touch sensor is triggered by playing a music tune & displaying a message on the LED grid. The code for this section will go inside the ‘forever’ block.

  • Playing a music tune: Select “play (tune) until done” in the micro:bit (V2) section under the Music tab, change the tune to yawn and place the line underneath the conditional if statement
  • Displaying a message: In the Basic tab select “show string”, write the message “Ouch!” and place it under the conditional if statement.
Click to expand
Flashing the code onto the Micro:Bit:
  • Make sure the Micro:Bit is connected to the computer 
  • On the bottom left corner, click the “Download” button and follow the prompts

Python Code:

def on_forever():
    # Checking if the sensor has been touched
    if pins.digital_read_pin(DigitalPin.P2) == 1:
        # Displaying message and playing sound
        music.play(music.builtin_playable_sound_effect(soundExpression.yawn),
            music.PlaybackMode.UNTIL_DONE)
        basic.show_string("Ouch!")
basic.forever(on_forever)

Simulated Touch Reaction:

The microbit will scroll through the text ‘ Ouch’ as shown below, and a tone will play at the same time. 

Downloadable Content:

Please find this tutorial's python & hex file for microsoft makecode on our Github.

Credits:

  • Microsoft
  • The Micro:bit Community
  • The STEM Community
Table of Contents