Measuring distance using the ultrasonic sensor

Ultrasonic sensors work by sending out a sound wave at a frequency too high for humans to hear. When this sound wave hits an object, it bounces back and is detected by the sensor. The time it takes for the sound wave to bounce back is used to calculate the distance to the object.

Overview


The ultrasonic sensor is a device that can measure distance by sending out sound waves and measuring how long it takes for them to bounce back.

It can be used to measure the distance between two objects, or to detect whether an object is in the way.

The ultrasonic sensor is often used in robots, as it can help the robot avoid obstacles.


In this tutorial we will learn together how to use the ultrasonic sensor to measure distance and print the value through the serial monitor!


Getting the items


For this project you will need the following components: 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Ultrasonic Sensor (HC-SR04)
17 QAR
Sale Off
Voltaat Jumper Wires - Male to Female (40 Pack)
10 QAR

Wiring it up


To obtain the correct readings from the sensor, follow the instructions below as well as the image that demonstrates how to connect the sensor to the Arduino.



Connections from the Arduino to the ultrasonic sensor:

• Arduino VCC pin → ultrasonic sensor VCC pin (+ pin)

• Arduino pin 11 → ultrasonic sensor Trig pin

• Arduino pin 11 → ultrasonic sensor Echo pin

• Arduino GND pin → ultrasonic sensor GND pin (- pin)


Coding


The function of this sketch is to calculate distance using the ultrasonic sensor, it sends a wave and waits for the echo, if there's an object in front of the sensor it will reflect the wave and we can measure the time that took to come back, with that information we can calculate the distance using an equation based on the speed of sound in air.


The code is basic and does not require any libraries; it will teach you about the sensor and how it works, allowing you to work on more complicated projects.


  1. /*
  2.   Voltaat learn (https://www.voltaat.com)
  3.   Link for full tutorial: https://www.voltaat.com/blogs/tutorials/measuring-distance-using-the-ultrasonic-sensor
  4.   Link for libraries: There is no libraries used in this sketch
  5.  
  6.   This is an Arduino sketch that uses an ultrasonic sensor to compute the distance from an object
  7.  
  8.   Connections from the Arduino to the ultrasonic sensor:
  9.   • Arduino VCC pin → ultrasonic sensor VCC pin (+ pin)
  10.   • Arduino pin 11 → ultrasonic sensor Trig pin
  11.   • Arduino pin 11 → ultrasonic sensor Echo pin
  12.   • Arduino GND pin → ultrasonic sensor GND pin (- pin)
  13.  
  14.  
  15. */
  16.  
  17. //Define pin numbers
  18. #define trig 11
  19. #define echo 12
  20. //Defines variables and initialize their values
  21. int distance =0, t =0;
  22.  
  23. //Commands inside void setup run once
  24. void setup(){
  25. //Start the serial monitor at 9600 baud rate (9600 bits per second)
  26. Serial.begin(9600);
  27. //Sets the trig Pin as an output
  28. pinMode(trig, OUTPUT);
  29. //Sets the echo Pin as an intput
  30. pinMode(echo, INPUT);
  31.  
  32. }
  33.  
  34. //Commands inside void loop run forever
  35. void loop(){
  36. //Clears the trig pin
  37. digitalWrite(trig, LOW);
  38. //delay for 5 micro seconds
  39. delayMicroseconds(5);
  40. //Sets the trig Pin HIGH for 10 micro seconds
  41. digitalWrite(trig, HIGH);
  42. delayMicroseconds(10);
  43. digitalWrite(trig, LOW);
  44. //Reads the echoPin, returns the sound wave travel time in microseconds
  45. t = pulseIn(echo, HIGH);
  46. //Calculating the distance
  47. distance =(t /57);
  48. //Print to serial monitor
  49. Serial.print("Distance= ");
  50. Serial.print(distance);
  51. Serial.println(" Cm");
  52. delay(1000);
  53. }

Testing it out


You may now access the serial monitor on your Arduino IDE by clicking on it.


The serial monitor is great way that’s enables you to make a communication between your computer and Arduino by sending and receiving different commands and information using the USB cable.


Now as we see in the following image the serial monitor displays the distance between the sensor and an object, and the value is updated and printed every second because of the delay we made in our code, you should also make sure you have chosen the right baud rate (9600) as specified in the code.


You can ensure that the sensor is calibrated and that the value it provides is completely correct by comparing it with one of the measuring tools, such as a ruler. For example, the speed of sound varies slightly depending on temperature and pressure, so the accuracy of the sensor may differ from the value in the equation that we used.


Resources 


Arduino Code

Fritzing Wiring file

Related Tutorials


How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

Liquid error: Could not find asset snippets/zend_pre_order_theme_snippet.liquid
Liquid error: Could not find asset snippets/uploadkit.liquid