How to design a backpack anti-theft alarm using Arduino UNO
Source: InternetPublisher:supremeOne Keywords: Alarm Burglar Alarm Arduino Updated: 2024/05/27
background
Have you ever sat in a public place with your backpack and worried that someone might steal it? If so, this is the project for you! It's time you needed a backpack alarm.
Using the LSM303 compass/accelerometer, this project will sound an alarm when the backpack is moved. It uses the compass to monitor the orientation and when there is more than 10 degrees of movement, it will sound an alarm.
Step 1: Connect Arduino
Connect the LSM303 breakout board to the Arduino as follows:
SDA-SDA
SDL-SDL
VIN - 5V
Earth - Earth
For more details, check out Adafruit's guide.
Connect the piezo buzzer to the Arduino like this:
+ to pin 13
- Ground
Step 2: Install the library
Download the Adafruit_LSM303DLHC library and the Adafruit_Sensor library as zip files.
Open the Arduino IDE and go to Sketch > Include Library > Add.ZIP Library
Then, locate and select the .ZIP file you downloaded earlier.
Step 3: Programming the Arduino
Open up the Arduino IDE and create a new file. Delete everything that's already there and paste in the following code.
// Include Libraries
#include
// Adjust the sensitivity
const int sens = 10;
// Assign a unique ID to this sensor at the same time
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);
void setup(void)
{
//Just for Debugging
//Serial.begin(9600);
//Serial.println("Starting...");
// Setup pin 13
pinMode(13, OUTPUT);
// Initialise the sensor
if(!mag.begin())
{
// There was a problem detecting the LSM303 ... check your connections
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
while(1);
}
// Wait 5 seconds
delay(5000);
// Alert when started
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
}
// function for getting the sensor value
int getDeg(void){
// Get a new sensor event
sensors_event_t event;
mag.getEvent(&event);
float Pi = 3.14159;
// Calculate the angle of the vector y,x
float heading = int((atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi);
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
return heading;
}
void loop(void)
{
// get sensor values
int oldDeg = getDeg();
delay(1000);
int newDeg = getDeg();
if (newDeg < (oldDeg-sens) && oldDeg != 0 && newDeg != 0) {
// sound the alarm
digitalWrite(13, HIGH);
// Just for debugging
//Serial.println("Triggered");
//Serial.println("");
}else if (newDeg > (oldDeg+sens) && oldDeg!= 0 && newDeg != 0) {
// sound the alarm
digitalWrite(13, HIGH);
// Just for debugging
//Serial.println("Triggered");
//Serial.println("");
}
// Just for debugging
//Serial.print("New:");
//Serial.println(newDeg);
//Serial.print("Old:");
//Serial.println(oldDeg);
//Serial.println("");
}
Plug in your Arduino, then compile and upload the code.
Step 4: Testing
If you did everything correctly, after pressing the reset button on your Arduino you should hear some beeps, 3 seconds of silence, then a longer beep. If you twist the LSM303 breakout you should hear the alarm go off. If the sensor is too sensitive, just increase the "sens" variable in the code near the top. If you want more sensitivity, decrease the "sens" variable.
If you hear 2 long beeps before the 3 second delay, you have a wiring problem, check all connections.
If it doesn't seem to be working, then maybe the library is not named correctly. In your code, remove the "#include" statement at the beginning. Then go to Sketch > Include Library and select the "Wire", "Adafruit LSM303DLHC", and "Adafruit Unified Sensor" libraries. If it still doesn't work, make sure there aren't any strong magnetic fields near you. Go to another room, or outside. Also, make sure the breakout board is somewhat level and parallel to the ground.
Step 5: Add the key
You should now have a working project, just plug a 9v battery into the barrel jack on the Arduino, mount it in your backpack, and you're good to go. However, it's a bit annoying to unplug and reinsert the battery every time you want to arm/disarm the alarm. That's where the 6.35mm audio jack comes into play. Using a 6.35mm jack allows for increased security, as opposed to the 3.5mm jack that most people use on their headphones. However, if you want to use a key switch or some other switch, feel free to use that.
If you want to add a key, connect the switch/key between the battery and the DC barrel jack.
If you have one of these, just cut one of the wires, add an extension cord if necessary, and then add the switch. In my case, I had a junction box barrel jack and wired it using that. Since I'm not too concerned about security, my key is just a simple toggle switch, but you can get creative. Use a reed switch and a magnet, RFID, or maybe even a smartphone app!
Step 6: Installation
You now have a fully functional alarm with a key, now all you have to do is mount it in your backpack. Cut a hole in the side of the buzzer and one for the keyhole. I used zip ties but a more permanent solution could be used. Make sure the sensor is mounted somewhat parallel to the ground and that the hole in the buzzer is not blocked.
- Working principle of ZNB-S digital display intelligent motor protector control circuit
- Design and production of no-load automatic power-off device for household power supply
- Home wireless burglar alarm circuit
- "Watchdog" anti-theft alarm circuit
- Disconnection anti-theft protection circuit
- Automatic power off switch when power outage occurs
- A novel resonance demonstration device
- CD4017 simple and easy to make zero-crossing power regulation circuit
- New type of wind and rainproof automatic window that is simple and easy to make
- NE556 Police siren sound generator