Ultrasonic Sensor with Ardiuno

Introduction-

An ultrasonic sensor is a device that uses ultrasonic waves to measure an object’s distance. Ultrasonic transducers which are the microphone and speaker tandems  send and receive ultra-high frequency sound waves to obtain an object’s distance or proximity. The ultra-high frequency sound waves are reflected from an object’s surface creating a unique echo pattern

5

It can be used to determine the distance of an object in the range of 2 cm – 400 cm

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

Working-

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.

4

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. It comes complete with ultrasonic transmitter and receiver modules

3

Technical Specifications-

Here’s a list of some of the HC-SR04 ultrasonic sensor features and specs:

  • 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
  • Trigger Input Pulse width: 10uS
  • Dimension: 45mm x 20mm x 15mm

1

The module has only 4 pins,as shown Above:

  • VCC: +5VDC
  • Trigger (INPUT)
  • Echo (OUTPUT)
  •  GND

Materials Required-

  • Arduino UNO
  • Ultrasonic Sensor HC-SR04
  • Jumper Wires
  • Breadboard

Schematics-

In order to generate the ultrasound we need to set the Trigger Pin 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.

2

6

Now how to calculate distance?

We know that,

\textbf{Distance = Speed x Time}

The speed of sound waves is 343 m/s.

So,

\textbf{Total Distance }= \frac{\textbf{343 x } \textbf{Time of High(Echo) Pulse}}{\textbf{2}}

Total distance is divided by 2 because signal travels from HC-SR04 to object and returns to the module HC-SR-04.

The code-

 /*
Author:Dharmendra Kumar yadav
*/
// defines pins numbers
const int trigPin = 3;
const int echoPin = 2;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Ultrasonic Sensor code with ping Library-

Download Ping : Download NewPing Library

Put the “NewPing” folder in “libraries\”.
In the Arduino IDE, create a new sketch (or open one) and select from the menubar “Sktech->Import Library->NewPing“.

Code-

 /*
Author:-Dharmendra Kumar Yadav
*/
// Include NewPing Library
#include "NewPing.h"
// Hook up HC-SR04 with Trig to Arduino Pin 3 Echo to Arduino pin 2
// Maximum Distance is 400 cm
#define TRIGGER_PIN 3
#define ECHO_PIN 2
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
float distance;
void setup()
{
Serial.begin (9600);
}
void loop() {
distance = sonar.ping_cm();
// Send results to Serial Monitor
Serial.print("Distance = ");
if (distance >= 400 || distance <= 2) 
  {
   Serial.println("Out of range");
  }
else 
   {
   Serial.print(distance);
   Serial.println(" cm");
   delay(500);
   }

delay(500);

}
Example:
NewPing sonar(3, 2, 200);

This initializes NewPing to use pin 3 for trigger output, pin 2 for echo input, with a maximum ping distance of 200cm. max_cm_distance is optional [default = 400cm]. If connecting using a single pin, specify the same pin for both trigger_pin and echo_pin as the same pin is doing both functions.

Function used-

  • sonar.ping(); – Send a ping, returns the echo time in microseconds or 0 (zero) if no ping echo within set distance limit
  • sonar.ping_in(); – Send a ping, returns the distance in inches or 0 (zero) if no ping echo within set distance limit
  • sonar.ping_cm(); – Send a ping, returns the distance in centimeters or 0 (zero) if no ping echo within set distance limit

 

Code with New ping Library Rewritten to use Duration instead of Distance-

/*
Author:Dharmendra kumar Yadav
*/
// Include NewPing Library
#include "NewPing.h"

// Hook up HC-SR04 with Trig to Arduino Pin 3, Echo to Arduino pin 2
// Maximum Distance is 400 cm

#define TRIGGER_PIN 3
#define ECHO_PIN 2
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
float duration, distance;
void setup() {
Serial.begin (9600);
}
void loop() {
duration = sonar.ping();
// Determine distance from duration
// Use 343 metres per second as speed of sound

distance = (duration / 2) * 0.0343;

// Send results to Serial Monitor
Serial.print("Distance = ");
if (distance >= 400 || distance <= 2) {
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
delay(500);
}

Interfacing Multiple Ultra sonic sensor-

Circuit-

we have interface 3 ultra sonic sensor as shown below

8

 

The Code-

In this code we have interface 3 sensor name Right,Back,Frontand Left

/*
Author:Dharmendra Kumar Yadav
*/

#define trigPin1 3
#define echoPin1 2
#define trigPin2 5
#define echoPin2 4
#define trigPin3 7
#define echoPin3 6
long duration, distance, RightSensor,FrontSensor,LeftSensor;
void setup()
{
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
}

void loop() {
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;
SonarSensor(trigPin3, echoPin3);
FrontSensor = distance;

Serial.print(LeftSensor);
Serial.print(" - ");
Serial.print(FrontSensor);
Serial.print(" - ");
Serial.println(RightSensor);
}

void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
}

That all for today!

Leave a comment

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started