问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Arduino门传感器入门教程:硬件连接与编程实战

创作时间:
作者:
@小白创作中心

Arduino门传感器入门教程:硬件连接与编程实战

引用
CSDN
1.
https://blog.csdn.net/acktomas/article/details/139945324

门传感器广泛应用于安防领域,用于检测门窗的开关状态。本文将详细介绍如何使用Arduino来检测门的开关状态,并通过编程实现对门开关事件的监测。

Arduino - Door Sensor, Arduino - 门传感器

Arduino - 门传感器
The door sensor is widely used in security area, It is used to detect/monitor entrances (such as door, window …). The door sensor is also known as “entry sensors”, “contact sensors”, or “window sensors”.
门传感器广泛用于安防领域,用于检测/监控入口(如门、窗等)。门传感器也称为“进入传感器”、“接触传感器”或“车窗传感器”。

About Door Sensor 关于门传感器

Pinout 引脚排列

Door sensor includes two components:
门传感器包括两个组件:

  • One reed switch with two pins
    一个带两个引脚的簧片开关
  • One magnet 一块磁铁

Just like a normal switch/button, we do NOT need to distinguish the two pins of the reed switch.
就像普通的开关/按钮一样,我们不需要区分簧片开关的两个引脚。

How It Works 它是如何工作的

The magnet is attached to the door/window (moving part), and the reed switch is attached to the door frame (fixed part). The two components are in contact when the door is closed
磁铁固定在门窗上(运动部分),簧片开关固定在门框上(固定部分)。当门关闭时,这两个组件是接触的

  • When the magnet is close to the reed switch, the reed switch circuit is closed
    当磁铁靠近簧片开关时,簧片开关电路闭合

  • When the magnet is far from the reed switch, the reed switch circuit is open
    当磁铁远离簧片开关时,簧片开关电路开路

※ NOTE THAT: ※ 注意事项:

The reed switch itself does NOT outputLOWorHIGHon its pins. It is just a closed or open state. Depending on how we connect its pins to Arduino, the value on the pin can be theLOW,HIGH, or floating value (unpredicted value). To avoid the floating value, we MUST use the pull-up or pull-down resistor on the Arduino pin
簧片开关本身在其引脚上不输出低电平或高电平。它只是一个封闭或开放的状态。根据我们将其引脚连接到Arduino的方式,引脚上的值可以是LOW,HIGH或浮点值(不可预测的值)。为了避免浮点,我们必须在Arduino引脚上使用上拉或下拉电阻

If we connect a pin of the reed switch toGND, the other pin of the reed switch to Arduino’s input pin with a pull-up resistor (internal or external):
如果我们将簧片开关的一个引脚连接到 GND,则簧片开关的另一个引脚带有上拉电阻器(内部或外部)连接到 Arduino 的输入引脚:

  • When the magnet is closed to the reed switch, the value in Arduino’s input pin isLOW
    当磁铁闭合到簧片开关时,Arduino输入引脚中的值为LOW

  • When the magnet is far from the reed switch, the value in Arduino’s input pin isHIGH
    当磁铁远离簧片开关时,Arduino输入引脚中的值为HIGH

So:

  • To check the state of the door, we just check the state of Arduino’s input pin:
    要检查门的状态,我们只需检查Arduino输入引脚的状态:

  • If the state isLOW, the door is closed
    如果状态为 LOW,则门关闭

  • If the state isHIGH, the door is open
    如果状态为 HIGH,则门是打开的

  • To detect the door-opening/door-closing events, we can detect the state change on the Arduino input pin:
    为了检测开门/关门事件,我们可以检测Arduino输入引脚上的状态变化:

  • If the state changes fromLOWtoHIGH, the door-opening event is detected
    如果状态从 LOW 变为 HIGH,则检测到开门事件

  • If the state changes fromHIGHtoLOW, the door-closing event is detected
    如果状态从“高”变为“低”,则检测到关门事件

Wiring Diagram 接线图

This image is created usingFritzing. Click to enlarge image
此图像是使用 Fritzing 创建的。点击放大图片

How To Program For Door Sensor 如何对门传感器进行编程

  • Initializes the Arduino pin to the digital input mode by usingpinMode()function. For example, pin 13
    使用 pinMode() 函数将 Arduino 引脚初始化为数字输入模式。例如,引脚 13

pinMode(13, INPUT_PULLUP);  
  • Reads the state of the Arduino pin by usingdigitalRead()function.
    使用 digitalRead() 函数读取 Arduino 引脚的状态。

int doorState  = digitalRead(13);  

Arduino Code - Check Door Open and Close State Arduino代码 - 检查门打开和关闭状态


/*
 * Created by ArduinoGetStarted.com
   *
 * This example code is in the public domain
   *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-door-sensor
   */
const int DOOR_SENSOR_PIN = 13; // Arduino pin connected to door sensor's pin
int doorState;
void setup() {
  Serial.begin(9600);                     // initialize serial
  pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
}
void loop() {
  doorState = digitalRead(DOOR_SENSOR_PIN); // read state
  if (doorState == HIGH) {
    Serial.println("The door is open");
  } else {
    Serial.println("The door is closed");
  }
}

Arduino Code - Detect Door-opening and Door-closing Events Arduino代码 - 检测开门和关门事件


/*
 * Created by ArduinoGetStarted.com
   *
 * This example code is in the public domain
   *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-door-sensor
   */
const int DOOR_SENSOR_PIN = 13;  // Arduino pin connected to door sensor's pin
int currentDoorState; // current state of door sensor
int lastDoorState;    // previous state of door sensor
void setup() {
  Serial.begin(9600);                     // initialize serial
  pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read state
}
void loop() {
  lastDoorState = currentDoorState;              // save the last state
  currentDoorState  = digitalRead(DOOR_SENSOR_PIN); // read new state
  if (lastDoorState == LOW && currentDoorState == HIGH) { // state change: LOW -> HIGH
    Serial.println("The door-opening event is detected");
    // TODO: turn on alarm, light or send notification ...
  }
  else
  if (lastDoorState == HIGH && currentDoorState == LOW) { // state change: HIGH -> LOW
    Serial.println("The door-closing event is detected");
    // TODO: turn off alarm, light or send notification ...
  }
}

Video Tutorial 视频教程

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to ourYouTube channelto give us motivation for making the videos.
我们正在考虑制作视频教程。如果您认为视频教程是必不可少的,请订阅我们的 YouTube 频道,为我们制作视频提供动力。

Function References 函数参考

  • pinMode()
  • digitalRead()
  • Serial

See Also 另见

  • Arduino - Door Sensor - LED
    Arduino - 门传感器 - LED

  • Arduino - Door Sensor - Relay
    Arduino - 门传感器 - 继电器

  • Arduino - Door Sensor Toggle LED
    Arduino - 门传感器切换 LED

  • Arduino - Door Sensor Toggle Relay
    Arduino - 门传感器拨动继电器

  • Arduino - Door Sensor - Piezo Buzzer
    Arduino - 门传感器 - 压电蜂鸣器

  • Arduino - Door Sensor - Servo Motor
    Arduino - 门传感器 - 伺服电机

  • Arduino - Door Open Email Notification
    Arduino - 开门电子邮件通知

  • Arduino - Door Open - Send Email Notification
    Arduino - 开门 - 发送电子邮件通知

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号