​Arduino: Interfacing Ultrasonic Sensor

by AlanJacob688 in Circuits > Arduino

397 Views, 0 Favorites, 0 Comments

​Arduino: Interfacing Ultrasonic Sensor

Ultrasonic.jpg

In this instructable, we are going to work with Ultrasonic Sensor and run the basic test using Arduino Uno.

First we will start with introduction to both Arduino and Ultrasonic Sensor and then we will proceed to make the connection and add codes for interfacing sensor.

Let's get started.

Supplies

Arduino Uno

Ultrasonic Sensor

Jumper Wires (male-female)

About Ultrasonic Sensor

hc-sr04-ultrasonic-sensor-module-500x500.jpg

An Ultrasonic Sensor is an electronic device that is used to detect the distance of the object. The sensor converts electrical signals into Ultrasonic Sound waves which travel faster than the Normal Audible sound waves and emits them then receives the reflected sound waves and converts the sound waves into electrical signals and determines the distance of the object.

The Ultrasonic Sensor has to trigger the pulse for at least 10 microseconds (10us).

Pinout of 4 pins:

VCC - positive terminal of the source should be connected.

GND - negative or ground terminal of the source should be connected.

TRIG - Acts as an output that emits the Ultrasonic Sound waves.

ECHO - Acts as an input that receives the reflected Ultrasonic Sound waves.

Datasheet stated features:

  • Operating Voltage: 5V DC
  • Operating Current: 15mA
  • Measure Angle: 15°
  • Ranging Distance: 2cm - 4m

About Arduino

arduino.jpg

Arduino is an open-source microcontroller board running on the ATmega328p microcontroller. The board is consists of digital as well as analog I/O pins.

Coding is done and uploaded by Arduino IDE.

The programming language used by the Arduino is C, C++.

Circuit Connection

Ultrasonic.jpg
Screenshot (287).png
Screenshot (288).png

Connecting the Ultrasonic Sensor to the Arduino UNO using jumper wires.

Connection of Ultrasonic to Arduino:

  1. GND --- GND
  2. VCC --- 5V or VIN
  3. TRIG --- Pin 2 (Digital pin)
  4. ECHO --- Pin 3 (Digital pin)

You can use a breadboard but it is not important.

Note: Don't let the metal surface of the ultrasonic sensor to any other metal surface that may cause it to short circuit.

Coding

/* Ultrasonic Sensor Testing Arduino Code 
Author - AlanJacob688
*/

/* These two are used to define the pin number of the trig and echo in arduino */

#define trig 2
#define echo 3

long duration; // This will calculate the duration of the sound to return
int distance; // calculates the distance

void setup() {

  // put your setup code here, to run once:
  /* Here initialize the pinMode we are goind to have for trig and echo*/
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);

   /*For serial monitor */
   Serial.begin(9600); //Recommended Baud-rate
   Serial.println("Ultrasonic Sensor");
   

}

void loop() {

  // put your main code here, to run repeatedly:
   /*Now here we are going to add the codes that are needed to be run repeatedly*/

   while(!Serial.available()){  //Only runs the code when we open the serial monitor

    digitalWrite(trig,LOW);
    delayMicroseconds(2);
    digitalWrite(trig,HIGH);
    delayMicroseconds(10);
    digitalWrite(trig,LOW);

    Serial.println("Obtaining...");

    duration = pulseIn(echo,HIGH);
    distance = ((duration*0.03435)/2); // The output is in Centimeters (cm)

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

    delay(2000);
   }
}

Downloads

Code Explanation

#define trig 2
#define echo 3
long duration; 
int distance;

First, we will define the pin of Arduino we are using for trig and echo using #define. Then we will declare two variables:

  1. long duration - duration is used to store the duration of the pulse sent and received from an Ultrasonic Sensor. long is used as the result we get is larger than what int could store.
  2. int distance - distance is stored here (in centimeters).
void setup() {
  // put your setup code here, to run once:

  /* Here initialize the pinMode we are goind to have for trig and echo*/
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);

   /*For serial monitor */
   Serial.begin(9600); //Recommended Baud-rate
   Serial.println("Ultrasonic Sensor");
}<br>

void setup() is used to run the code for one time only.

Now inside void setup(), declare the pinMode (used to determine the state of the pin, it can either be INPUT or OUTPUT) of trig and echo.

trig is emitting the pulse so it will be set to OUTPUT, while echo is receiving the pulse and thus is set to INPUT.

We are using a Serial Monitor to get our results and thus we have to declare its Baud-rate.

void loop() {
  // put your main code here, to run repeatedly:
   /*Now here we are going to add the codes that are needed to be run repeatedly*/

   while(!Serial.available()){  //Only runs the code when we open the serial monitor

    digitalWrite(trig,LOW);
    delayMicroseconds(2);
    digitalWrite(trig,HIGH);
    delayMicroseconds(10);
    digitalWrite(trig,LOW);

    Serial.println("Obtaining...");

    duration = pulseIn(echo,HIGH);
    distance = ((duration*0.03435)/2); // The output is in Centimeters (cm)

    Serial.print(distance);
    Serial.println("cm");
    delay(2000);
   }
}<br>

void loop() is used to run code repeatedly.

Now inside void loop(), we are writing the code that will trigger as well as read the results and print it to the Serial monitor.

At first, we will trigger the sound wave using trig, we will use digitalWrite(Pin, Value). Write a HIGH or a LOW value in Value. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.

trig is set to LOW at the beginning for 2 microseconds and then it is set to HIGH for 10 microseconds as given in the datasheet and then again set it back to the LOW.

Now, the pulseIn() function will be set the echo pin to HIGH and will receive the reflected sound waves.

If the value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW. Returns the length of the pulse in microseconds. Works on pulses from 10 microseconds to 3 minutes in length.

The value from pulseIn() is stored in the duration variable then we will calculate the distance in centimeters.

The formula for calculations of distance:

Distance(in cm) = pulse width(microseconds) / 58. As the distance between the sensor and the object increases the pulse width also increases with it.

Now, we have to take the speed of sound into consideration

The speed of sound at 0-degree Celcius is 331.5 m/s.

The speed of sound at 20-degree Celcius is 343.5 m/s.

* Check your temperature and search on the internet for the value.

I am taking the speed of sound at 20-degree for my calculations but it can be different for you.

Speed of sound at 20-degree Celcius = 343.5 m/s

= (343.5 *100)/1000000 = 0.03435 cm.

Now we know that the duration of sound emitted and received both are included when we get the result from pulseIn() and thus we have to discard the time of emission and only consider the time of receiving and for that we are dividing the duration variable by 2.

And thus, distance = (duration*0.03435)/2.

At last, you just have to print the values in Serial Monitor.


Testing

Screenshot (286).png

Upload the code to Arduino Uno and check for the status to show "Done Uploading".

Now open the Serial Monitor and place any object or hand in front of the sensor and you will get your result.