Raspberry Pi - Touch Sensor - LED

This tutorial instructs you how to use the Raspberry Pi and touch sensor to control the LED. We will learn two different applications:

Application 1 - The LED state is synchronized with the touch sensor state. In detail:

Application 2 - The LED state is toggled each time the touch sensor is touched. More specifically:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Touch Sensor
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

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. We appreciate your support.

Overview of LED and Touch Sensor

If you are unfamiliar with LED and touch sensor (including pinout, operation, and programming), the following tutorials can help:

Wiring Diagram

The wiring diagram between Raspberry Pi and Touch Sensor LED

This image is created using Fritzing. Click to enlarge image

Application 1 - The LED state is in sync with the touch sensor state

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_led.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-led 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 = 15 # Set the pin number connected to the LED LED_PIN = 16 # Set the GPIO pin as an input GPIO.setup(TOUCH_PIN, GPIO.IN) # Set the GPIO pin as an output for the LED GPIO.setup(LED_PIN, GPIO.OUT) try: while True: # Read the state from the touch sensor touch_state = GPIO.input(TOUCH_PIN) if touch_state == GPIO.HIGH: # Sensor touched, turn on the LED GPIO.output(LED_PIN, GPIO.HIGH) else: # Sensor not touched, turn off the LED GPIO.output(LED_PIN, GPIO.LOW) 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_led.py
  • Touch the touch sensor and hold it for a few seconds.
  • Check out the change in the LED's condition. You will see that the LED state is in sync with the touch sensor state.

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

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Application 2 - Touch Sensor toggles LED

Detailed Instructions

  • Create a Python script file touch_sensor_toggle_led.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-led import RPi.GPIO as GPIO import time # Set the pin number connected to the touch sensor TOUCH_PIN = 15 # Set the pin number connected to the LED LED_PIN = 16 # Set GPIO mode and initial LED state GPIO.setmode(GPIO.BCM) GPIO.setup(TOUCH_PIN, GPIO.IN) # Touch sensor input GPIO.setup(LED_PIN, GPIO.OUT) # LED output led_state = False # LED is initially OFF try: prev_touch_state = GPIO.input(TOUCH_PIN) while True: touch_state = GPIO.input(TOUCH_PIN) if touch_state != prev_touch_state: if touch_state == GPIO.HIGH: # Check if touch sensor is being touched led_state = not led_state # Toggle the LED state. If it was ON, it becomes OFF, and vice versa. GPIO.output(LED_PIN, led_state) # Apply the updated LED state # Debounce by adding a small delay time.sleep(0.2) prev_touch_state = touch_state except KeyboardInterrupt: GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 touch_sensor_toggle_led.py
  • Touch and release the touch sensor several times.
  • Check out the change in the LED's state.

You will see that the LED state is toggled once each time the touch sensor is touched.

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!