Arduino Anti-Theft Device

by sophiatnguyen586 in Circuits > Arduino

146 Views, 0 Favorites, 0 Comments

Arduino Anti-Theft Device

IMG_6342.jpeg
Arduino Anti-Theft Device (Simple)

This DIY anti-theft device is a fun, hands-on project for anyone looking to create a simple security system. Using an Arduino Uno and an ultrasonic sensor, it detects movement and triggers an alarm if the monitored distance changes. A combination of tactile buttons lets you arm and disarm the device, with a buzzer and flashing LEDs signaling any alert. It’s an ideal project for building practical skills in electronics and programming while creating something useful to keep items secure.


*** Side Note: After step 6, the remaining parts of the project are optional, as it functions fully without the aesthetic enhancements.

Supplies

1 Arduino Uno

  1. Purpose: The microcontroller to run the logic for the device, monitor the sensor inputs, and control outputs (alarm, lights, etc.).


1 Ultrasonic Sensor

  1. Purpose: Measure the distance from the object to detect movement. When the distance changes by more than 1 inch, it will trigger the alarm.


2 Tactile Buttons

  1. Purpose:
  2. One Button: Start the system by pressing a button. It will begin monitoring the distance.
  3. Two Buttons: To disarm the alarm, press a specific sequence of two buttons in the correct order.


1 LED RGB

  1. Purpose: Flashing lights when the alarm is triggered.


3 Resistors (330Ω)

  1. Purpose: To limit current for the LEDs and buttons.


1 AA Battery Holder

  1. Purpose: Power the Arduino Uno and components when not connected via USB.


17 Jumper Wires and 1 Breadboard

  1. Purpose: Connect all the components without soldering.


4 Male - Female Wires

  1. Purpose: Extend the connection between the board and LED RGB


1 Micro Servo

  1. Purpose: Adds actuation (movement) to the device


3D Printer

  1. Purpose: Create covers for the wires and board
  2. This is purely optional as covers can be made with any other material


1 Bell

  1. Purpose: Produces noise when hit


1 Nail/Extension

  1. Purpose: To extend the Micro Servo arm


Hot Glue/Tape

  1. Purpose: Hold the cover pieces together


Thread/Wire

  1. Purpose: Hang the bell from the stand

Gather Supplies

IMG_6310.JPG

Gather all of the necessary supplies for the project as listed above

Power Connections

IMG_6313.JPG
IMG_6316.JPG
IMG_6317.JPG

Materials Needed:

  1. 2 wires
  2. Battery holder

Assembly:

  1. Connect the 5V pin on the Arduino to the positive rail (5V) of the breadboard.
  2. Connect the GND pin on the Arduino to the ground rail (GND) of the breadboard.
  3. Ensure the AA Battery Holder is connected to the Arduino's 5V and GND pins if using battery power instead of USB.

Ultrasonic Sensor Connections

IMG_6322.JPG
IMG_6324.JPG
IMG_6339.JPG
IMG_6334.JPG

Materials Needed:

  1. Ultrasonic Sensor
  2. 4 wires

Assembly:

  1. Connect the Ultrasonic Sensor to pins: J24 (VCC), J23 (TRIG), J22 (ECHO), J21 (GND)
  2. The sensor should face away from the board.
  3. Arduino and Breadboard Wiring:
  4. TRIG → Digital Pin 7
  5. Wire I23 to Pin 7
  6. ECHO → Digital Pin 6
  7. Wire I22 to Pin 6
  8. VCC to 5V rail
  9. Wire I24 to 5V
  10. GND to the GND rail
  11. Wire I21 to GND

LED RGB Connections

IMG_6340.JPG
IMG_6347.JPG
IMG_6350.JPG
IMG_6355.JPG

Materials Needed:

  1. RGB LED
  2. 3, 330 Ω resistors
  3. 4 Female - Male connector wires

Assembly:

  1. RGB LED:
  2. Connection to the breadboard is temporary (as shown in the photos)
  3. Attach each of the F-M connector wires to the legs of the RBG LEB
  4. Connect the F-M wires of the LED to pins J1, J2 (GND long leg), J3, and J4
  5. Breadboard and Arduino:
  6. Connect resistors to the LED on the breadboard
  7. Resistor 1: F1 to E1 (connected to Arduino Pin 9) (Wire A1 to Pin 9)
  8. Resistor 2: F3 to E3 (connected to Arduino Pin 10) (Wire A3 to Pin 10)
  9. Resistor 3: F4 to E4 (connected to Arduino Pin 11) (Wire A4 to Pin 11)
  10. Connect the common cathode (GND pin) of the LED RGB to the ground rail on the breadboard. Wire (A2 to (-)GND)

Tactile Button Connections

IMG_6357.JPG
IMG_6358.JPG
IMG_6362.JPG
IMG_6365.JPG

Materials Needed:

  1. 2 tactile buttons
  2. 4 wires

Assembly:

  1. Button 1 (Red): F8, F10, C8, C10
  2. Connect one side to (-)GND on the Arduino. (Wire A8 to GND)
  3. Connect the other side to Digital Pin 5 on the Arduino. (Wire A10 to GND)
  4. Button 2 (Blue): F14, F16, C14, C16
  5. Connect one side to (-)GND on the Arduino. (Wire A14 to GND)
  6. Connect the other side to Digital Pin 4 on the Arduino. Wire (A16 to GND)

Micro Servo Connections

IMG_6368.JPG
IMG_6369.JPG
IMG_6370.JPG
IMG_6373.JPG

Materials Needed:

  1. Micro servo
  2. 2 wires

Assembly:

  1. Connect the signal pin of the servo to Digital Pin 3 on the Arduino. (White wire)
  2. Connect the power (VCC) pin to the 5V rail on the breadboard. (Red Wire)
  3. Connect the ground (GND) pin to the GND rail on the breadboard. (Black Wire)

Download Code

You need to complete this step now so that the Arduino stores the code in its memory. This ensures it can operate independently, running off batteries later, instead of relying on being plugged into a computer to execute the program.

Code download is at the bottom of this step.

Steps:

  1. Plug in the board to the HDMI port of your device (computer/laptop)
  2. Download the Arduino IDE app
  3. Select Arduino Uno in the app
  4. Press the checkmark (Verify) and arrow (Upload) buttons in the top left to run the code
// Arduino Anti-Theft Code
// C++ Code

// Seting up servo and distance vars
#include <Servo.h>
Servo myservo;

float getDistance(); // function to measure distance from sensor
float distance; // variable for stracking current distance
float initialDistance; // intital distance measured when armed
float thresholdDistance = 05; // allowed tolerance of movement

// variable for tracking system states
bool isArmed;
bool alarmTriggered;

//Declare pins
int trigPin = 7; // sensor pins
int echoPin = 6;

int startButtonPin = 5; // button pins
int disarmButtonPin = 4;

int servoPin = 3; // servo controller

int redpin = 9; // LED pins for different colors
int bluepin = 10;
int greenpin = 11;// on physical board, green and blue pin are switched

// Function to measure distance from ultrasonic sensor
float getDistance() {

long duration;
float currentDistance;

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
currentDistance = (duration*0.034) / 2; // Convert time to distance in cm return distance
return currentDistance;
}

void setup()
{
//setup pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

pinMode(startButtonPin, INPUT_PULLUP);
pinMode(disarmButtonPin, INPUT_PULLUP);

pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
//servo
myservo.attach(servoPin);
myservo.write(100);
//Initial system states
isArmed = false; // System starts unarmed
alarmTriggered = false; // alarm starts untriggered
}


// Function to handle the main logic
void loop()
{

//first, check if alarm is triggered
if (alarmTriggered == true)
{
//pulse red and move servo back and forth to hit noisemaker
analogWrite(redpin, 255); // red intensity 1
analogWrite(bluepin, 0);
analogWrite(greenpin,0);
myservo.write(20);//position 1
delay(400);
analogWrite(redpin, 0); // red intensity 2
analogWrite(bluepin, 0);
analogWrite(greenpin,0);
myservo.write(120);//position 2
delay(250);
}
//if alarm not triggered
else
{
// Arm/Trigger logic
//start button pressed
if (digitalRead(startButtonPin) == LOW)
{
//not armed
if (isArmed == false)
{
initialDistance = getDistance(); // Set initial distance
isArmed = true; // Arm the system

analogWrite(redpin, 0);
analogWrite(bluepin, 0);
analogWrite(greenpin,255); // Green

delay(250); // Debounce delay
}
//already armed -->flash red
else
{
analogWrite(redpin, 255); // Red
analogWrite(bluepin, 0);
analogWrite(greenpin,0);

delay(250);

analogWrite(redpin, 0);
analogWrite(bluepin, 0);
analogWrite(greenpin,255); // Green

delay(250);
}
}
//still under 'if alarm not triggered'
// When the system is armed, track distance and check if alarm needs to be triggered
if (isArmed == true)
{
analogWrite(redpin, 0);
analogWrite(bluepin, 0);
analogWrite(greenpin,255); // Green

distance = getDistance(); // Measure current distance

// If the alram isn't triggered, and the sensor moves out of the threshold, trigger alarm and move servo
if (alarmTriggered == false && (abs(distance-initialDistance) > thresholdDistance))
{
alarmTriggered = true; // Set alarm state to triggered
}
}
}
/* Disarm logic
- has to be outside the 'if alarm is triggered' statement
because we want to be able to disarm it even if it's not triggered
*/
if (digitalRead(disarmButtonPin) == LOW)// Disarm button is pressed
{
isArmed = false; // Disarm the system
alarmTriggered = false; // Reset alarm state
analogWrite(redpin, 0);
analogWrite(bluepin, 255); // Reset LED to blue (unarmed)
analogWrite(greenpin, 0);
myservo.write(100);
delay(1000); // Debounce delay
}


if (isArmed == false) //system is on and unarmed
{
analogWrite(redpin, 0);
analogWrite(bluepin, 255); // Blue
analogWrite(greenpin, 0);
}
}

Downloads

Print 3D Models

Files to Print:

  1. 2 Extended Button Rods
  2. 1 Breadboard Cover
  3. 1 Arduino Cover
  4. 1 Bell Stand

Assemble

IMG_6384.JPG
IMG_6388.JPG
IMG_6382.JPG

This step does NOT utilize the bell stand.

Assembly:

  1. Lay newly printed pieces on top of assigned place
  2. Thread LED RGB and Micro Servo into correct places
  3. Tape the battery pack to any desired place

Extend Micro Servo

IMG_6396.JPG
IMG_6393.JPG

Materials Needed

  1. 1 screw

Assembly:

  1. Secure the screw with tape to the micro servo's arm

Bell Installation

IMG_6342.jpeg
IMG_6389.JPG

Materials Needed:

  1. 3D printed bell stand
  2. 1 bell
  3. Thread/wire
  4. Hot glue

Assembly:

  1. Glue the stand to the device, directly between the two covers
  2. Tie and hang the bell to the stand using a thread or wire

Run the Code

Congratulations! The project is complete. Once the Arduino is powered on, the system is active. Press the red button to arm the device, and hold down the blue button to disarm it after movement is detected.