Overview
This tutorial is to design a touch switch through a raspberry pi board and digital touch sensor.When it starts,the raspberry pi will check the output signals fromTTP223 digital touch sensor.The relay module will be in circuit close and the buzzer will ring once when this sensor feel the touch. The touch sensor uses the most popular capactiive sensing technology.Generally,the sensor feedback low level voltage, it stay in low-power mode.Once it feel the touch,the sensor will feedback high level voltage and change to fast mode. After 12 seconds without the touch, the mode switch to the low-power mode.
Experimental Parts
Raspberry Pi3 x1
|
|
Buzzer sensor x1 | |
Digital touch sensor TTP223 x1 | |
relay module x1 | |
breadboard x1 | |
male to male jumper wire | |
male to female jumper wire |
Hardware
The digital touch sensor,buzzer alarm sensor and 2-channel relay module need to be used in this project.
The working volatage of touch sensor is 3.3V-5.5V and this project use the 3.3V as power supply.When the sensor feel the touch,it feedback high level voltage;
The working volatage of buzzer alarm sensor and 2-channel relay module is 3.3V-5.5V and this project use the 3.3V as power supply. The sensor module will respond to the low level voltage.
The schematic diagram is as follows:
Pay more attention to the anode and cathode, otherwise it would burnout your raspberry pi board and sensor.You can connect the GND of the sensor to OV, VCC to 3.3V.You could visit the article for your reference if you want to learn more about the raspberry pi IO port : How to read Raspberry Pi i/o pin diagram (GPIO pin graph)
Software
You could choose the connect the raspberry pi to monitor, or login in pi via SSH.
1)Write the code
Add new file named touch.py under the /home/pi file path via nano editor (the name is arbitrary)
sudo nano touchsensor.py
Write the sample code in new file:
import RPi.GPIO as GPIO import time import os #sensor pin define buzzer = 14 touch = 26 relay_in1 = 13 relay_in2 = 19 #GPIO port init def init(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(buzzer,GPIO.OUT) GPIO.setup(relay_in1,GPIO.OUT) GPIO.setup(relay_in2,GPIO.OUT) GPIO.setup(touch,GPIO.IN,pull_up_down=GPIO.PUD_UP) pass #turn on buzzer def buzzer_on(): GPIO.output(buzzer,GPIO.LOW) time.sleep(0.2) GPIO.output(buzzer,GPIO.HIGH) time.sleep(0.2) pass #turn off buzzer def buzzer_off(): GPIO.output(buzzer,GPIO.HIGH) pass #turn on relay def relay_on(): #open relay channal1 ana channal2 GPIO.output(relay_in1,GPIO.LOW) GPIO.output(relay_in2,GPIO.LOW) #turn off relay def relay_off(): GPIO.output(relay_in1,GPIO.HIGH) GPIO.output(relay_in2,GPIO.HIGH) touchstatus = False #read digital touch sensor def read_touchsensor(): global touchstatus if (GPIO.input(touch)==True): touchstatus = not touchstatus if touchstatus: print"Turn on relay" print"\n" buzzer_on() relay_on() else: print"Turn off relay" print"\n" buzzer_on() relay_off() pass #main loop def main(): print"...................................................................System initializing..." init() buzzer_off() relay_off() print"...................................................................Ok" print"...................................................................Please touch" print"\n" while True: read_touchsensor() if __name__ == '__main__': try: main() pass except KeyboardInterrupt: pass pass GPIO.cleanup()
The code can be obtained by executing shell commands.
sudo wget --no-check-certificate http://osoyoo.com/driver/touchsensor.py
2)Execute python program
sudo python ./touchsensor.py
3)Test
When you use the finger touch the place with concentric circles, you will hear a click sound from buzzer sensor closes and ring sound. In the same way,you could connect the other devices to relay module and control it via the touch sensor.
DownLoad Url osoyoo.com