Arduino enabled security system
Project:
Using an Arduino, a simple security alert system can be built with a magnetic reed switch and a buzzer. When the door opens, it breaks the magnetic connection, prompting the Arduino to activate the buzzer as an alarm. Here's a brief overview of the steps involved:
Attach a red cable from the Arduino's 5V pin to the breadboard's positive rail. Link a black cable from the Arduino's GND pin to the breadboard's negative rail. Settings are as follows:
- Buzzer: Pin 7
- For the Ultrasonic Sensor:
- Echo: Pin 3
- Trig: Pin 2
- For LEDs:
- RedLED: Pin 4
- YellowLED: Pin 5
- GreenLED: Pin 6
The green cables tied to the LEDs should align with the LEDs' positive terminal. The LEDs' negative terminal should link to the breadboard's negative rail via a 220 ohm resistance.
Link the Arduino's 5V pin to the breadboard's positive rail and the GND pin to its negative rail. Remember to correctly align the 5V wire with the positive rail and the GND wire with the negative one.
For optimal positioning, mount the HC-SR04 ultrasonic sensor to the far right of the breadboard ensuring it faces outward. Link the sensor's GND pin to the breadboard's negative channel, the Trig pin to the Arduino's pin 2, the Echo pin to pin 3 on the Arduino, and the VCC pin to the breadboard's positive channel.
Proceed by linking the LEDs to both the breadboard and the Arduino. Begin by linking the Green LED's longer leg (anode) to the Arduino's pin 6 using a green cable and its shorter leg (cathode) to the breadboard's negative lane with a 220 ohm resistor. Follow this procedure for the Yellow LED (anode to pin 5) and the Red LED (anode to pin 6). While you can operate without resistors, their inclusion is advised for optimal performance.
Start by connecting the LEDs to the Arduino and breadboard. Connect the Green LED's anode (longer leg) to Arduino's pin 6 with a green wire and its cathode (shorter leg) to the breadboard's ground through a 220-ohm resistor. Repeat this with the Yellow LED connecting to pin 5 and the Red LED to pin 6. It's strongly advised to connect the buzzer's shorter leg to the breadboard's negative channel using a resistor, since this not only lowers the buzzer's volume but also extends its lifespan.
Parts and Materials:
Code:
#define triggerPin 8
#define receiverPin 9
#define redLED 10
#define yellowLED 11
#define greenLED 12
#define alarmBuzzer 13
int beepFrequency = 500;
void setup() {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(receiverPin, INPUT);
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(alarmBuzzer, OUTPUT);
}
void loop() {
long pulseDuration, measuredDistance;
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pulseDuration = pulseIn(receiverPin, HIGH);
measuredDistance = (pulseDuration / 2) / 29.1;
if (measuredDistance < 50) {
digitalWrite(greenLED, HIGH);
} else {
digitalWrite(greenLED, LOW);
}
if (measuredDistance < 20) {
digitalWrite(yellowLED, HIGH);
} else {
digitalWrite(yellowLED, LOW);
}
if (measuredDistance < 5) {
digitalWrite(redLED, HIGH);
beepFrequency = 1000;
} else {
digitalWrite(redLED, LOW);
}
if (measuredDistance > 5 || measuredDistance <= 0) {
Serial.println("Out of effective measurement range");
noTone(alarmBuzzer);
} else {
Serial.print(measuredDistance);
Serial.println(" cm");
tone(alarmBuzzer, beepFrequency);
}
delay(500);
}
Explaination:
Variable Definitions:
#define triggerPin 8
#define receiverPin 9
#define redLED 10
#define yellowLED 11
#define greenLED 12
#define alarmBuzzer 13
int beepFrequency = 500;
These lines define names for the pins being used. Instead of remembering that, say, pin 10 is for the red LED, you can simply use redLED. This makes the code more readable. The beepFrequency variable will be used to determine the frequency of the sound emitted by the buzzer.
Initialization in setup():
The setup() function initializes the required pins for input or output:
pinMode(triggerPin, OUTPUT);
pinMode(receiverPin, INPUT);
triggerPin sends out an ultrasonic wave, and receiverPin receives the echo.
The loop():
This is the part of the code that runs continuously after the setup() function.
long pulseDuration, measuredDistance;
Here, two variables are declared:
-
pulseDuration will store the time taken for the ultrasonic wave to travel out and back.
-
measuredDistance will store the calculated distance based on pulseDuration.
The next few lines send out the ultrasonic wave:
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
This sequence is necessary to ensure a clear signal.
Next, the time for the ultrasonic pulse to travel out and return is measured:
pulseDuration = pulseIn(receiverPin, HIGH);
The actual distance to the object is then calculated:
measuredDistance = (pulseDuration / 2) / 29.1;
This formula derives the distance based on the speed of sound in air.
The following conditional blocks control the LEDs:
if (measuredDistance < 50) {...}
if (measuredDistance < 20) {...}
if (measuredDistance < 5) {...}
-
If an object is detected within 50 cm, the green LED turns on.
-
If the object is within 20 cm, the yellow LED turns on.
-
If the object is within 5 cm, the red LED turns on and the beep frequency is set to 1000.
Finally, this part handles the buzzer and serial output:
if (measuredDistance > 5 || measuredDistance <= 0) {...}
else {...}
-
If the object's distance is out of range or an invalid measurement is detected, a message is printed to the serial monitor, and the buzzer is turned off.
-
Otherwise, the measured distance is printed, and the buzzer sounds at the determined frequency.
Lastly, there's a short delay before the loop runs again:
delay(500);
The Arduino employs an ultrasonic sensor to determine the distance of an object, activating various LEDs and triggering a buzzer depending on how close the object is.
Circuit Diagram: