Skip to main content

Want a simple but challenging project to do over the weekend? You've come to the right place. This project only took me a day to complete.

Background

I don't have a lock on my bedroom door in the flat I'm currently living in. This is quite concerning as there'd be no second layer of safety if my flat were to be broken into (which I hope will never happen of course!). Hence, I took this opportunity to develop a makeshift alarm system for myself. It's a simple problem that I'd like to fix but there is a twist on how I'd like to configure my alarm.

Acknowledgements

Before I start, a shoutout to FemEng from the University of Glasgow for giving me this opportunity to use my creativity and a channel for me to express it. I had always wanted to create my own Arduino project but never really had the time or resources to do so. Additionally, I have been given a chance to present my project to other people as well, which allows me to gain feedback and to just share my own work.

Another individual who has to be recognised is Jana Wong, the Technical Convener for the FemEng Arduino Workshop. It has been such a long time since I last used Arduino and with her help, she provided me with the basics I needed to get this project up and going. In the workshop, I actually learnt about the use of libraries and how to utilise the RGB LED which I used in this project, so kudos to Jana for teaching me about that.

Components Needed

  1. I2C Serial LCD 1602 Module x1 (257-6784)
  2. HC-SR04 Ultrasonic Distance Sensor x1 (215-3181)
  3. Potentiometer x1
  4. Piezo buzzer x1
  5. Pushbutton x1
  6. RGB LED (Common Anode) x1
  7. Male-to-male Jumper Wire x22 (791-6463)
  8. Female-to-male Jumper Wire x4 (791-6454)
  9. 330Ω Resistor x3
  10. 10kΩ Resistor x1
  11. Breadboard x2 (215-3175)
  12. Arduino UNO x1 (715-4081)
  13. USB Cable x1

Please note: In my final product I used a female-to-male jumper wire for one of the pins on my RGB LED but I advise you to use the number of components I have listed if you want to recreate this project.

Schematic

Some things to note :

  • Digital pins will be denoted as the letter D
  • Analogue pins will be denoted as the letter A

Below are the connections for each component :

Ultrasonic sensor

  • VCC to 5V
  • Trig to D6
  • Echo to D7
  • GND to GND

Buzzer

  • Positive pin to D2
  • Negative pin to GND

LCD

  • VCC to 5V
  • GNd to GND
  • SDA to A4
  • SCL to A5

Potentiometer

  • Pin 1 to GND
  • Pin 2 to A1
  • Pin 3 to 5V

Button

  • Pin 1 to D4
  • Pin 1 (Opposite side) to 10kΩ Resistor
  • 10kΩ Resistor to GND
  • Pin 2 (Opposite side) to 5V

RGB LED

  • Pin 1 to 330Ω Resistor to D9
  • Anode (Pin 2) to GND
  • Pin 3 to 330Ω Resistor to D10
  • Pin 4 to 330Ω Resistor to D11

After connecting them, you should get a circuit similar to below :

Wiring Diagram

Discrepancies in the photo :

  1. The potentiometer used in the picture is a bit different than the one I'm using.
  2. The brown jumper wire used for LCD is white in my actual circuit.
  3. I'm using a common Anode RGB instead of a common Cathode used in the picture (I have provided a link that explains how to set it up under the references section).
  4. I tried making the connections as accurate as the real product, but I had to move around some wires so that it doesn't look as confusing in the photo.
  5. Some connections on the components aren't in the same order as the ones I'm using so please be aware of that.

Code

A short explanation of my code :

  1. Check if the system is OFF, if it is then check the state of button.
  2. If the button is pressed, display that the alarm is activated, set alarm flag as TRUE (means alarm is ON).
  3. Check if alarm flag is TRUE, if it is, then turn on the sensor.
  4. Read values from the sensor, calculate the distance of object from sensor.
  5. If an object is less than or at a distance of 5cm from sensor, set intruder flag as TRUE. Turn on buzzer. Display warning that there is an intruder.
    1. Ask for password. Keep buzzer on as long as the wrong password is entered.
    2. Green and blue LED flashes mean that first code is about to be read.
    3. For each subsequent change of LED colour (Green and blue only) indicates the correct password entered.
    4. Once the full correct password is entered, disarm alarm. Set alarm flag and intruder as FALSE.
  6. If object is further than 5cm, password can be entered to disarm the alarm.
    1. Ask for password once.
    2. If no correct password entered, continue reading values from sensor.
  7. The whole process repeats infinitely.

Whole code :

//Import all relevant libraries, libraries can be downloaded in the link provided
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

//Assign pin numbers to variables that correspond to the component
const int trig = 6, echo = 7; //Ultrasonic sensor
const int buzzer = 2, button = 4, pot = A1; //Buzzer, button & potentiometer
const int red = 9, blue = 10, green = 11; //RGB LED

//Declare data types for variables used
long duration; //For description of the data type long, it is in the references section
int distance;
int buttonState = 0; //buttonState is initially set to 0 for OFF/LOW
int potVal = 0; //Value of potentiometer is set initially to 0

//Variables below act as flags
bool alarmON = false; //Alarm is set to OFF
bool intruderIN = false; //No intruder initially

//Set address of LCD and size : characters x row
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  //Perform initialisation steps
  Wire.begin();
  lcd.begin();
  lcd.backlight();
  Serial.begin(9600); //Baud rate set to 9600, explanantion is provided in the link under the references
  //Set each pin either INPUT/OUTPUT
  //INPUTS
  pinMode(button,INPUT);
  pinMode(echo,INPUT);
  //OUTPUTS
  pinMode(trig,OUTPUT);
  pinMode(buzzer,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(blue,OUTPUT);
  pinMode(blue,OUTPUT);
}

//Function for setting up the colour of LED
void RGB(int redVal, int greenVal, int blueVal){
  //Puts in the values for each colour
  analogWrite(red, redVal); 
  analogWrite(blue, blueVal);
  analogWrite(green, greenVal);
}

//Function to display alarm is OFF
void displayOFF(){
  RGB(0,255,255); //Red to indicate system is OFF
  lcd.clear();
  lcd.print("Alarm is OFF");
  lcd.setCursor(0,1);
  lcd.print("Please activate");
}

//Function to activate alarm
bool switchON(bool alarmON){
  while(alarmON == false){ //As long as the system is OFF, it will loop
    buttonState = digitalRead(button); //Reads state of button
    if (buttonState == HIGH){ //Checks if button is pressed
      lcd.clear();
      lcd.print("Alarm is ON");
      RGB(255,255,0); //Green to indicate system is ON
      alarmON = true; //Set alarm as ON so loop is exited
    }
  }
  return alarmON;
}

//Function for password to disarm alarm
// 2,1,3,2,3 is password for now
// 1 : Range 0-340
// 2 : Range 341-680
// 3 : Range 681-1023
bool password(){
  alarmON = true;
  if (intruderIN == true){ //Only switches LED colour if there is an intruder
    RGB(255,255,0); //Green
    delay(1000);
    RGB(255,0,255); //Blue
    delay(1000);
    RGB(255,255,0); //Green
    delay(2000);
  } 
  potVal = analogRead(pot); //Reads value of potentiometer
  if (potVal>340 && potVal<680){
    RGB(255,0,255); //Blue
    delay(3000);
    potVal = analogRead(pot);
    if (potVal<340){
      RGB(255,255,0); //Green
      delay(3000);
      potVal = analogRead(pot);
      if (potVal>680){   
        RGB(255,0,255); //Blue
        delay(3000);
        potVal = analogRead(pot);
        if (potVal>340 && potVal<680){
          RGB(255,255,0); //Green
          delay(3000);
          potVal = analogRead(pot);
          if (potVal>680){
            RGB(255,0,255); //Blue
            lcd.clear();
            lcd.print("Disarmed alarm");
            noTone(buzzer); //Buzzer is turned off
            alarmON = false; //Alarm is set to OFF
            intruderIN = false; //Set as no intruder
            delay(2000);
          }
        }
      }
    }
  } 
  return alarmON;
}

//Function when alarm is triggered
bool alarmTrig(bool alarmON){
  while (alarmON == true){ //As long as alarm is ON, loop is continuous
    //Clear trig pin for 2 microseconds
    digitalWrite(trig,LOW); 
    delayMicroseconds(2);
    //Set trig pin to HIGH for 10 microseconds
    digitalWrite(trig,HIGH); 
    delayMicroseconds(10);
    digitalWrite(trig,LOW);
    //Read input from echo pin
    duration = pulseIn(echo,HIGH); 
    //Read input of echopin and set as duration in microseconds
    distance = duration*0.034/2; 
    if (distance <=5){ //Checks if there is an object within 5cm of the sensor
      tone(buzzer,1000); //Buzzer is turned on
      lcd.clear();
      lcd.print("Intruder alert!");
      lcd.setCursor(0,1);
      lcd.print("Enter passcode : ");
      intruderIN = true; //There is an intruder
      while(true){ //Loops as long as correct password is not entered
        alarmON = password();
        if (alarmON == false){
          break;
        }
      }
    }
    else {
      alarmON = password(); //Option to turn off the alarm if wanted to
    } 
  }
  return alarmON;
}
//Main function that loops infinitely
void loop() {
  //Displays that the alarm is OFF and resets everything
  displayOFF();
  //Switches ON alarm
  alarmON = switchON(alarmON);
  //Triggers alarm
  alarmON = alarmTrig(alarmON);
}
 

I've decided that I wanted to create a basic alarm system so you can definitely make it more complicated if you want to. This project can be evolved in a much more complex system that has an increased robustness.

To find more explanation on how I set the code up. I have provided multiple references for the components I used. Only the password function is fully written and originally by me. The rest are mostly taken from the websites I listed in the reference section. 

Key features that I have added to my project :

TLDR - Just read the bolded words

  1. LCD display - As with most security systems, I wanted mine to have a screen so that the system's state is displayed. It also eases the process of debugging as I can test each components' functionality with it.
  2. RGB LED - Initially, I wanted to use a couple of LEDs but, with limited wires, I decided it would be best to use an RGB LED which colours can be varied. I only used the 3 basic colours in my code because I struggle with telling the difference between multiple colours so it would be confusing to do so. The change between green and blue in the password function serves as to signify that I have entered the correct number and I know that I have a few seconds to enter the next number.
  3. Potentiometer - There are multiple ways to create a passcode, but I decided that using a potentiometer acting as a safe combination provides me a way to add more complexity in the future as well as make it harder to crack. In this project, I only divided the potentiometer into 3 different sections (so there are only 3 numbers in my "safe" combination). 
  4. Turning off the alarm when needed - I have also written the code to let me turn off my alarm if I wanted to without having to trigger it. I mean imagine having to listen to your alarm every single time you enter the room, you'd go crazy too, there's also a few seconds of delay between each passcode entered so it'd feel like an eternity before the alarm is turned off.
  5. Extra breadboard  - I segregated the breadboards according to its position if it was implemented in real life. The sensor, LED and buzzer would be placed inside my room next to the door so it can detect when the door is opened. On the other hand, the button and potentiometer would be placed outside of the door, because logically I'd activate the alarm when I'm leaving and would disarm it before entering my room.

Final Product

Please note: This is not a robust system, it will only sound an alarm but no further action is taken. It is only meant to be used to scare the "intruder" and alarm those who are nearby. Even with a password implemented, it can easily be turned off if one of the connections is severed.

Arduino Door Alarm - Final Product

Shown above is the fully finished product.

Arduino Door Alarm - Breadboard for Potentiometers 

Above is the breadboard for the potentiometer and button.

Below is the breadboard for the sensor, LED and buzzer

Arduino Door Alarm - Sensors, LED and Buzzer

Finally here's a video of it working :

Future Improvements

Here are a few things that I have in mind which would make this a better alarm system:

  1. Use Arduino UNO WiFi REV2 - Using this board would be a game changer, this board allows you to send emails to yourself or anyone you'd like to. This makes the whole system much more functional as it can alarm you if you're away from your room.
  2. Cover the product/Soldering - Even though the whole point of using an Arduino board is for prototyping, I feel like encapsulating the whole circuit with a clear cover would prevent it from easily being taken apart by someone or even any other kinds of damage. Another alternative is to actually solder all the connections together, maybe not using a breadboard but a PCB. This makes the connections secure and prevents anyone from pulling the wires apart.
  3. Use more numbers in the password - If you do more testing and figure out the range of values corresponding to the range of the potentiometer knob, you can divide it up into more than 3 sections. It can be really precise and makes it even harder to guess the actual passcode. 
  4. Use longer wires - As I've said previously, I wanted to place each breadboard at different locations so, longer wires would be needed in order to do so. By doing this, it allows you to place the boards at different places if you wanted to. For example, instead of placing the sensor at eye level, you can place it above the door frame, making it even harder to reach.

Troubleshooting

Below is a list of possible problems you might encounter or just some advice that I think would help while doing this project. These are all based on my experience and I hope it can help you with the same or any similar problems you might have.

  1. LCD - The LCD I used is the LCD with I2C, not the normal one. Do refer to the link I provided for further details. The connections and code process is quite different.
  2. Functions - Please do research on how to create your own functions. I used void for my functions initially and errors were popping up. Void is a data type meant to be used for functions that have no return value. If you wanted to have any values return please use the correct data type. As an example, I used bool for my functions as I wanted to return the values of the flags which are either TRUE or FALSE. Also, don't forget to assign a variable to the return values to update them.
  3. Button State/Potentiometer Value - This is something I overlooked and my whole code was always producing the same outcome. It turned out I forgot to keep updating the value of the button/potentiometer. For example, each time I want to check if the correct password is entered, I'd have to read the potentiometer again to make sure that the value is updated. If not, it'll use the first value it reads for all the checking.
  4. RGB values - Although this was minor, it made me so confused as to why the colours of my LED were different than what I expected. Take note of what type of RGB LED is used. I used a common Anode LED so the values are inverted to that of a common Cathode. More explanation is provided under the references section.
  5. Sensor calculation - If you were to read behind the theory of the ultrasonic sensor and tried calculating the number of cycles produced it'd be far from what you're gonna get. This is because it's mentioned in the datasheet to trigger it for a certain amount of time and those number of cycles are going to be produced. Don't think too much like I did.
  6. Flags - Make use of these, especially with the password function. I tried making it an infinite loop but it just couldn't work, as I wanted an option to turn the system off even when it's not triggered.

References

diyanaamani has not written a bio yet…