Devices & Components
Arduino Uno Rev3
Buzzer
Jumper wires (generic)
Breadboard (generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Software & Tools
Arduino IDE
Project description
Code
code for alarm system
c_cpp
copy and paste it in the aurdino ide
1int trigPin = 9;//first we should the pins 2int echoPin = 10; 3int buzzer = 7; 4void setup() { 5 Serial.begin (9600);//to print on serial moniter we should start the serial baud 6 pinMode(trigPin, OUTPUT);//we should declare trigpin as output and echo pin as input 7 pinMode(echoPin, INPUT); 8 pinMode(buzzer, OUTPUT); 9} 10 11void loop() { 12 //block of code used to find the distance of an object in cm 13 long duration, distance; 14 digitalWrite(trigPin, LOW); 15 delayMicroseconds(2); 16 digitalWrite(trigPin, HIGH); 17 delayMicroseconds(10); 18 digitalWrite(trigPin, LOW); 19 duration = pulseIn(echoPin, HIGH); 20 distance = (duration / 2) / 29.1; 21 22 if (distance <= 10)// if the distance is less than 10 or = 10 23 //than the buzzer will make sound 24 { 25 digitalWrite(buzzer,HIGH); 26 Serial.print(distance);//to print the distance in serial moniter 27 Serial.println(""); 28 } 29 else { 30 digitalWrite(buzzer,LOW);//else it will turn off 31 Serial.print(distance);//to print the distance in serial moniter 32 Serial.println(""); 33 } 34 delay(10); 35}
Downloadable files
schematic for alarm system
schematic for alarm system

schematic for alarm system
schematic for alarm system

Comments
Only logged in users can leave comments