Making a High-Tech Security System — with an Arduino Uno

Ruhaan
6 min readDec 2, 2023

I think most people with siblings can understand when I say that I really don’t like my sister going to my room. The one place in the house I can truly say is mine. I have this problem quite a bit. My sister, as much as I love her, really loves coming into my room, for no apparent reason — and no matter how much I tell her, she will still barge in. So, I decided to take this into my own hands, and build something that could (hopefully) bring an end to this.

About the project

Now the system itself is fairly simple. It uses a computer called the ‘Arduino Uno’, which is a really simple computer, that can handle basic tasks. If you want to get into robotics, this computer is a really good start, since it is very simple to use, and is very user-friendly. The remaining components were actually all fairly simple, and made the system very easy to put together. The parts altogether costed around $100, which is very reasonable in the world of robotics. The parts included:

  • A 5V piezo buzzer
  • Male-to-male jumper wires
  • A breadboard
  • 3 colored LED’s (I used green, red, and yellow)
  • An HC-SR04 ultrasonic sensor
  • 221 ohm resistors
  • Arduino Uno

Simple right? The system works by having the ultrasonic sensor send out sound waves, which helps the sensor detect where something is, and roughly how large it is. The sensor then sends that info through the computer, and emits a green light from the green LED if the sensor detects something within 50 cm. As the target moves closer (in the case of my testing the target was my hand), the sound waves take less time to hit the target and return, which tells the computer it is closer. Once the target reaches around 20 cm from the sensor, a sound is emitted, and the yellow LED turns on, signifying that the target is detected and moving towards what you are trying to protect, in this case my door. Once the target reaches anything within around ~10 cm, the buzz sound becomes very loud, and the red LED turns on, indicating a threat.

Understanding the components

Each component plays a crucial part in this build, and they all work together to make it work.

  • The resistors are tiny little wire-like components that limit how much electricity reaches the component it is connected to, in this case the LED’s and the buzzer. The LED’s were more optional than needed for the lights, but for the buzzer I would say it was needed, as the resistor helped to limit how loud the buzzer was (it can be really loud), and also allows the buzzer to last longer
  • The buzzer does exactly what you expect — it buzzes. The name is a little off putting because it is really loud, but with the help of the resistors, it made sounds for when a target was detected by the security system.
  • The breadboard is used to connect all the wiring and components. It is usually made of plastic and filled with many holes, each used to hold a wire, LED, or any other component. Every row is connected by one electric channel, meaning if one hole is connected to a slot in the computer, every wire connecting to that row is as well, which makes it easy for multiple components to be connected to one spot in the computer
  • The ultrasonic sensor, as explained above, helps the system detect whether or not a target is in front of it by sending out sound waves and receiving them back.
  • Jumper wires are the simplest to understand. They just help connect all the parts together. They fit in the holes on the breadboard and the Arduino Uno
  • LED’s are a very simple tool, and were used in this project to visually show how close a target is.
  • Lastly, the Arduino Uno itself, is a basic computer/microcontroller that helps run this system. Code can be uploaded from the Arduino IDE software directly to the Arduino Uno, which makes the Arduino a very user friendly microcontroller.
An Arduino Uno

Building and coding the system

Building the system was very simple. There were many iterations of this project I found during my time researching, and I mixed and matched a few components from the best ones to make the best system I could. The process took around an hour, and was overall extremely fun. Connecting the wires and components was a little meticulous as the security system is actually quite small and compact, which made connecting the already small wires tougher. In the end, I ended up with a system that looked like this:

But the tough part came with coding.

Now I have barely any coding experience. Coding was something I have tried to get into for a very long time, but I could never grasp it fully. So coding was pretty annoying, especially because the people that I found the project from weren’t very descriptive of what their code did, or often gave code that had errors or code that was just downright wrong. But after a little bit of patience, some research and quite a bit of Stack Overflow, I managed to finish and understand the code. It looked a little something like this :

If the code is hard to read, I do apologize, but there was a lot of code that had to be added in order to make this functional.

Basic understanding of the code

Code can always be copy and pasted, then fixed and troubleshot by someone who understands it better. But I really wanted to understand the code I was writing, so I took the time to get a basic understanding. This included some basic research, reaching out to experienced coders, and more.

The beginning lines:

const int trigPin = 7; 
const int echoPin = 6;
const int LEDlampRed = 9;
const int LEDlampYellow = 10;
const int LEDlampGreen = 11;
const int buzzer = 3;
int sound = 500;
long durationInDigit, distanceInCM;

help to determine what component is connected to what pin on the Arduino. “const int” is used to define an integer that does not change, so in this case it shows that these components are in their respective slots, and that does not change throughout the code. The “trigPin” and “echoPin” represent the trigger and echo pins on the ultrasonic sensor, which are responsible for triggering and receiving sound waves.

Parts like:

 if (distanceInCM > 50) 
{
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}

and

else if (distanceInCM <= 50 && distanceInCM > 20) 
{
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}

else if (distanceInCM <= 20 && distanceInCM > 5)
{
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampRed, LOW);
tone(buzzer, 500);

help the computer to determine what to do when something is within distance. For example in the line “else if (distanceInCM <= 50 && distanceInCM > 20)”, that says that if something is within a distance under 50 cm and over 20 cm, that it should do what the lines below say, which in this case say:

{
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}

those lines say that when the criteria in the “else if” statement is met, the green LED lamp should be high brightness, and the other 2 should be low brightness.

See what I mean when I said this was simple? The code is kinda understandable if you really read it.

Takeaways

I learned a lot during this project. Although I have used these components before, and written similar code, I really took the time to understand what each part of the project does. In my future projects, I plan on using many of the things I learned with this project, even though it may use a new coding language, or new components. I had a lot of fun working on this, and now I know when my sister is reaching for the handle of my door.

Thanks for reading!

--

--