Steven Jonathan
Published © GPL3+

Soda Can Alarm

Ever lose something because of someone entering your room? if you have, then I will show you how to build a low-cost alarm!

BeginnerFull instructions provided30 minutes4,477
Soda Can Alarm

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

The Schematic

Code

Soda Can Alarm Code

Arduino
Make sure to put the right distance by measuring the distance of your sensor to the target (wall, etc)
#include <Servo.h> //include the servo library

#define trig 6 // we used pin 6 for the trig pin, and pi
#define echo 5   


int x = 17; // (in cm), measure how long the distance between object and ultrasonic sensor 
int distance;  // variable to save the distance you get from getdistance function



Servo myservo;

void setup(){
  pinMode(trig, OUTPUT); // setup trig and echo 
  pinMode(echo, INPUT);
  myservo.attach(9); // attack servo to pin number 9
  Serial.begin(9600);// begin serial monitor
}


void loop(){
    distance = getdistance(); // get the distance from function
    Serial.print("Distance:"); // print Distance 
    Serial.println(distance); // print on the same line as distance
                              // it put the value of the distance in cm
  
   if(distance <= x){          // if distance less than equal to x
      myservo.write(180); // turn servo 180 degree
      delay(1000);        // delay 1 second
      myservo.write(0);   // turn servo back to it's original position
      delay(1000);        // delay 1 second
    }
    else if(distance >= x){  // if distance more than equal to x
                          // do nothing
    }
    delay(50); // delay loops for 50 second
}
    

float getdistance(){ // function to get distance from sensor

  unsigned long duration; 
  float calculatedDistance; // add duration and calculatedDistance as variable
 
  digitalWrite(trig, LOW); // send signal to object
  delayMicroseconds(10);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);


  duration = pulseIn(echo, HIGH); // receive the signal
  calculatedDistance = duration*0.034/2;
  // from send to receive signal we get the time and 
  // put the equation s = v*t (v= 0.034 the speed of sound)
  // divided by two in order to get the real distance 
  return calculatedDistance; // return the distance to loop
  
}

Credits

Steven Jonathan
1 project • 1 follower
Like to make something out arduino
Contact

Comments

Please log in or sign up to comment.