Arduino Projects

Distance Measurement Using Arduino Ultrasonic Sensor and Oled

Arduino Ultrasonic Sensor, Example:

In this example, I am going to explain how to use the most popular HC-SR04 Ultrasonic Sensor with the Arduino and display the distance measurement value on the OLED display module. You can use an ultrasonic sensor for the obstacles detection in a robot, you can make a water level monitoring system, and you can even make a security system.

The HC-SR04 ultrasonic sensor uses sound waves to determine the distance between the sensor and an object. It emits an ultrasonic pulse and measures the time it takes for the pulse to bounce back after hitting an object. Since the speed of sound in air is relatively constant, you can calculate the distance by using the formula:

Distance = (Time × Speed of Sound) / 2

allpcb circuit

Where:

Time is the time it takes for the ultrasonic pulse to travel to the object and back.

Speed of Sound is the speed at which sound travels in air (approximately 343 meters per second at room temperature).




Amazon Links:

Arduino Nano USB-C Type (Recommended)

SSD1306 Oled display

HC-SR04 Ultrasonic Sensor

5V Buzzer

*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.




Arduino Ultrasonic Sensor and Oled Circuit:

It has a total of four pins clearly labeled as Vcc, Trig, Echo,and Gnd.

Arduino course

Connect the Vcc and Gnd Pins to the Arduino 5V and GND pins. Connect the Trigger and Echo pins to the Arduino D10 and D9 respectively.

The SSD1306 Oled display module VCC and GND pins are connected to the Arduino 3.3V and GND pins. Whereas the SDA and SCL pins are connected to the Arduino A4 and A5 pins which are the I2C pins. A4 is the SDA and A5 is the SCL. I have already explained this in the Arduino Pinout. Now let’s go ahead and take a look at the programming.



Arduino Ultrasonic Sensor and Oled Programming:

In the Oled display module example, I have already explained how to install these libraries. The Trigger and Echo pins of the Ultrasonic sensor are connected to the digital pins D10 and D9. Next, I defined two variables for storing the duration and distance values.

All these instructions are used with the Oled display module and I have already explained these. The trigger pin is set as output and the Echo pin is set as the input.

The trigPin is set to LOW to clear it to avoid any residual triggers.

A short 2-microsecond delay is added.

The trigPin is set to HIGH for 10 microseconds to trigger the ultrasonic pulse.

The trigPin is set back to LOW.

pulseIn() is used to measure the duration for which the echoPin remains HIGH, which corresponds to the time taken for the sound wave to travel to the object and back.

The distance is calculated using the formula and stored in the distance variable.

The calculated distance is printed to the Serial Monitor.

If you don’t want to print values on the serial monitor then you can deactivate the Serial communication and delete all these Serial.print() functions. I used it only for the debugging purposes just to confirm that everything is working. Anyway,

The OLED display is cleared.

Text size is set to 2, and “Distance” is printed at coordinates (0, 10) on the display.

Text size is set to 3, and the calculated distance is printed at coordinates (0, 35) on the display, along with the unit “cm”.

Finally, the display content is updated using the display.display() function.

Arduino course



video tutorial

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button