Displaying Distance Using Ultrasonic Sensor on OLED Display

 

1. Activity Name:

Displaying Distance Using Ultrasonic Sensor on OLED Display


2. Materials Required:

  • Arduino Uno/Nano
  • Ultrasonic Sensor (HC-SR04)
  • OLED Display (SSD1306 - 0.96” I2C)
  • Connecting Wires
  • Breadboard (Optional)

3. Explanation:

This activity involves measuring the distance of an object using an HC-SR04 ultrasonic sensor and displaying the measured distance on an OLED screen using an Arduino. The ultrasonic sensor emits sound waves, calculates the time taken for the waves to bounce back, and converts it into distance using the speed of sound formula. The OLED screen then displays the real-time distance in centimeters.

Working Principle:

  1. The HC-SR04 sensor emits ultrasonic waves through its TRIG pin.
  2. The waves reflect back after hitting an obstacle and are received by the ECHO pin.
  3. The time difference between sending and receiving waves is calculated.
  4. The Arduino converts this time into distance using the formula: Distance=Time×0.03432
  5. The calculated distance is displayed on the OLED screen in real time.

4. Connections:

ComponentArduino Pin
Ultrasonic Sensor TRIGD9
Ultrasonic Sensor ECHOD10
OLED Display VCC5V
OLED Display GNDGND
OLED Display SDAA4 (SDA)
OLED Display SCLA5 (SCL)

5. Conclusion:

This project successfully demonstrates real-time distance measurement using an ultrasonic sensor and an OLED display. Students learned about sensor interfacing, real-time data visualization, and practical applications such as obstacle detection, smart parking, and automation systems. The hands-on approach sparked interest in sensor-based automation and practical electronics applications.

 

Code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Ultrasonic Sensor Pins
#define TRIG_PIN 9
#define ECHO_PIN 10

void setup() {
  Serial.begin(115200);
 
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
}

void loop() {
  float distance = getDistance();
 
  // Check if distance is within valid range
  if (distance > 2 && distance < 400) {
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    // Display Distance on OLED
    display.clearDisplay();
    display.setCursor(20, 20);  // Centering the text
    display.setTextSize(1);
    display.print("Dist: ");
    display.print(distance);
    display.print("cm");
    display.display();
  } else {
    Serial.println("Out of range!");
  }

  delay(500); // Keep a short delay for stable reading
}

// Function to Measure Distance
float getDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000); // Timeout at 30ms
  if (duration == 0) return -1; // Return -1 if no echo received

  float distance = (duration * 0.0343) / 2; // Convert to cm
  return distance;
}