Arduino

How to Use Ultrasonic Sensor with Arduino

Arduino is a microcontroller board used by engineers to design multiple projects. Arduino makes it easy to interact with microcontrollers and design products of our choice. Arduino has capability of interfacing with multiple sensor hardware modules. One of the most popular sensors used with Arduino is ultrasonic distance sensor. It plays a vital role in building Arduino based robotic projects where we can execute different instructions based on distance measured by Arduino. Let’s see how we can use this sensor with Arduino.

Ultrasonic Sensor with Arduino

HC-SR04 is one of the most used ultrasonic sensors with Arduino. This sensor determines how far an object is. It uses SONAR to determine object distance. Normally it has a good range of detection with accuracy of 3mm however sometimes it is difficult to measure soft materials distance like cloth. It comes with a built-in transmitter and receiver. Following table describes the technical specifications of this sensor.

Characteristics Value
Operating Voltage 5V DC
Operating Current 15mA
Operating Frequency 40KHz
Min Range 2cm/ 1 inch
Max Range 400cm/ 13 feet
Accuracy 3mm
Measuring Angle <15 degree

Pinout

Ultrasonic sensor HC-SR04 has four pins:

  • Vcc: Connect this pin to Arduino 5V
  • Gnd: Connect this pin with Arduino GND
  • Trig: This pin receives controlling signal from Arduino digital pin
  • Echo: This pin sends a pulse or signal back to Arduino. Received back pulse signal is measured to calculate distance.

How Ultrasonic Works

Once the ultrasonic sensor is connected to Arduino, the microcontroller will generate a signal pulse on the Trig pin. After sensors receive an input at the Trig pin an ultrasonic wave is automatically generated. This emitted wave will hit the surface of an obstacle or object whose distance we must measure. After that, the ultrasonic wave will bounce back to the receiver terminal of the sensor.

A picture containing text Description automatically generated

Ultrasonic sensor will detect the reflected wave and calculate the total time taken by wave from sensor to object and back to sensor again. Ultrasonic sensor will generate a signal pulse at Echo pin which is connected to Arduino digital pins once the Arduino receives signal from Echo pin it calculates the total distance between object and sensor using Distance-Formula.

How to Connect Arduino with Ultrasonic Sensor

Arduino digital pins generate a 10 microseconds pulse signal which is given to ultrasonic sensor pin 9 while to receive incoming signal from ultrasonic sensor another digital pin is used. Sensor is powered using an Arduino ground and 5V output pin.

Ultrasonic Sensor Pin Arduino Pin
Vcc 5V Output Pin
Trig PIN9
Echo PIN8
GND GND

Trig and Echo pins can be connected to any of Arduino digital pins. Below given image represents the wiring diagram of Arduino with HC-SR04 ultrasonic sensor.

Schematics

How to Program Ultrasonic Sensor Using Arduino

To program an ultrasonic sensor connect it with an Arduino using the above diagram. Now we must generate a pulse signal at the Trig pin of the ultrasonic sensor.

Generate a 10-microseconds pulse at pin 9 of Arduino using digitalWrite() and delayMicroseconds() functions.

digitalWrite(9, HIGH);
delayMicroseconds(10);
digitalWrite(9, LOW);

To measure the output from the sensor at pin 8 use pulseIn() function.

Duration_microsec = pulseIn(8, HIGH);

Once the pulse is received from the echo pin of the sensor to Arduino pin number 8. Arduino will calculate distance using the above formula.

Distance_cm = 0.017 * Duration_microsec;

Code

int triggerPin = 9;    /* PIN 9 is set for sensor TRIG pin*/
int echoPin = 8;    /* PIN 8 is set for sensor ECHO pin input*/
float durationMicroSec, distanceincm;
void setup() {
  Serial.begin (9600);  /*serial communication started*/
  /* TriggerPin is set as Output*/
  pinMode(triggerPin, OUTPUT);
  /* Echo pin 9 is set as Input*/
  pinMode(echoPin, INPUT);
}
void loop() {
  /* generate 10-microsecond pulse to TRIG pin*/
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  /* measure duration of pulse from ECHO pin*/
  durationMicroSec = pulseIn(echoPin, HIGH);
  /* calculate the distance*/
  distanceincm = 0.017 * durationMicroSec;
  /* print the value to Serial Monitor*/
  Serial.print("distance: ");
  Serial.print(distanceincm);  /*Print distance in cm*/
  Serial.println(" cm");
  delay(1000);
}

In above code pin 9 is set as trigger while pin 8 is set as the output pin for ultrasonic sensor. Two variables durationMicroSec and distanceincm is initialized. Using pinMode() function pin 9 is set as input while pin 8 is set as output.

In the loop section of code using the formula explained above distance is calculated and the output is printed on serial monitor.

Hardware

Place the object near the ultrasonic sensor.

Output

Approximate distance of 5.9cm is shown by the ultrasonic sensor on the serial monitor.

Now move the object away from ultrasonic sensor.

Output

Approximate distance of 10.8cm is shown by the ultrasonic sensor on the serial monitor.

Conclusion

Ultrasonic sensor is a great tool for measuring distance using contactless operation. It has vast application in DIY electronics projects where we need to work with distance measuring, checking presence of an object and leveling or correct position of any equipment. This article covers all parameters which are needed to operate an ultrasonic sensor with Arduino.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.