Overview

本文介绍如何用树莓派设计数字触摸开关,并通过这个数字触摸开关控制继电器通断。程序启动树莓派不间断检测TTP223数字触摸传感器模块输出信号,当检测到有触摸动作发生时候,继电器闭合,并且在触摸时候蜂鸣器会响一声,代表被触摸。该模块是一个基于触摸检测ICTTP223B的电容式点动型触摸开关模块。常态下,模块输出低电平,模式为低功耗模式;当用手指触摸相应位置时,模块会输出高电平,模式切换为快速模式;当持续12秒没有触摸时,模式又切换为低功耗模式。

DSC_5484

Experimental Parts

为了完成设计需要用到一下器件

PI
Pi3 x1
18
 有源蜂鸣器模块 x1
13
 数字触摸开关TTP223 x1
17
 2路继电器模块 x1
LACC2006AD-12
 面包板 x1
LACC2006AD-31
 公对公跳线 x若干
19
 公对母跳线 x若干

Hardware

本文中用到了三个传感器,分别是数字触摸开关TTP223模块、有源蜂鸣器模块和2路继电器模块。数字触摸开关TTP223模块工作在DC2—5.5V,本文中采用3.3V供电,检测到有触摸动作时候输出高电平,否则输出低电平;有源蜂鸣器模块采用2TY三极管(S8550)驱动,工作在3.3V-5V之间,本文将其接到3.3V电压上,模块低电平触发;2路继电器模块低电平触发,同样用3.3V供电。

与树莓派具体连线参考下图

Untitled Sketch_touchsensor

在接线的时候请认清模块正负极,不要接反了,否则会烧坏你的Pi和模块。模块的上的GND接Pi上的0V;模块上的VCC接3.3V。对于如何识别Pi的IO口请参考这篇文章: How to read Raspberry Pi i/o pin diagram (GPIO pin graph)

Software

可以把pi直接接到显示器上,也可以通过SSH方式远程登录Pi。

1)编写代码

在/home/pi路径下用nano新建一个touchsensor.py(名字随意,你喜欢就好!)

sudo nano touchsensor.py

并往新建的文件中写入如下代码:

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()

上面的代码可以通过执行下面的shell命令得到

sudo wget http://osoyoo.com/driver/touchsensor.py

2)执行python脚本

sudo python ./touchsensor.py

2017-03-25-070337_1824x984_scrot

3)测试

用手指触摸数字触摸开关模块上同心圆位置,会听到滴答声,这是蜂鸣器吸合的声音,在触摸同时蜂鸣器会响一声。可以将一些其他设备接到继电器,就可以通过自己设计的触摸开关来控制了。

Comments are closed.

Bombax Theme designed by itx