Distance measuring and obstacle avoiding sensor SR04 with arduino


Welcome to the second Tutorial in the series of Arduino Learn by Doing.Today our task is to learn about Ultrasonic sensor. How and why to use ultrasonic sensor and the implementation of ultrasonic SR04 SONAR sensor module with Arduino. So today We will discuss some of the projects with ultrasonic sensor later in coming posts but before that, we will see the working and usage of ultrasonic sensor SR04 with Arduino.
So Let's start with the contents list.

Contents:

  1. Introduction to Ultrasonic SR04 SONAR sensor
  2. How Ultrasonic sensor works
  3. Components list
  4. Circuit diagram
  5. Arduino code for Ultrasonic SR04 sensor

1-Introduction to Ultrasonic SR04 SONAR sensor

The Ultrasonic Sensor as the name sounds like the sound it works on Sound waves. The Ultrasonic is a sensor device that actually works with sound waves.It works on the phenomenon of sending and receiving of ultrasonic sound waves.And by sound waves, it measures the distance and obstacles.It is a sensor that measures the distance or Detects obstacle.And it can be used either in both ways.The Ultrasonic Sr04 SONAR is a module created for interfacing with micro controllers.
The basic phenomenon of measuring distance or detecting an obstacle with ultrasonic sound waves comes from underwater communication.And If I'm not wrong we can say the communication way of fishes.

2-How Ultrasonic SR04 SONAR sensor works

The Ultrasonic sensor works on the phenomenon of sending sound waves at a particular frequency and receive on bouncing back.The sensor measures distance and existence of the obstacle with in the time of sending and receiving of the sound wave.
The total traveling distance of sound wave can be calculated by using this formula:
The SR04 obstacle avoiding module easily interfaces with Arduino. This SR04 ultrasonic module is a high accuracy range detector module.The Distance range of SR04 SONAR is from 2 cm to 400 cm.
The ultrasonic module works fine in almost all good conditions but remembers in high noise and in some situations and materials the module will not work. It also not works in dead band. The dead band is in the too much less distance below 2 cm.
The good thing about ultrasonic SR04 is it's quite cheap and has built-in transmitter and receiver.You don't need to buy two modules to communicate with each other.
The SR04 module has 4 pins other than the power supply and ground one is an echo that is used for receiving signals and the other is the trigger that is used for triggering the signal.

3-Components List

  1. 1x Arduino Board Uno  (any of your choice)
  2. HC-SR04 Ultrasonic SONAR sensor
  3. 1x Bread board
  4. jumper wires

4-Circuit Diagram


The working current of Ultrasonic SR04 module is 15 mA. 
  • Connect the Vcc pin to the 5v on Arduino and Ground pin to GND on Arduino
  • Connect Trig pin to any digital pin on Arduino
  • Connect echo pin on any digital PWM pin on Arduino as echo pin is our INPUT
Connect all pins correctly and test the sample code first then try to implement more sensor and complex code.The Arduino code is, as usual, simple so let's see the coding part.

5-Arduino Code

Test the Arduino code first then try to change for more sensors and complex situations.The output is in voltages in modules so we need to convert it into cm or inches so divide the time duration by 29

.......      #define trigPin 11         .........
.......      #define echoPin 10     ........
.............        void setup() {         // .................
.........       Serial.begin (9600);
 ...........    pinMode(trigPin, OUTPUT);   //.................
............      pinMode(echoPin, INPUT);     ...........
}                  //....................
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;             // ..................................
  Serial.print(distance);
   Serial.print("cm");
   Serial.println();
   delay(100);
}


You can connect any output motors or LEDs depends on your application.For that, we need to add some extra code just add conditions to the code but remember to define any output first.

if (distance < 4) {
    digitalWrite(led,HIGH);
  digitalWrite(led2,LOW);
}
  else {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(100);

Ultrasonic SR04 sensor distance measuring and obstacle avoiding sensor robot projectDefine the led pins as we define echo and trig pin as an in put or output.
We will see some of the great projects using the Ultrasonic sensor in coming posts.Here is the picture of the Obstacle avoiding Robot that I have made.The Robot has many functionalities we will see this in coming soon so stay connected and subscribe to our YouTube channel for video Tutorials.
Stay motivated and happy.

Share To:

Post A Comment:

0 comments so far,add yours