Devices & Components
Buzzer, Piezo
Jumper wires (generic)
PIR Sensor, 7 m
Hardware & Tools
Breadboard, 170 Pin
Project description
Code
My Coding Project
arduino
Using a pir motion sensor, i created an alarm that detects motion which sets off a buzzer and an LED
1int ledPin = 13; 2int nxtPinA = 11; 3int nxtPinB = 9; // choose 4 the pin for the LED 5int inputPin = 2; // choose the input pin (for 6 PIR sensor) 7int pirState = LOW; // we start, assuming no motion detected 8int 9 val = 2; 10int piezoPin = 12; 11 //Variable to store analog value (0-1023) 12 13 // variable for reading the pin status 14 15void setup() { 16 pinMode(ledPin, 17 OUTPUT); 18 pinMode(piezoPin, OUTPUT); 19 pinMode(nxtPinA, OUTPUT); 20 pinMode(nxtPinB, 21 OUTPUT);// declare LED as output 22 pinMode(inputPin, INPUT); // declare sensor 23 as input 24 25 Serial.begin(9600); 26 27} 28 29void loop(){ 30 val = 31 digitalRead(inputPin); // read input value 32 if (val == HIGH) { // 33 check if the input is HIGH 34 digitalWrite(ledPin, HIGH); 35 digitalWrite(nxtPinA, 36 HIGH); 37 digitalWrite(nxtPinB, HIGH); 38 digitalWrite(ledPin, HIGH); 39 40 tone(piezoPin, 1000, 500); 41 // turn LED ON 42 if (pirState == LOW) 43 { 44 // we have just turned on 45 Serial.println("Motion detected!"); 46 47 // We only want to print on the output change, not state 48 pirState 49 = HIGH; 50 } 51 } else { 52 digitalWrite(ledPin, LOW); 53 54 if 55 (pirState == HIGH){ 56 // we have just turned of 57 Serial.println("Motion 58 ended!"); 59 // We only want to print on the output change, not state 60 61 pirState = LOW; 62 } 63 } 64} 65
My Coding Project
arduino
Using a pir motion sensor, i created an alarm that detects motion which sets off a buzzer and an LED
1int ledPin = 13; 2int nxtPinA = 11; 3int nxtPinB = 9; // choose the pin for the LED 4int inputPin = 2; // choose the input pin (for PIR sensor) 5int pirState = LOW; // we start, assuming no motion detected 6int val = 2; 7int piezoPin = 12; 8 //Variable to store analog value (0-1023) 9 // variable for reading the pin status 10 11void setup() { 12 pinMode(ledPin, OUTPUT); 13 pinMode(piezoPin, OUTPUT); 14 pinMode(nxtPinA, OUTPUT); 15 pinMode(nxtPinB, OUTPUT);// declare LED as output 16 pinMode(inputPin, INPUT); // declare sensor as input 17 18 Serial.begin(9600); 19 20} 21 22void loop(){ 23 val = digitalRead(inputPin); // read input value 24 if (val == HIGH) { // check if the input is HIGH 25 digitalWrite(ledPin, HIGH); 26 digitalWrite(nxtPinA, HIGH); 27 digitalWrite(nxtPinB, HIGH); 28 digitalWrite(ledPin, HIGH); 29 tone(piezoPin, 1000, 500); 30 // turn LED ON 31 if (pirState == LOW) { 32 // we have just turned on 33 Serial.println("Motion detected!"); 34 // We only want to print on the output change, not state 35 pirState = HIGH; 36 } 37 } else { 38 digitalWrite(ledPin, LOW); 39 40 if (pirState == HIGH){ 41 // we have just turned of 42 Serial.println("Motion ended!"); 43 // We only want to print on the output change, not state 44 pirState = LOW; 45 } 46 } 47} 48
Downloadable files
My Coding Project
My Coding Project
Comments
Only logged in users can leave comments