今日头条
本项目BOM很简单,只有PIR传感器、蜂鸣器、LED和按钮开关几个元件,按下按钮就可关闭蜂鸣声。
首先,将Arduino UNO开发板的 +5V、GND连接到面包板。
LED的阴极接地,阳极(长引脚)通过330 or 220欧姆的上拉电阻器连接到Arduino开发板的6#引脚。
蜂鸣器的正极连接到Arduino板子的5#引脚,负极引脚接GND。
按钮开关的一个引脚通过1k欧姆电阻器接GND,另一引脚连接到Arduino的12引脚。
PIR运动传感器的+Vcc、GND、output三个引脚分别连接于Arduino开发板的+Vcc、GND、pin 7引脚。
按照上述步骤连接正确后,接下来取Arduino IDE上传代码到。再检查Serial Monitor的读数,移动手掌到传感器前面,LED将闪亮,蜂鸣器将会报警。
CODE;C/C++
// Declaring Pins
const int buzzerPin = 5;
const int ledPin = 6;
const int motionPin = 7;
const int buttonPin = 12;
// Setting Buzzer mode to False
boolean buzzer_mode = false;
// For LED
int ledState = LOW;
long previousMillis = 0;
long interval = 100; // Interval at which LED blinks
void setup()
{
//The Following are our output
pinMode(ledPin,OUTPUT);
pinMode(buzzerPin,OUTPUT);
//Button is our Input
pinMode(buttonPin, INPUT);
// Wait before starting the alarm
delay(5000);
}
void loop()
{
// To chech whether the motion is detected or not
if (digitalRead(motionPin)) {
buzzer_mode = true;
}
// If alarm mode is on,blink our LED
if (buzzer_mode){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// Switch the LED
digitalWrite(ledPin, ledState);
}
tone(buzzerPin,1000);
}
// If alarm is off
if (buzzer_mode == false) {
// No tone & LED off
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
}
// If our button is pressed Switch off ringing and Setup
int button_state = digitalRead(buttonPin);
if (button_state) {buzzer_mode = false;}
}
如果你觉得这个小制作有用处,就把它装进小盒子里,固定到窗台、门框和阳台的不显眼位置,一个属于自己的防盗警报器就诞生了。
全部0条评论
快来发表一下你的评论吧 !