Tomi Chen
Published © GPL3+

Backpack Alarm

Using an Adafruit compass/accelerometer and an 80-decibel alarm, this project prevents thieves from stealing your backpack.

BeginnerFull instructions provided2 hours26,785
Backpack Alarm

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Adafruit Triple-axis Accelerometer+Magnetometer (Compass) Board - LSM303
×1
SparkFun Large Piezo Alarm - 3kHz
×1
9V battery (generic)
9V battery (generic)
×1
6.35mm female audio jack (switched)
Optional
×1
6.35mm audio plug
Optional
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
9V to Barrel Jack Connector
9V to Barrel Jack Connector
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic for Arduino

To the Compass:
Blue - SDA
Yellow - SDL
Black - GND
Red - VIN

Code

Code

Arduino
Arduino 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("");

}

Credits

Tomi Chen

Tomi Chen

5 projects • 26 followers
I am an electronics hobbyist and like to do electronics in my free time. I enjoy reading, cooking, and programming.

Comments