Raspberry Pi - Touch Sensor - Relay

This tutorial instructs you how to use the Raspberry Pi and touch sensor to control the relay. By connecting the relay to a soleniod lock, light bulb, LED strip, motor, or actuator..., we can use a touch sensor to control the them. We will learn two different applications:

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

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

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Touch Sensor
1×Relay
1×Breadboard
1×Jumper Wires
1×(Optional) Solenoid Lock
1×(Optional) 12V Power Adapter
1×(Optional) DC Power Jack
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 Relay and Touch Sensor

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

Wiring Diagram

The wiring diagram between Raspberry Pi and Touch Sensor relay

This image is created using Fritzing. Click to enlarge image

Application 1 - The relay 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_relay.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-relay """ import RPi.GPIO as GPIO # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin number to which the relay is connected RELAY_PIN = 20 # Define the GPIO pin number to which the touch sensor is connected TOUCH_SENSOR_PIN = 18 # Set up the GPIO pins GPIO.setup(TOUCH_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up resistor GPIO.setup(RELAY_PIN, GPIO.OUT) # Output try: while True: touch_state = GPIO.input(TOUCH_SENSOR_PIN) if touch_state == GPIO.HIGH: print("The sensor is touched") GPIO.output(RELAY_PIN, GPIO.HIGH) # Turn the relay on else: print("The sensor is not touched") GPIO.output(RELAY_PIN, GPIO.LOW) # Turn the relay off except KeyboardInterrupt: GPIO.output(RELAY_PIN, GPIO.LOW) # Turn off the relay GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 touch_sensor_relay.py
  • Touch your finger on the touch sensor and hold it for a few seconds, then release it.
  • Check out the change in the relay's condition. You will see that the relay 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 Relay

Detailed Instructions

  • Create a Python script file touch_sensor_toggle_relay.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-relay """ import RPi.GPIO as GPIO import time # Constants won't change TOUCH_SENSOR_PIN = 16 # The number of the touch sensor pin RELAY_PIN = 18 # The number of the relay pin # Variables will change relay_state = GPIO.LOW # The current state of the relay prev_touch_state = GPIO.LOW # The previous state of the touch sensor touch_state = GPIO.LOW # The current state of the touch sensor # Set up GPIO GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbering GPIO.setup(RELAY_PIN, GPIO.OUT) # Initialize the relay pin as an output GPIO.setup(TOUCH_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Initialize the touch sensor pin as a pull-up input try: while True: # Read the state of the touch sensor value prev_touch_state = touch_state # Save the last state touch_state = GPIO.input(TOUCH_SENSOR_PIN) # Read new state if prev_touch_state == GPIO.LOW and touch_state == GPIO.HIGH: time.sleep(0.1) # 100 milliseconds debounce time print("The sensor is touched") # Toggle the state of the relay if relay_state == GPIO.LOW: relay_state = GPIO.HIGH else: relay_state = GPIO.LOW # Control relay according to the toggled state GPIO.output(RELAY_PIN, relay_state) except KeyboardInterrupt: # Clean up GPIO on program exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 touch_sensor_toggle_relay.py
  • Touch and release the touch sensor several times.
  • Check out the change in the relay's state. You'll notice that the relay will switch on or off one time whenever you place your finger on the touch sensor.

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!