厂家说明 https://docs.particle.io/assets/datasheets/makerkit/pir-sensor.pdf
一、物料
二、接线
蜂鸣器 长针接GPIO4针脚,对应D2,短针接G
Led 长针接GPIO5 ,对应D1,短针接3V
HC-SR501 中间接GPIO13 对应D7,左侧(针脚边缘靠近身体)接3V,右侧(针脚边缘靠近身体)接G
三、源码
int inputPin = 13; // ESP8266 d7
int ledOut = 5; //esp8266 D1
//通过PWM脉冲调制控制(无源)蜂鸣器电压的连续改变,实现蜂鸣器的鸣响
#define buzzerOut 4 //esp8266 D2
void setup() {
pinMode(inputPin, INPUT);
pinMode(buzzerOut, OUTPUT); //蜂鸣器
pinMode(ledOut, OUTPUT); //led 二极管
Serial.begin(9600);
digitalWrite(buzzerOut, LOW);
digitalWrite(ledOut, LOW);
delay(30 * 1000);
}
void loop() {
int val = digitalRead(inputPin);
if (val == HIGH) {
mingX();
Serial.println("Motion detected!");
delay(5000);
}
else {
Serial.println("No Motion detected!");
}
delay(100);
}
void mingX() {
digitalWrite(buzzerOut, HIGH);//输出HIGH电平,停止发声
digitalWrite(ledOut, HIGH); //led on
delay(50); //等待3000毫秒
digitalWrite(buzzerOut, LOW);//输出LOW电平,发声
digitalWrite(ledOut, LOW); //led on
}
本文链接:https://blog.nnwk.net/article/134
有问题请留言。版权所有,转载请在显眼位置处保留文章出处,并留下原文连接
Leave your question and I'll get back to you as soon as I see it. All rights reserved. Please keep the source and links
友情链接:
子卿全栈
全部评论