Ultrasonic Sensor Using Arduino

Ultrasonic Sensor

The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package from 2 cm to 400 cm or 1” to 13 feet.

Image result for ultrasonic sensor

The operation is not affected by sunlight or black material, although acoustically, soft materials like cloth can be difficult to detect. It comes complete with ultrasonic transmitter and receiver module.

Technical Specifications

  • Power Supply − +5V DC
  • Quiescent Current − <2mA
  • Working Current − 15mA
  • Effectual Angle − <15°
  • Ranging Distance − 2cm – 400 cm/1″ – 13ft
  • Resolution − 0.3 cm
  • Measuring Angle − 30 degree

How It Works – Ultrasonic Sensor

It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.

Image result for ultrasonic sensor

The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.

In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.

Related image

For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward.  So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.

Ultrasonic-Sensor-Equasions

Components needed for this tutorial

  • Ultrasonic Sensor HC-SR04
  • Arduino Board
  • Breadboard and Jump Wires
  • Power Source

ULTRASONIC RANGE FINDER SETUP FOR SERIAL MONITOR OUTPUT

Let’s start by making a simple ultrasonic range finder that will output distance measurements to your serial monitor. If you want to output the readings to an LCD instead, check out the next section. Connecting everything is easy, just wire it up like this:

Arduino Ultrasonic Range Finder

Once you have everything connected, upload this program to the Arduino:

/*  DHT11/ DHT22 Sensor Temperature and Humidity Tutorial
 *  www.genbays.com 
 */
 
#define trigPin 10
#define echoPin 13
float duration, distance; //Declares the variables duration and distance.
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344; //Defines the distance variable as the duration
  
  if (distance >= 400 || distance <= 2){
    Serial.print("Distance = ");
    Serial.println("Out of range");
  }
  else {
    Serial.print("Distance = ");
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

ULTRASONIC RANGE FINDER WITH LCD OUTPUT

If you want to output the distance measurements to a 16X2 LCD, follow this diagram to connect the range finder and LCD to your Arduino:

/*  DHT11/ DHT22 Sensor Temperature and Humidity Tutorial
 *  www.genbays.com 
 */
 #include <LiquidCrystal.h>
#define trigPin 10
#define echoPin 13

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  
  if (distance >= 400 || distance <= 2){
    lcd.print("Out of range");
    delay(500);
  }
  else {
    lcd.print(distance);
    lcd.print(" cm");
    delay(500);
  }
  delay(500);
  lcd.clear();
}