Title :
Design a distance measurement system using Arduino and an ultrasonic sensor with OLED display and
performance validation through data analysis.
Objective :
To build a system that measures the distance of an object using the HC-SR04 ultrasonic sensor and
displays the result on a 0.96" OLED screen. The system calculates and displays the error and accuracy
based on (10-100) cm reference distance.
Apparatus :
• Arduino Uno
• HC-SR04 Ultrasonic Distance Sensor
• OLED Display (SSD1306, 128x64, I2C interface)
• Jumper wires
• Breadboard
Experimental Setup :
Trig Pin of HC-SR04 → Digital Pin 9
Echo Pin of HC-SR04 → Digital Pin 10
OLED SDA → A4
OLED SCL → A5
OLED and Sensor connected to 5V and GND
Figure : Hardware Implementation
The object is placed at exactly 30 cm from the sensor. Distance, error, and accuracy are shown on the
OLED screen. The system is powered via USB from a PC. Measured distance, error, and accuracy are
shown on the OLED.
Codes of the Program :
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define TRIG_PIN 11
#define ECHO_PIN 12
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
long duration;
float distance_cm;
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED init failed"));
while (true); // Halt
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Distance Measurer");
display.display();
delay(1000);
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance_cm = duration * 0.0343 / 2;
display.clearDisplay();
display.setCursor(0, 0);
display.print("Distance: ");
display.print(distance_cm);
display.println(" cm");
display.display();
delay(1000);
Result :
SL Actual Distance Measured Distance Error (cm) Accuracy (%)
(cm)
1 10 10.3 0.3 97%
2 20 20.6 0.6 97%
3 30 31.05 1.05 96.5%
4 40 40.03 0.03 99.93%
5 50 50.02 0.02 99.96%
6 60 60.2 0.2 99.67%
7 70 69.8 0.2 99.71%
8 80 81.3 1.3 98.38%
9 90 90.7 0.7 99.22%
10 100 100.03 0.03 99.97%
Table 1: Measured Distance & Accuracy
Figure 2 : Actual vs Measured Distance
Figure 3: Error vs Accuracy
Discussion:
The HC-SR04 ultrasonic sensor provides reliable measurements within its range (2 cm – 400 cm). The
OLED screen effectively displays real-time results, including error and accuracy. The accuracy was above
97% in all cases, with very small errors due to surface reflectivity and air fluctuations. The system is ideal
for distance sensing tasks in robotics and automation.
Conclusion:
The distance measurement system using Arduino, HC-SR04, and an OLED display was successfully
designed and implemented. It consistently measured distances from 10 cm to 100 cm with high accuracy.
The system is compact, low-cost, and highly suitable for embedded applications.