iwoox
Published © GPL3+

Arduino Alarm with Ultrasonic Sensor

This project is about how to make an easy and cheap alarm device by youself. All you need is basic knowledge in electronics and Arduino.

IntermediateFull instructions provided2 hours386
Arduino Alarm with Ultrasonic Sensor

Things used in this project

Hardware components

UTSOURCE Arduino Mega
×1
UTSOURCE Ultrasonic sensor
×1
UTSOURCE Relay module
×1
UTSOURCE LED
×1
UTSOURCE Resistor 10k ohm
×1
UTSOURCE Buzzer module
×1
jumper wires
×1
breadboard
×1
2 push buttons
×1

Story

Read more

Code

Code for alarm

Arduino
#define trigPin 7
#define echoPin 8
#define buzzer 3
#define Led_red 4
#define Led_blue 5
#define Led_green 11

const int off = 6;
const int on = 10;



int onState = 0;
int offState = 0;

int x = 0;

void setup() {
Serial.begin(9600);
  
  pinMode(on, INPUT);
  pinMode(off, INPUT);
  pinMode(Led_green, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(Led_red, OUTPUT);
  pinMode(Led_blue, OUTPUT);
  


}

void loop() {
  
 long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;  
    
  
onState = digitalRead(on);
offState = digitalRead(off);

if(x > 1)
{
  x = 0;
}

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


if(onState == HIGH)
{ 
  x = x +1;
  delay(100);
}

if(offState == HIGH)
{
x = x -1;
delay(100);
}

Serial.print("Variable  ");
Serial.print(x);
Serial.print("  ");
Serial.println( );
Serial.print("Distance  ");
Serial.print(distance);
Serial.print("  ");
delay(300);

if(x == 1)
{
  digitalWrite(Led_green, HIGH);
  digitalWrite(Led_red, LOW);
}
else
{
  digitalWrite(Led_green, LOW);
  digitalWrite(Led_red, HIGH);
}

if(distance < 15){
  digitalWrite(buzzer, HIGH);
  digitalWrite(Led_blue, HIGH);
  delay(50);
  digitalWrite(buzzer,LOW);
  digitalWrite(Led_blue, LOW);
  delay(50);
  digitalWrite(buzzer, HIGH);
  digitalWrite(Led_blue, HIGH);
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(Led_blue, LOW);
}


}

Credits

iwoox

iwoox

10 projects • 2 followers
Student at Faculty of Electrical Engineering and Computer Science.

Comments