Have you ever felt the need for a backpack alarm? Have you ever sat down in a public area with your backpack, and feared someone might steal it? If so, this is the project for you!
Using the LSM303 compass/accelerometer, this project sounds an alarm when the backpack has been moved. It uses the compass to monitor the orientation, and when there has be movement of more than 10 degrees, it sounds an alarm.
Step 1: Wire up the ArduinoConnect the LSM303 breakout board to the Arduino like so:
- SDA - SDA
- SDL - SDL
- VIN - 5v
- GND - GND
For more details, check out Adafruit's guide.
Connect the piezo buzzer to the Arduino like so:
- + to Pin 13
- - to GND
Download the Adafruit_LSM303DLHC library and the Adafruit_Sensor library as zip files.
Open the Arduino IDE. If you don't have it, download it here.
Go to Sketch > Include Library > Add.ZIP Library...
Then, find and select the.ZIP files you downloaded earlier.
Open up the Arduino IDE and create a new file. Delete everything already there and paste in the following code.
// Include Libraries
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_LSM303.h>
// 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: Test it!If you did everything correctly, after pressing the reset button on your Arduino, you should be greeted with some beeps, 3 seconds of silence, then a longer beep. If you rotate the LSM303 breakout, you should then hear the alarm turn on. 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 get 2 long beeps before the 3 second delay, then your wiring has issues. Check all the connections.
If it doesn't seem to work, then it may be that the libraries are not properly named. In the code, remove the '#include' statements in the beginning. Then go to Sketch > Include Library and select the 'Wire', 'Adafruit LSM303DLHC', and 'Adafruit Unified Sensor' libraries. If after all that, it still doesn't work, make sure you don't have any strong magnetic fields near you. Go to a different room, or outside. Also, make sure that the breakout board is somewhat level and parallel to the ground.
Step 5: Add a KeyYou should have a working project now, just plug in a 9v battery into the barrel jack on your Arduino, mount it in your backpack, and you should be good to go. However, unplugging and plugging back in the battery every time you want to arm/disarm your alarm is kinda annoying. That's where the 6.35mm audio jack comes into play. Instead of a 3.5mm jack which most people have on headphones, using a 6.35mm jack increases security. However, if you have a keyed switch or some other switch you want to use instead, feel free to do so.
Anyway, to add the key, wire the switch/key between the battery and the DC barrel jack.
If you have one of these, just cut one of the wires, add extension wires if necessary, and add the switch. In my case, I got a terminal block barrel jack and wired it using that. As I am not too concerned about security, my key is just a simple toggle switch, but you can get creative. Use a reed switch and magnet, RFID, maybe even a smartphone app!
Step 6: Mount it!Congrats! You now have a fully functioning alarm with key. All you have to do now is mount it in your backpack. Cut a hole in the side for the buzzer and one for the keyhole. I used zip ties, but a more permanent solution may be used. Make sure that the sensor is mounted somewhat parallel to the ground and the hole in the buzzer is not obstructed.
This alarm is not limited to backpacks. Add it to a door, a cookie jar, the fridge, or just anything you want to protect. The possibilities are endless!
Comments