Featured

Search

How to Use Ultrasonic Sensor SR04 with arduino and How ultrasonic sensor works

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.The working of ultrasonic sensor and the implementation of ultrasonic SR04 SONAR sensor module with arduino. We will discus 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 sound it works on Sound waves.Ultrasonic is a sensor device that works with sound waves.It works on the phenomenon of sending and receiving of ultrasonic sound waves.By sound waves it measures the distance and obstacles.Its a sensor that measures the distance or Detects obstacle.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 obstacle with ultrasonic sound waves comes from underwater communication. 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. It measures distance and existence of the obstacle with in the time of sending and receiving of sound wave.
The total traveling distance of sound wave can be calculated by using this formula:


The SR04 obstacle avoiding module is easily interface with arduino. The SR04 ultrasonic module is a high accuracy range detector module.The Distance range of SR04 SONAR is from 2 cm to 400 cm
The module works fine in almost all good conditions but remember in high noise and in some situations and materials the module will not work. The sensor 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 between each other.


working principle of distance sensor ultrasonic SONAR sensor

The SR04 module has 4 pins other than power supply and ground one is echo that is used for receiving signals and the other is 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

circuit diagram of ultrasonic sonar sensor.the pinout of ultrasonic sensor


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 simple 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 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);

Define 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 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....!!!

Ultrasonic SR04 sensor distance measuring and obstacle avoiding sensor robot project
Previous
Next Post »

Featured