[Diao Ye learns programming] Arduino hands-on (13)---TTP223B capacitive touch module detects the state of the capacitive touch key and controls the relay through the serial port monitor

insert image description here

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, in accordance with the concept of true knowledge (must be hands-on), for the purpose of learning and communication, here I am going to try and do more experiments one by one. Whether it is successful or not, it will be recorded ——Small progress or unsolvable problems, I hope to be able to throw bricks and spark jade.

[Arduino] 168 kinds of sensor module series experiment (data code + simulation programming + graphic programming)
experiment 13: TTP223 touch button module self-locking jogging capacitive switch single-channel transformation SUNLEPHANT

insert image description here
Several experiments on TTP223B capacitive touch button module
1. Experimental environment
1. Hardware list required for the experiment——
Arduino Uno development board X1
DuPont lines (10 pieces are prepared)
LED light-emitting diode (blue) X1
220 ohm current-limiting resistor (1 /8W) x1
low-level trigger single-channel 5V relay module X1
TTP223B capacitive touch key module (four types) X4
Proto Shield prototype expansion board (with mini breadboard) X1
key switch module (pull-down resistor and pull-up resistor 1 each ) x2

insert image description here

5. Experiment 7: Press the capacitive touch button to pull in the relay (LED is on), then press to release (LED is off)
1. Experiment 7 refers to the open source code (Arduino):

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验七:电容触摸按下继电器吸合(LED亮),再按下释放(LED灭)
  接线:D13板载LED灯
  触摸模块    Uno
  VCC        Vcc
  GND        GND
  SIG         D2
  继电器模块   Uno
  VCC        Vcc
  GND        GND
  IN         D8
*/

// 引脚定义
int LED = 13;
int Relay = 8;//继电器接D8
int Touch_Sensor = 2;//触摸模块接D2
int condition = 0;//记录传感器的状态,标识它是否被触摸
int state = 0; //记录LED和继电器的状态,开启或关闭

void setup() {
    
    
  pinMode(LED, OUTPUT);//触摸传感器是输入,继电器和LED引脚是输出
  pinMode(Relay, OUTPUT);
  pinMode(Touch_Sensor, INPUT);
}

void loop() {
    
    
  condition = digitalRead(2); // 触摸传感器在触摸时将逻辑0更改为1
  //digitalRead()函数读取该值,并且将值存储在变量condition中。

  if (condition == 1) {
    
    
    delay(500); // 使用去抖动延迟500毫秒,用于确认单点触摸
    if (condition == 1) {
    
    
      state = ~state; //更改开关的状态(自锁反转)
      digitalWrite(LED, state);
      digitalWrite(Relay, state);
    }
  }
}

2. Experimental scene diagram
insert image description here

3. Experiment 8 Open source simulation programming (Linkboy V4.2)

insert image description here
4. Experiment 9: Open source graphics programming (Mind+, learning while editing)

insert image description here
6. Experiment 10: Detect the state of the capacitive touch key through the serial port monitor
1. Experiment 10 refers to the open source code (Arduino):

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验十:通过串口监视器检测电容触摸键的状态
  接线:
  触摸模块    Uno
  VCC        Vcc
  GND        GND
  SIG         D2
*/

#define TOUCH_SIG 2

//获取状态
boolean get_touch() 
{
    
    
  boolean touch_stat = 0;
  touch_stat = digitalRead(TOUCH_SIG); //读入状态
  return touch_stat;
}

void setup() 
{
    
    
  pinMode(TOUCH_SIG, INPUT); //设置2号端口为输入模式
  Serial.begin(115200);
}

void loop()
 {
    
    
  boolean touch_stat;
  Serial.print("\nrunning\nTouch Stat - ");
  touch_stat = get_touch();
  Serial.print(touch_stat);//串口打印触摸按键状态值
  delay(1000);//延时1000毫秒
}

2. Serial port feedback in Experiment 10

insert image description here
7. Experiment 11: Arduino CapacitiveSensor capacitive touch sensor (software implementation)
1. Install the required CapacitiveSensor library
(1) Open the IDE, pull down and click to open the management library

insert image description here
(2) Search CapacitiveSensor in the search bar

insert image description here
(3) The installation is like this

insert image description here
2. Experiment 11 reference open source code (Arduino):

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验十一:Arduino CapacitiveSensor电容式触摸传感器
  安装库:IDE-工具-管理库-搜索CapacitiveSensor-安装
  接线:引脚2和4之间接1-10M电阻,在引脚8上连接杜邦线(触摸端)
*/


#include <CapacitiveSensor.h> //调用函数库
CapacitiveSensor cs_2_4 = CapacitiveSensor(2, 4); //设置发射脚和接收脚
//引脚2和4之间的1-10M电阻,在引脚4上连接杜邦线(触摸端)
unsigned long csSum;

void setup() {
    
    
  Serial.begin(9600);
}

void loop() {
    
    
  CSread();
}

void CSread() {
    
    
  long cs = cs_2_4.capacitiveSensor(120); //a: 传感器分辨率设置为120
  if (cs > 100) {
    
     //b: 任意数
    csSum += cs;
    Serial.println(cs);
    if (csSum >= 3600) //c: 此值是阈值,高值表示触发时间更长
    {
    
    
      Serial.print("Trigger: ");//串口输出触发数值
      Serial.println(csSum);
      if (csSum > 0) {
    
    
        csSum = 0;  //重置
      }
      cs_2_4.reset_CS_AutoCal(); //停止读取
    }
  }
  else {
    
    
    csSum = 0; //读数错误导致超时
  }
}

3. Experimental serial port output

insert image description here

4. Experimental scene diagram
insert image description here

5. Explanation of the experiment -
In addition to receiving digital signals from digital ports, the only analog physical quantity that Arduino can detect is voltage. The detection value of any analog sensor is almost converted into a voltage value through related circuits, and then input to the analog port of arduino for analog-to-digital conversion. The capacitance value needs to be converted into a voltage value by a relatively more complicated and expensive circuit before it can be detected by Arduino, and the detection of many physical processes can be easily and reliably realized by detecting the capacitance value. The most commonly used place is touch sensor. The popular MaKey MaKey is an example. Here, through the CapacitiveSensor library, a large resistor, a wire and two ports are used, and the capacitive touch trigger method that does not require other components. The idea of ​​this method is to first set a digital port to a low potential, and turn on the internal pull-up resistor of the arduino, and start to calculate the time required for this port to reach a high potential. And this time is related to the capacitance value of this port to ground, the larger the capacitance, the longer the time. In hardware, only one wire needs to be connected to one port. Touching the bare end of this wire with a finger will cause a change in capacitance, and the arduino can detect this change through the above method. If you want to increase the sensitivity, you can connect a piece of tin foil to the wire. In order to prevent the chip from being broken down by strong static electricity on your hands, you can cover the tin foil with a thin layer of insulating paper.

6. The CapacitiveSensor library contains three main methods and some utility methods——
(1) CapacitiveSensor CapacitiveSensor(byte sendPin, byte receivePin)
CapacitiveSensor creates an instance of the library (note the uppercase letters, which are different from the methods below)

(2) long capacitiveSensorRaw(byte samples)
CapacitySensorRaw requires a parameter sample, and returns a long integer containing absolute capacitance in arbitrary units. The samples parameter can be used to increase the resolution returned, at the expense of performance. The returned value will not be averaged over the sample size, and the total value will be reported. CapacitySensorRaw will return -2 if the capacitance value exceeds the value of CS_Timeout_Millis in milliseconds. The default value of CS_Timeout_Millis is 2000 milliseconds (2 seconds).

(3) long capacitiveSensor(byte samples)
CapacitySensor takes a parameter, samples it, and returns a long integer containing the additive (sensing) capacitance in arbitrary units. The CapacitySensor keeps track of the lowest baseline (undetected) capacitance and subtracts that value from the detected capacitance, so a lower value should be reported in the case of no detection. Recalibrate the baseline value at intervals determined by CS_Autocal_Millis. The default is 200000 milliseconds (20 seconds). This recalibration can be turned off by setting CS_Autocal_Millis to a higher value using the set_CS_AutocaL_Millis() method.

(4) void set_CS_Timeout_Millis(unsigned long timeout_millis)
The set_CS_Timeout_Millis method can be used to set the CS_Timeout_Millis value, which determines how long the method will take if the receive (detection) pin fails to switch in the same direction as the transmit pin. The timeout is required because the while loop will lock the sketch unless a timeout is provided. The default value of CS_Timeout_Millis is 2000 milliseconds (2 seconds).

(5) void reset_CS_AutoCal()
reset_CS_AutoCal can be used to force immediate calibration of the capacitive sensor function.

(6) void set_CS_AutocaL_Millis(unsigned long autoCal_millis)
The method set_CS_AutocaL_Millis(unsigned long autoCal_millis) can be used to set the timeout interval of the capacitive sensor function. Recalibration can be turned off by setting CS_AutocaL_Millis to "0xFFFFFFFF" with set_CS_AutocaL_Millis.

7. Experimental scene diagram

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132223069