Introduction: Arduino Alarm System

Before I go into my process in this project I will give a brief rundown on what this project does. As the name suggests it acts like an alarm system only working during the night (no light detected) to detect movements with a motion sensor. As the object gets closer the buzzer gets faster and faster and the RBG led changes colour to represent how far the distance is between the object and motion sensor (green being farthest red being closest).

Step 1: Idea

This Idea is similar to the raspberry pi alarm system I made. However, using the Arduino UNO I knew I could make much needed improvements, such as being able to tell how close the object is to the motion sensor through not only a change in the alarm but also a change in led colour if you wish to turn the alarm off which is also a new feature. With the Arduino UNO and some code this was possible.

Step 2: Research

The project that I was most influence by was this one.

https://www.instructables.com/id/Ultrasonic-Distan...

As you can see in the image above, the thing I drew closest inspiration was that as the object got closer to the motion sensor more LED's turned on indicating how close the object is. I changed this slightly so that instead of LED's turning on it was an RGB led changing colour. I also added various other features such as a buzzer and photoresistor.

Step 3: Materials

  1. Arduino UNO
  2. Piezo (Buzzer)
  3. Ultrasonic distance sensor
  4. Bread board
  5. RGB LED
  6. LED
  7. Switch
  8. Photo resistor
  9. 6 Resistors (1k x3, 330 ohms, 220 ohms x2)
  10. Jumper wires

Step 4: Basic Assembly

Put down all the compoents that will be used. The bread board, Arduino, photo resistor, rgb led, led, switch, piezo. Make sure they are all spread apart so you don't run into issues.

Step 5: Resistor Assembly

First power and ground each of your rails. Then connect a 1k ohm resistor to any leg of the photoresistor and to the power rail. Another 1k ohm resistor to the positive leg of the buzzer. Connect a 330 ohm resistor to the red leg of the RGB led and two 220 ohm resistors to the blue and green legs avoiding the cathode. Finally connect a 1k ohm resistor to the anode leg of the LED, note don't connect it to any rail as it will be connect to the Arduino.

Step 6: Wiring Assembly

Ground the photo resistor and connect the other leg to the A0 pin on the Arduino. Connect the ultrasonic sensor to the power and ground pings. The trig and echo pings go to pin 7 and 6 respectively. Ground the piezo and connect it's resistor to pin 5. For the RGB led connect the cathode to the ground. Then connect the red, blue and green resistor to pin 11,10 and 9 respectively. Connect the terminal 1 and 2 legs of the switch into power and ground, then connect it's common leg to pin 0. Finally connect the final LED pin to 1.

Step 7: Code

I will break down each segment of code.

//all pin numbers
const int trigPin = 7;
const int echoPin = 6;
int buzzer = 5;
int redPin = 11;
int bluePin = 10;
int greenPin = 9;
int switchPin = 0; 
int switchState = LOW;
int onOffLight = 1;

This part of the code makes it so you don't have to remember each number for each component.

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(onOffLight, OUTPUT);
  pinMode(A0, INPUT);

}

This part sets each pin to output or input. The only input ones are the switch as we need to read its state and the photoresistor (A0) as we need to read the brightness.

void setColour(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(bluePin, blue);
  analogWrite(greenPin, green);
}

This function will change each individual leg of the RGB to make a certain colour depending.

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

This is to convert the ultra sonic sensor information into inches

void alarm(int speed){ //function for alarm 
  if(switchState == HIGH){ //alarm only works if switch is on
  	tone(5,220,100); //speed is how many miliseconds between two rings
  	delay(speed);
  	tone(5,180,100);
  	delay(speed);
    digitalWrite(onOffLight, HIGH);//if alarm is on than the LED is on
  }
  else
    digitalWrite(onOffLight, LOW);//if alarm is off than led is off
}

this is the method for the alarm. The user can control wether or not the alarm is working via the switch. It only It has two tones that are delayed between each other depending on the speed in miliseconds. If the alarm is working the Light indicator turns on, if its not its off.

void loop()
{
  long duration, inches;
  switchState = digitalRead(switchPin);//reading the pin state each loop


  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  inches = microsecondsToInches(duration);
 
  delay(100);
  setColour(0,0,0); //reseting RGB led each time
  
  //3 tiers of distance to sensor
  //each tier closer makes colour change
  //and alarm gets faster
  if (inches <=35 & analogRead(A0)> 500){
    setColour(255,0,0);//red
    alarm(300);
  }
  else if (inches <=80 & analogRead(A0)> 500){
    setColour(255,255,0);//yellow
    alarm(400);
  }
  else if (analogRead(A0)> 500){
    setColour(0,255,0);//green
    alarm(700);
  }   
}

This is the actual part of the code that will be in loop. First it takes information from the distance sensor and converts that to inches. There is a for loop with 3 tiers representing the distance from the sensor. If the Object is within 35 inches the alarm is beeping fast and the RGB is red, if its within 80 inches the alarm is beeping at a medium pace and the RGB is yellow, if its more than 80 inches the alarm is beeping slowly and the RGB is green.

Attachments

Step 8: Refine

There were a few features I added over time

  1. I added the option to turn off the alarm but not the LED and distance sensor. You might want the intruder to not hear the alarm but only see the distance.
  2. I added an led if the alarm is on so the user knows if the alarm is on or off.
  3. I also added an LDR so that it only works during night time, the time where you would want to check for an intruder.