Key unlock based on wemos d1

Today atarduino platformMade aKey unlock, The actual code is just a few lines. I also summarize here

1. The hardware used is mainlyFour:

1. Wemos d1 board
Insert picture description here
2. Relay
Insert picture description here
3. Single touch sensor
Insert picture description here
Insert picture description here
4. Lock
Insert picture description here

2. Development environment

The development environment isarduino, I won’t say more here.

Three, wiring

Insert picture description here

4. Observe the difference between buttons and no buttons through the serial port.

#define ZHIWEN D2 //定义按键引脚
#define RELAY  D8 //定义继电器引脚
int data; //定义data接收数据
void setup() {
    
    
  Serial.begin(115200); //设置波特率
  pinMode(ZHIWEN,INPUT);//设置按键模块为输入引脚
  pinMode(RELAY,OUTPUT);//设置继电器为输出引脚
  digitalWrite(RELAY,HIGH); //初始化引脚为高电平(关闭继电器)
}

void loop() {
    
    
  data = analogRead(ZHIWEN); //读取数据
  Serial.println(data); //在 串口打印出读取到的数据
        
  }
   
     

phenomenon

1. The state of the serial port when there is no button

Insert picture description here

2. The data of the serial port when the key is pressed
Insert picture description here

Five, summarize the conclusion and program the key to unlock

First of all, we found that when we do not press the key, the printed value is 0, and when we press the key, the value is 1023. Then we can use this to control the unlocking and closing of our keys.

The following is the code

#define ZHIWEN D2
#define RELAY  D8
int data;
void setup() {
    
    
  Serial.begin(115200); //设置波特率
  pinMode(ZHIWEN,INPUT);//设置按键模块为输入引脚
  pinMode(RELAY,OUTPUT);//设置继电器为输出引脚
  digitalWrite(RELAY,HIGH); //初始化引脚为高电平(关闭继电器)
}

void loop() {
    
    
  data = analogRead(ZHIWEN); //读取按键状态数据
  switch(data)
  {
    
    
      case 0:
        Serial.println("close");
        digitalWrite(RELAY,HIGH); //关闭继电器(关锁) 
        break;
      case 1023:
       Serial.println("open");
       digitalWrite(RELAY,LOW); //打开继电器(开锁)
       delay(1000); //开锁后延时1秒
       break;
        
  }
   

The above is a gadget I played today. Sometimes a simple gadget can increase confidence, increase fun, and sense of accomplishment. The prospect is to play more powerful modules in the future, and the technology will continue to improve. I want to make smart homes, etc.

Guess you like

Origin blog.csdn.net/weixin_47457689/article/details/109774466