Raspberry Pi Pico - Touch Sensor

This tutorial instructs you how to utilize a Raspberry Pi Pico with a touch sensor, commonly referred to as a touch button or touch switch. Touch sensors are frequently employed to control devices like lamps through simple touch interaction, functioning similarly to conventional buttons. The preference for touch sensors in many modern devices stems from their ability to give products a sleeker, more integrated appearance compared to traditional mechanical buttons.

Raspberry Pi Pico touch sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Touch Sensor
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Touch Sensor

Pinout

The touch sensor has three connectors.

  • GND pin: connect to GND (0 volts)
  • VCC pin: connect to VCC (5 volts or 3.3 volts)
  • SIGNAL pin: if not touched, it shows LOW; if touched, it shows HIGH. Connect this pin to the Raspberry Pi Pico's input pin.
Touch Sensor Pinout

How It Works

  • When no one is touching the sensor, the signal pin is LOW.
  • When someone touches the sensor, the signal pin is HIGH.

Raspberry Pi Pico - Touch Sensor

The touch sensor's SIGNAL pin is connected to an input pin on the Raspberry Pi Pico.

To know if the touch sensor is touched, check if the input pin on the Raspberry Pico is active.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Touch Sensor

This image is created using Fritzing. Click to enlarge image

Touch Sensor - Raspberry Pi Pico Code

Reads the value from the touch sensor and print it out

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-touch-sensor """ from machine import Pin import utime SENSOR_PIN = 28 # The Raspberry Pi Pico pin connected to the touch sensor (GP28) # Create a Pin object to handle the input from the touch sensor sensor = Pin(SENSOR_PIN, Pin.IN) while True: # Read the state of the input pin state = sensor.value() # Print the state to the console print(state) # Sleep for a short period to avoid flooding the output utime.sleep(0.5)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the touch sensor according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Touch the sensor with your finger and then take your finger off.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 0 0 1 1 1 1 1 0 0 0 0
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Detects the sensor is touched or released

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-touch-sensor """ from machine import Pin import utime SENSOR_PIN = 28 # The Raspberry Pi Pico pin connected to the touch sensor (GP28) # Create a Pin object to handle the input from the touch sensor sensor = Pin(SENSOR_PIN, Pin.IN) # Initialize the previous touch state to 0 (assumed LOW) prev_touch_state = 0 while True: # Read the current state of the input pin touch_state = sensor.value() # Check if the sensor state has changed from LOW to HIGH (touched) if prev_touch_state == 0 and touch_state == 1: print("The sensor is touched") # Check if the sensor state has changed from HIGH to LOW (released) elif prev_touch_state == 1 and touch_state == 0: print("The sensor is released") # Save the current state as the previous state for the next loop iteration prev_touch_state = touch_state # Sleep for a short period to debounce the sensor utime.sleep(0.1)

Detailed Instructions

  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your Raspberry Pi Pico.
  • Click the green Run button (or press F5) to execute the script.
  • Put your finger on the sensor and keep it there.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The sensor is touched
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡
  • Remove your finger from the sensor.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The sensor is touched The sensor is is released
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!