搜索代码

使用HC-SR501运动传感器、继电器和Arduino代码

使用HC-SR501运动传感器、继电器和Arduino代码

在本教程中,我们将探讨如何将 HC-SR501 移动传感器与继电器结合使用,以通过 Arduino 控制一个交流灯泡或其他类型的负载。HC-SR501 是一个红外移动传感器,能在最远 7 米的距离内检测到运动,非常适合用于安全报警或自动照明系统等各种应用。通过利用这个传感器和继电器,您可以创建一个在检测到运动时自动开启灯光的项目。

我们将提供一个有关硬件设置、接线说明和实现此功能所需的Arduino代码的逐步指南。为了更清楚地理解这一过程,请务必观看相关视频(视频在00:00)。

硬件解析

该项目的主要组件包括HC-SR501运动传感器、继电器模块和Arduino板。HC-SR501传感器有三个引脚:VCC、GND和OUT。VCC引脚连接到电源(5V),GND引脚连接到地,OUT引脚在检测到运动时向Arduino发送信号。

继电器模块充当开关,可以控制高压设备。它有三个主要引脚:COM(公共)、NO(常开)和NC(常闭)。在此设置中,我们将使用NO引脚,当继电器被激活时,它连接到负载。这使得Arduino能够根据运动传感器收到的信号来控制继电器。

数据表详情

制造商HC-SR501
部件号HC-SR501
逻辑/输入输出电压5 V
供电电压5-20 伏
电力消耗65 毫安
检测角度120度
检测距离3-7 米
操作温度-15 到 70 °C
输出类型数字
包裹模块

  • 确保传感器的电源供应适当(5-20 伏)。
  • 调整灵敏度电位器以获得最佳探测范围。
  • 使用继电器的常开触点有效地控制负载。
  • 注意继电器的电流额定值(最大10 A)。
  • 在最终安装之前测试运动传感器的检测范围。
  • 如果在高负载下使用继电器,请考虑散热问题。

接线说明

Arduino wiring for HC-SR501 Motions sensor
Arduino wiring for HC-SR501 Motions sensor

要将HC-SR501运动传感器和继电器接线到Arduino,首先将运动传感器的VCC引脚连接到Arduino上的5V引脚。接下来,将传感器的GND引脚连接到Arduino上的一个GND引脚。运动传感器的OUT引脚应连接到Arduino上的数字引脚2。

对于继电器模块,将VCC引脚连接到Arduino的5V引脚,将GND引脚连接到地面。继电器的输入引脚(通常标记为IN或类似标记)应连接到Arduino的数字引脚4。确保检查继电器的方向,以确保它设置为常开(NO)以便于此设置。最后,根据继电器的规格,将负载(例如,交流灯泡)连接到继电器,确保连接稳固。

代码示例和演示

在Arduino代码中,我们为传感器和继电器引脚定义常量。传感器引脚设置为SENSOR_PIN分配给数字引脚2,继电器引脚设置为RELAY_PIN,分配给数字引脚4。此配置使我们能够读取传感器的输出并相应地控制继电器。

const int SENSOR_PIN = 2; // the Arduino pin connected to the output of the sensor
const int RELAY_PIN = 4; // the Arduino pin which is connected to control the relay

setup()在该函数中,我们初始化串行监视器以进行调试,并设置传感器和继电器引脚的引脚模式。这确保了Arduino知道哪些引脚是输入,哪些引脚是输出。

void setup() {
  Serial.begin(9600); // setup Serial Monitor to display information
  pinMode(SENSOR_PIN, INPUT); // Define SENSOR_PIN as Input from sensor
  pinMode(RELAY_PIN, OUTPUT); // Define RELAY_PIN as OUTPUT for relay
}

loop()函数中,我们持续检查运动传感器的状态。如果检测到运动,我们会向串口监视器打印一条消息,并通过将其设置为低电平来打开继电器。如果没有检测到运动,我们会打印另一条消息,并通过将其设置为高电平来关闭继电器。

void loop() {
  int motion = digitalRead(SENSOR_PIN); // read the sensor pin
  if (motion) {
    Serial.println("Motion detected");
    digitalWrite(RELAY_PIN, LOW); // Turn the relay ON
  } else {
    Serial.println("===Nothing moves");
    digitalWrite(RELAY_PIN, HIGH); // Turn the relay OFF
  }
  delay(500);
}

演示 / 期望内容

当运动传感器检测到运动时,继电器将被激活,使电源能够流向连接的负载,例如一个交流电灯泡。您应该能够在检测到运动时立即看到灯泡点亮。如果传感器没有检测到任何移动,灯泡将保持关闭。请注意,传感器的灵敏度设置会影响其检测范围,因此可能需要进行调整以获得最佳性能(视频中在05:00)。

常见的陷阱包括接线不正确或超过继电器的电流限制,这可能导致故障。确保所有连接稳固,并在最终使用之前在受控环境中测试设置。

视频时间戳

  • 00:00 开始
  • 00:35 硬件解释
  • 04:35 继电器功率额定值
  • 06:00 接线说明
  • 07:53 Arduino 代码解析
  • 09:48 本项目的演示

图像

HC-SR501 back
HC-SR501 back
HC-SR501 module
HC-SR501 module
Arduino wiring for HC-SR501 Motions sensor
Arduino wiring for HC-SR501 Motions sensor
141-Arduino source code for an HC-SR501 motion sensor to control an AC bulb or load
语言: C++
/*
 * This Arduino code uses an HS-SR501 motion sensor and a relay
 * and switches an alarm or a device using the relay when motion is detected.
 * The device can be an alarm, a light, or any other device.
 *

 * Written by Ahmad Shamshiri on August 5, 2018, at 12:36 PM for Robojax.com
 * in Ajax, Ontario, Canada
 * Watch the video instruction for this code: https://youtu.be/QQ7iDagQnQM
 * This code has been downloaded from Robojax.com

 */

const int SENSOR_PIN = 2;// the Arduino pin connected to the output (middle) wire of the sensor
const int RELAY_PIN = 4;// the Arduino pin which is connected to control the relay

void setup() {
  Serial.begin(9600);// setup Serial Monitor to display information
   Serial.println("Robojax Arduino Tutorial");
   Serial.println("HC-SR501 sensor with relay");
  pinMode(SENSOR_PIN, INPUT);// Define SENSOR_PIN as Input from sensor
  pinMode(RELAY_PIN, OUTPUT);// Define RELAY_PIN as OUTPUT for relay
}

void loop() {
  // Robojax.com HC-SR501 motion sensor tutorial August 5, 2018
  int motion =digitalRead(SENSOR_PIN);// read the sensor pin and stores it in "motion" variable
 
  // if motion is detected
  if(motion){
    Serial.println("Motion detected");
    digitalWrite(RELAY_PIN, LOW);// Turn the relay ON
  }else{
     Serial.println("===Nothing moves");
     digitalWrite(RELAY_PIN,HIGH);// Turn the relay OFF
  }
  delay(500);
  // Robojax.com HC-SR501 motion sensor tutorial August 5, 2018
}

资源与参考

尚无可用资源。

文件📁

没有可用的文件。