0

$USD

$USD
PROJECTS Arduino

Arduino Alarm with Digital Vibration Sensor

DFRobot Nov 19 2018 895

How to make your own alarm system from idea to final product.

Story

Intro: Arduino Alarm With Digital Vibration Sensor
This tutorial is about how to make an easy and cheap alarm device by youself. All you need is basic knowledge in electronics and arduino programming.

Step 1: Materials

For this project we will need:
Arduino Uno (you can also take MEGA 2560)
Digital capacitive touch sensor V2 4x
Digital vibration sensor V2 1x
Digital buzzer module 1x
LCD display 20x4
Some LEDs
4 Channel Relay Shield for Arduino
Jumper wires (a lot of them)
Breadboard
9V battery
Battery housing

If you want to control lights or something like that, you can use Relay Shield for Arduino.
I also made housing for this alarm, so for that you will need SolidWorks or some other similar program. Of course you will also need 3D printer. If you dont have 3D printer you can also use surface mounted plastic sealed electric junction box case or something similar to that.

Step 2: Modules
For this project I used three different modules. It is very easy to connect those modules to Arduino because of its very good connection design. You get connection wire in the package with module(It can be seen on the picture).
Digital vibration sensor module
This sensor module is very easy to use and its fun to play with. His sensitivity is very high, so you don't need to shake it very hard. A little flick will be enough.

Specification:

IO Type: Digital
Switch life: up to 10 million seconds
Open circuit resistance: 10Mohm
Supply Voltage: 3.3V to 5V
Interface: Digital
Size: 22x30mm (0.87 x 1.18 in)

Digital capacitive touch sensor module
If you dont like the ordinary push buttons you can use this capacitive touch sensor modules. They have very fast response time, large touch area on the both sides.
Specification:
Supply Voltage: 3.3V to 5V
Interface: Digital
Size: 22x30mm (0.87 x 1.18 in)

Digital buzzer module
Very simple and in most cases very annoying buzzer module.
Specification:
Type: Digital
Power supply: 5VDC
If you want to know more about this modules you can visit: DFRobot Product Wiki

Step 3: Alarm Housing
I made 3D printed housing for this alarm.

First I measured every component, than I made the prototype of the front plane on millimeter paper, so that I checked if very distance is correct. You can see this prototype on the picture.
When I finished the prototype I stared to draw it in program for 3D drawing. If you dont know how to draw in this programs you can also write on my mail for STL file of this housing, so that you can print it.

It took about four hours to print this housing. This printer is in middle range so you have to do a little corrections on you product(you need to file housing or maybe widen some holes with a drill...)

Size:
150x60x120mm
Housing is made from PLA plastic. It has 3mm wide walls, which is more than enough for souch a housing. The biggest opening is for LCD display, smaller holes next to LCD display are for LEDs. On the bottom there are openings for touch modules and buzzer module. On the right side there is also one more opening for touch module. You can mount this housing on the wall or door.

Step 4: Wiring

There are a lot of different components in this project so you must be careful to connect them right.
I will show you how is every component wired in this project.
Touch sensor modules:
I used four of this modules. One for decrement and one for increment alarm time, third is used for choosing programs (I will tell more about this in next step) and fourth is used for activation.

This module has three pins.(5V+, GND, OUTPUT).
You connect the red wire to 5V+ and black wire to GND. The green wire goes to digital pin on Arduino. (You can also use different colors, but it is easier for you and more manageable if you use this colors.)

For this project:
Module for decrement is connected to digital pin 8.
Module for increment is connected to digital pin 9.
Module for choosing program is connected to digital pin 10.
Module for alarm activation is connected to digital pin 11.
Digital vibration sensor:
This module has three pins.(5V+, GND, OUTPUT).
Module is connected to digital pin 3
Buzzer module:
This module has three pins.(5V+, GND, OUTPUT).
Module is connected to digital pin 13.
LCD display:
Display has four pins.(Vcc, GND, SCL, SDA):
Connect Vcc to 5V+, GND to GND on Arduino, SCL and SDA pin on display to SLC and SDA pin on Arduino.
LED:

I used some LEDs for indication. The shorter leg or cathode of LED goes to GND, longer leg or anode goes to digital pin. If you dont belive me you can also check that with multimeter.(Diode conducts from anode to cathode if in good condition)
Green LED is connected to digital pin 4. (On when alarm is activated)
Yellow LED is connected to digital pin 5 (On when alarm is deactivated)
Red LED is connected to digital pin 12 (Blink if you choose the right program and if you shake the vibration sensor)
White LEDs are connected to digital pin 2. (On when alarm is deactivated so that you can see touch modules).
Modules and LEDs are supplied from Arduino. So you just connect all Vcc pins to 5V on Arduino and all GND pins to GND on Arduino. Arduino doesnt have enought 5V and GND pins so you have to use breadboard.

You can supply Arduino with 9V battery or with appropriate adapter. Powering it with 9V battery will give you much more mobility, but you will need to replace it after some time.
If you want to control AC appliances you will need to connect relay module.

Step 5: Code

Code is a bit longer and if you are not familiar with programming, you can get lost very easily.
I used subprograms because this makes program more manageable.

So the idea of program is that:
You can choose alarm duration (1-15 seconds with 5 intervals)

You can choose program(With program I mean different possibilities how the alarm goes off. You can choose that only buzzer is on, that buzzer combined with LED is on or that just LED is on.)

You can activate and deactivate alarm (this is indicated with LEDs. With green and yellow)

And everything is displayed on LCD display. You have numerous possibilities for what you want to show on you LCD display.

So when you shake the vibration sensor alarm goes off, but it depends on what program you choose.

So on and on, if you know how to use if statements and if you know controls for LCD display(lcd.clear, lcd.setCursor(x, y)...) this program shouldn't be difficult to understand.

Code

//LCD
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

#define BACKLIGHT_PIN 3

LiquidCrystal_I2C lcd(0x20, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

//Activation
int turnonPin = 11;
int turnon = 0;
unsigned int a = 0;

//Program
int programPin = 10;
int program = 0;
unsigned int b = 0;

//Decrement
int upPin = 9;
int up = 0;

//Increment
int downPin = 8;
int down = 0;

unsigned int y = 1000;
unsigned int DELAY = 1;

int led = 13;
#define SensorINPUT  3  
unsigned char state = 0;

void setup() {

  lcd.begin (20,4); //  
  // LCD Backlight ON
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();
  
  pinMode(turnon, INPUT);
  pinMode(up, INPUT);
  pinMode(down, INPUT);
  pinMode(led, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(SensorINPUT, INPUT);

  attachInterrupt(1, blink, FALLING);
  
  Serial.begin(9600);
}

void loop() {
 
 Activation();
 Delay();

 
}

//Activation alarma
void Activation(){

 delay(1000);
 turnon = digitalRead(turnonPin);
 
if(turnon == HIGH){
  a=a+1;
  }
  
if(a > 1){
   a=0;
  }

if(a < 0){
   a=0;
  }

if(a == 1){
  lcd.setCursor(0,0);
  lcd.print("  ALARM ACTIVATED  ");
  digitalWrite(2, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
}
else{
  digitalWrite(5, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(2, HIGH);
  lcd.setCursor(0,0);
  lcd.print("*");
  lcd.setCursor(1,0);
  lcd.print("ALARM DEACTIVATED");
  lcd.setCursor(18,0);
  lcd.print("*");
  
 }
}

void Delay() {

 up = digitalRead(upPin);
 down = digitalRead(downPin);
 program = digitalRead(programPin);

 
if(up == HIGH){
  y = y + 1000; 
 }
  if(down == HIGH){
   y = y - 1000;
   }

if(y > 15000){
  y = 15000;
  }
if(y < 1000){
  y = 15000;
  }


if(program == HIGH){
  b = b + 1;
}
 if(b > 3){
  b = 1;
}
if(b < 1){
  b=1;
}

DELAY = y/1000;
if(DELAY < 10){
  lcd.setCursor(19,1);
  lcd.print(" ");
  }
if(DELAY < 10){
  lcd.setCursor(19,2);
  lcd.print(" ");
}

switch(b){
  case 1:
  if(state != 0&&a == HIGH){
    state = 0;
    for(int x = 0;x<=5;x++){
    digitalWrite(led, HIGH);
    lcd.clear();
    lcd.setCursor(7,1);
    lcd.print("BREAK IN");
    delay(y);
    digitalWrite(led,LOW);
     lcd.clear();
    delay(200);
    }
  }
else
{
  digitalWrite(led,LOW);
}
  Serial.println(y);
if(a == 0){ 
lcd.setCursor(0,2);
lcd.print("ALARM DURATION:");
lcd.print(" ");
lcd.print(DELAY);
lcd.print(" ");
lcd.print("s");

lcd.setCursor(0,3);
lcd.print("Program:");
lcd.print(" ");
lcd.print("Sound    ");
}
else{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  ALARM ACTIVATED  ");
}
break;

 case 2:
 if(state != 0&&a == HIGH){
  state = 0;
    for(int x = 0;x<=5;x++){
    digitalWrite(led, HIGH);
    lcd.clear();
    lcd.setCursor(7,1);
    lcd.print("BREAK IN");
    digitalWrite(12, HIGH);
    delay(y);
    digitalWrite(led,LOW);
    digitalWrite(12, LOW);
     lcd.clear();
    delay(200);
    }
  }
else
{
  digitalWrite(12, LOW);
  digitalWrite(led,LOW);
}

if(a == 0){
 lcd.setCursor(0,2);
lcd.print("ALARM DURATION:");
lcd.print(" ");
lcd.print(DELAY);
lcd.print(" ");
lcd.print("s");

lcd.setCursor(0,3);
lcd.print("Program:");
lcd.print(" ");
lcd.print("Sound+LED  ");
}
else{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  ALARM ACTIVATED  ");
}
break;

case 3:
if(state != 0&&a == HIGH){
  state = 0;
    for(int x = 0;x<=5;x++){
    digitalWrite(12, HIGH);
    lcd.clear();
    lcd.setCursor(7,1);
    lcd.print("BREAK IN");
    delay(y);
    digitalWrite(12,LOW);
     lcd.clear();
    delay(200);
    }
  }
else
{
  digitalWrite(led,LOW);
}

if(a == 0){
lcd.setCursor(0,2);
lcd.print("ALARM DURATION:");
lcd.print(" ");
lcd.print(DELAY);
lcd.print(" ");
lcd.print("s");

lcd.setCursor(0,3);
lcd.print("Program:");
lcd.print(" ");
lcd.print("Quiet    ");
}
else{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  ALARM ACTIVATED  ");
}
break;
}

}
void blink() {
    state++;
}