top of page
Post: Blog2 Post
Search

How to make a Security Alarm using an Adruino - Pt. 1

In this project we will be making a Security Alarm using an Arduino. This is a part one, which means this it will only consist the circuit and a working code. Part two will consist of the design and the circuit implemented in it.


Materials

  1. Arduino Uno

  2. Breadboard (any size)

  3. Ultrasonic Sensor

  4. Buzzer

  5. Button

  6. Resistor - 220 Ohm

  7. Jumper Wires

  8. Battery/Cable Source


If you don't have any of these materials, you can buy the kit in our shop :


Software


Circuit


In this circuit we want to read the value of the Ultrasonic Sensor (distance sensor), and react with it using the buzzer. So first we want to connect the sensor to the breadboard so it helps us better placing the sensor. Ultrasonic sensors work by emitting sound waves at a frequency too high for humans to hear. They then wait for the sound to be reflected back, calculating distance based on the time required. This is similar to how radar measures the time it takes a radio wave to return after hitting an object. We want to connect the trigPin to pin number 9 and the echoPin is connected to pin number 10. These two pins are how we are going to get our data from the sensor.





Programming


In this code our goal is to receive data from the Ultrasonic Sensor and react with the alarm, buzzer in our case. So we started off with declaring 5 variables, trigPin and echoPin are for the Ultrasonic Sensor, the buzzer is for our alarm and we are going to use the button for our turning off the alarm. Next we declared another variable for the button, which will read the button constantly if it ever gets pressed. And two more variables to help us convert and save the data from the Ultrasonic Sensor. Then we created a function that helps us repeat the alarm code going on and off. To do that we set the tone at 600 for 0.1 seconds (100) and turned it off for 0.1 seconds (100). Finally we setup the inputs, outputs and the serial monitor.



In the loop, we also started with setting up the Ultrasonic sensor, changing the data from the Ultrasonic sensor to a reasonable value and printing it to the serial monitor to help us see the distance at all times. The we created an if statement that will help us turn on the alarm. The if statement says, if distance is less than or equal to 40 then the action in the curly bracket happens . Inside we have a repeat until loop that will repeat until something happened. In this case we want it to repeat the alarm sound until the button is pressed. Inside the repeat until loop we have the Alarm function that we created earlier and a digitalRead command that will constantly read if the button is pressed. After it's pressed the loop will start again and the Ultrasonic sensor will start sensing. And it will keep on going.


Code :


const int trigPin = 9;
const int echoPin = 10;
const int Buzzer = 3;
const int buttonPin = 2;     

int buttonState = 0;

long duration;
int distance;

void Alarm() {
  tone(Buzzer,600);
  delay(100);
  noTone(Buzzer);
  delay(100);
}

void setup() 
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(Buzzer, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() 
{
  digitalWrite(trigPin, LOW);
  delay(25);
  digitalWrite(trigPin, HIGH);
  delay(25);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
  Serial.print("Distance: ");
  Serial.println(distance);

   buttonState = digitalRead(buttonPin);


    if (distance <= 40){
      
      while (!(buttonState == HIGH)) {
      buttonState = digitalRead(buttonPin);
      Alarm();
      }
      
    }
     
}



After finishing writing the code, download it using the the USB cable to download it to the Arduino. Before that you have to make sure the board type is the right one and it's setup to the right port. To do that go to the tools section choose the board type and port. In this case we are using an Arduino Uno. Finally connect the cable and see what happens. See the image below :




  • Make sure to like the post

  • Comment down below if it worked for you and what other projects you want me to keep doing!



You can find the forum for this project here : Security Alarm

bottom of page