Distance Measurement Using Arduino Ultrasonic Sensor and Oled
Table of Contents
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
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)
*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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> const int trigPin = 10; const int echoPin = 9; // defines variables long duration; int distance; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); // Starts the serial communication pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input display.begin(SSD1306_SWITCHCAPVCC, 0x3C); delay(2000); display.clearDisplay(); display.setTextColor(WHITE); delay(10); } 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); display.clearDisplay(); display.setTextSize(2); display.setCursor(0, 10); display.print("Distance"); display.setTextSize(3); display.setCursor(0, 35); display.print(distance); display.print("cm"); display.display(); } |
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.
video tutorial