Distance Measuring Device | Ultrasonic sensor | OLED Display



Components

  1. ESP8266 / Arduino: Microcontroller to run the code.
  2. OLED Display: To display the distance measured by the ultrasonic sensor.
  3. Ultrasonic Sensor: To measure distance.
  4. 3.7V Lithium-ion Battery: Power supply for the project.
  5. 5V Step-up Charging Module: To step up the voltage from 3.7V to 5V.
  6. Switch: To turn the project on and off.

Code Explanation

The provided code reads the distance from an ultrasonic sensor and displays the measured distance in centimeters and inches on an OLED display.

Libraries Needed

  1. Adafruit_GFX: Graphics library for the OLED display.
  2. Adafruit_SSD1306: OLED display library.

Wiring Connections

  • OLED Display:

    • VCC to 3.3V
    • GND to GND
    • SCL to GPIO 5 (D1 on ESP8266)
    • SDA to GPIO 4 (D2 on ESP8266)
  • Ultrasonic Sensor:

    • VCC to 5V (from step-up module)
    • GND to GND
    • TRIG to GPIO 12 (D6 on ESP8266)
    • ECHO to GPIO 14 (D5 on ESP8266)
  • Power Supply:

    • 3.7V battery connected to the input of the step-up module.
    • The output of the step-up module to the ESP8266's VIN pin (providing 5V).

Source Code:

/***MKinventions***************/
#include <SPI.h>
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> // OLED display configuration #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Ultrasonic sensor pins const int echoPin = 14; const int trigPin = 12; // Variables to hold distance measurements long distance; long distanceInch; long duration; void setup() { Serial.begin(115200); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // Initialize the OLED display Wire.begin(); if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;); } display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(1); display.setCursor(40, 30); display.println("Restarted"); display.display(); } void loop() { measureDistance(); Serial.println("DISTANCE CM : " + String(distance)); Serial.println("DISTANCE INCH: " + String(distanceInch)); delay(500); // Update the OLED display display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(2); display.setCursor(15, 0); // Column, Row display.print("Distance"); display.setTextSize(1); display.setCursor(0, 22); display.print("CM : " + String(distance)); display.setCursor(0, 42); display.print("INCH: " + String(distanceInch)); display.display(); } void measureDistance() { // Trigger the ultrasonic pulse digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the duration of the pulse duration = pulseIn(echoPin, HIGH); // Calculate distance in centimeters and inches distance = duration * 0.034 / 2; distanceInch = duration * 0.0133 / 2; }

Download and Install Libraries

Ensure you have the following libraries installed in your Arduino IDE:

  1. Adafruit GFX Library
  2. Adafruit SSD1306

You can install these libraries from the Arduino Library Manager (Sketch > Include Library > Manage Libraries...).

Notes

  • Make sure your wiring connections are secure and correct.
  • If you encounter issues with the ultrasonic sensor readings, consider replacing the sensor.
  • Verify the power supply configuration to ensure the step-up module is providing the necessary voltage to the ESP8266.

This project reads the distance from the ultrasonic sensor and displays it on the OLED screen. The code can be modified as needed to fit your specific requirements or microcontroller.

Post a Comment

0 Comments