Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

MengFanjun的博客

一、电路连接

PIR运动检测器

PIR运动检测器引脚 Arduino引脚
VCC 5V
GND GND
OUT A0(14)
在这里插入图片描述

LCD1602IIC

LCD1602IIC引脚 Arduino引脚
VCC 5V
GND GND
SDA A4
SCL A5

我这里的LCD1602是IIC的,所以只需要4根线

在这里插入图片描述

LED

LED引脚 Arduino引脚
正极 7
阴极 GND

二、代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Wire.h> 
#include <LiquidCrystal_I2C.h> //引用I2C库

//设置LCD1602设备地址,这里的地址是0x27,一般是0x20,或者0x27,具体看模块手册
LiquidCrystal_I2C lcd(0x27,16,2); //这里是0x27

#define PIR_sensor 14
#define ledpin 7


void setup()
{
pinMode(PIR_sensor, INPUT);
lcd.init(); // 初始化LCD
lcd.backlight(); //设置LCD背景等亮

lcd.setCursor(0,0);
lcd.print(" Automatic ");
lcd.setCursor(0,1);
lcd.print(" Lights opener ");
delay(3000);
lcd.clear();

}

void loop()
{
if(digitalRead(PIR_sensor))
{
lcd.setCursor(0,0);
lcd.print("Movement Detected");
lcd.setCursor(0, 1);
lcd.print(" Lights On ");
digitalWrite(ledpin, HIGH);
delay(1000);
lcd.clear();
lcd.print(" Lights Off ");
digitalWrite(ledpin, LOW);
delay(1000);
}
else
{
lcd.setCursor(0,0);
lcd.print(" No Movement ");
lcd.setCursor(0,1);
lcd.print(" Lights Off ");
digitalWrite(ledpin, LOW);

}
}

三、效果

请添加图片描述

评论