theriveroars
Published

Proximity Alarm and distance monitor Using Ultrasonic Sensor

A simple device to measure the distance to the nearest object. We have made it fun by adding sound and lights if the object is too close.

BeginnerFull instructions provided2,599
Proximity Alarm and distance monitor Using Ultrasonic Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
Or any servo motor
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Buzzer
Buzzer
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Tape, Double Sided
Tape, Double Sided
Something to keep things in place
Hot glue gun (generic)
Hot glue gun (generic)
It helps, but not a should have...

Story

Read more

Schematics

Circuit diagram

Made with Fritzing

Or Here Is The .fzz File

Code

Proximity Detector

Arduino
// Code by theriveroars
// On 25-8-2020


#include <Servo.h>
#include <NewPing.h>

Servo myservo;  // create servo object to control a servo


const int trigPin = 9;   // Trigger Pin of Ultrasonic Sensor
const int echoPin = 10;  // Echo Pin of Ultrasonic Sensor
const int red = 11;      // led to pin 11
const int buzz = 12;     // buzzer to pin 12
const int serv0 = 8;     // servo to pin 8

NewPing sonar (trigPin, echoPin, 300);      //300 is max distance

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(serv0);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600); // Starting Serial Terminal
  pinMode(red, OUTPUT); // setting led pin as output
  pinMode(buzz, OUTPUT); //setting buzzer pin as output
}

void loop() {
  long duration, inches, cm;
  for (pos = pos; pos <= 180; pos += 5) { // goes from 0 degrees to 180 degrees        // in steps of 5 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos' //pos varies from 0 to 180 in steps of 5
    delay(10);
    int cm = sonar.ping_cm();   // gets the distance in cm from the ultrasonic sensor
    Serial.print(cm);
    Serial.print("cm");     //prints the distance
    Serial.println();

    delay(15);

    if (cm < 20)
    {
      analogWrite(red, 125);          // if distance to object is less than 150 light up the red led to half its brightness
      if (cm < 10)     {            // if distance is less than 100
        digitalWrite(red, HIGH);    //light up red led to full brightness
        digitalWrite(buzz, HIGH);     //switch on the buzzer
      }
      else digitalWrite(buzz, LOW);

    }
    else
    { digitalWrite(buzz, LOW);     //otherwise switch of the buzzer and led
      digitalWrite(red, LOW);
    }
  }

  for (pos = 180; pos >= 0; pos -= 5) { // same thing is repeated for the turn from 180 to 0
    myservo.write(pos);
    delay(10);
    int cm = sonar.ping_cm();

    Serial.print(cm);
    Serial.print("cm");
    Serial.println();

    delay(15 );  // waits 15ms for the servo to reach the position
    if (cm < 20)
    {
      analogWrite(red, 125);
      if (cm < 10)
      {

        digitalWrite(red, HIGH);
        digitalWrite(buzz, HIGH);
      }
      else digitalWrite(buzz, LOW);

    }
    else
    { digitalWrite(buzz, LOW);
      digitalWrite(red, LOW);
    }
  }


  delay(100);
}

Link To Arduino Web Editor!

Credits

theriveroars
2 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.