Raspberry Pi - Touch Sensor

This tutorial instructs you how to use the capacitive touch sensor with Raspberry Pi. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Touch Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

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

A capacitive touch sensor, also known as a touch button or touch switch, is frequently used to operate devices (e.g. a touchable lamp). It has the same purpose as a button. Many modern devices are equipped with it instead of a button, as it gives the product a more polished look.

The Touch Sensor Pinout

The touch sensor has three pins:

  • GND pin: This must be connected to the ground (0V).
  • VCC pin: This must be connected to the VCC (5V or 3.3v).
  • SIGNAL pin: This is an output pin. It will be LOW when not touched and HIGH when touched. This pin needs to be connected to a Raspberry Pi's input pin.
Touch Sensor pinout

How It Works

  • When the sensor is not being touched, the SIGNAL pin of the sensor will be at a LOW level.
  • However, when the sensor is touched, the SIGNAL pin of the sensor will be at a HIGH level.

Raspberry Pi - Touch Sensor

The SIGNAL pin of the touch sensor is linked to an input pin of a Raspberry Pi.

By examining the status of a Raspberry Pi pin (set up as an input pin), we can detect if the touch sensor has been activated or not.

Wiring Diagram

The wiring diagram between Raspberry Pi and Touch Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Touch Sensor

  • Sets up the Raspberry Pi pin to digital input mode with the GPIO.setup() function.
GPIO.setup(TOUCH_PIN, GPIO.IN)
  • Utilizes the GPIO.input() function to ascertain the status of the Raspberry Pi pin.
touch_state = GPIO.input(TOUCH_PIN)

There are two common use cases for the touch sensor:

  • The first: If the input state is HIGH, perform one action. If the input state is LOW, take the opposite action.
  • The second: If the input state changes from LOW to HIGH (or HIGH to LOW), do an action.

Depending on the application, one of these is selected. For example, when using a touch sensor to control an LED:

  • If the goal is to have the LED be ON when the sensor is touched and OFF when the sensor is NOT touched, the first use case should be used.
  • If the aim is to have the LED toggle between ON and OFF each time the sensor is touched, the second use case should be used.

Raspberry Pi Code for Touch Sensor

We will learn two sample codes:

  • Raspberry Pi reads the value from the touch sensor and prints it on the Terminal.
  • Raspberry Pi checks if the sensor is touched or released.

Raspberry Pi reads the value from the touch sensor and prints to the Terminal

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file touch_sensor.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-touch-sensor """ import RPi.GPIO as GPIO # Set the GPIO mode to BCM (Broadcom SOC channel numbering) GPIO.setmode(GPIO.BCM) # Set the pin number connected to the touch sensor TOUCH_PIN = 12 # Set the GPIO pin as an input GPIO.setup(TOUCH_PIN, GPIO.IN) try: while True: # Read the state from the touch sensor touch_state = GPIO.input(TOUCH_PIN) # The touch sensor outputs LOW (0) when not touched, and HIGH (1) when touched if touch_state == GPIO.LOW: print("Touch sensor is not touched.") else: print("Touch sensor is touched!") except KeyboardInterrupt: # Clean up the GPIO settings on program exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 touch_sensor.py
  • Put your finger on the sensor and then take it away.
  • Check the outcome in the Terminal.
PuTTY - Raspberry Pi
Touch sensor is not touched. Touch sensor is not touched. Touch sensor is not touched. Touch sensor is touched! Touch sensor is touched! Touch sensor is touched! Touch sensor is touched! Touch sensor is touched! Touch sensor is touched! Touch sensor is not touched. Touch sensor is not touched.

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Raspberry Pi detects the sensor touched or released

Detailed Instructions

  • Create a Python script file TO_BE_UPDATED.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-touch-sensor """ import RPi.GPIO as GPIO import time # Set the GPIO mode to BCM (Broadcom SOC channel numbering) GPIO.setmode(GPIO.BCM) # Set the pin number connected to the touch sensor TOUCH_PIN = 12 # Set the GPIO pin as an input GPIO.setup(TOUCH_PIN, GPIO.IN) # Variable to track the touch sensor state prev_touch_state = GPIO.LOW # Assuming the sensor is not touched initially try: while True: touch_state = GPIO.input(TOUCH_PIN) if touch_state != prev_touch_state: if touch_state == GPIO.HIGH: # Sensor touched event print("Touch sensor is touched!") else: # Sensor released event print("Touch sensor is released!") prev_touch_state = touch_state time.sleep(0.1) # A small delay to debounce the input except KeyboardInterrupt: # Clean up the GPIO settings on program exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 touch_sensor_events.py
  • Touch and keep your finger on the sensor.
  • Check the output in the Terminal.
PuTTY - Raspberry Pi
Touch sensor is touched!
  • Take your finger off the sensor.
  • Check the output on the Terminal.
PuTTY - Raspberry Pi
Touch sensor is touched! Touch sensor is released!

Video Tutorial

※ 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!