Arduino Nano Based Ultrasonic Distance Measurement with OLED Display
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary of the Circuit
This circuit integrates an Arduino Nano microcontroller with an OLED 1.3" display and an HC-SR04 Ultrasonic Distance Sensor. The purpose of the circuit is to measure distances using the ultrasonic sensor and display the measured values on the OLED screen. The Arduino Nano serves as the central processing unit, controlling the sensor and managing the display output.
Component List
Arduino Nano
- Description: A compact microcontroller board based on the ATmega328P.
- Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK.
OLED 1.3" Display
- Description: A small display for showing graphics and text.
- Pins: GND, VCC, SCL, SDA.
HC-SR04 Ultrasonic Distance Sensor
- Description: A sensor for measuring distances using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND.
Wiring Details
Arduino Nano
- GND connected to OLED GND and HC-SR04 GND.
- D9 connected to HC-SR04 ECHO.
- D10 connected to HC-SR04 TRIG.
- VIN connected to HC-SR04 VCC and OLED VCC.
- A5 connected to OLED SCL.
- A4 connected to OLED SDA.
OLED 1.3" Display
- GND connected to Arduino Nano GND.
- VCC connected to Arduino Nano VIN.
- SCL connected to Arduino Nano A5.
- SDA connected to Arduino Nano A4.
HC-SR04 Ultrasonic Distance Sensor
- VCC connected to Arduino Nano VIN.
- TRIG connected to Arduino Nano D10.
- ECHO connected to Arduino Nano D9.
- GND connected to Arduino Nano GND.
Documented Code
/*
* This Arduino Sketch interfaces with an OLED display and an HC-SR04
* Ultrasonic Distance Sensor. The distance measured by the sensor is
* displayed on the OLED screen.
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#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);
#define TRIG_PIN 10
#define ECHO_PIN 9
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
// Initialize the ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Clear the trigger pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Set the trigger pin HIGH for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pin
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance
float distance = duration * 0.034 / 2;
// Display the distance on the OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.print("Distance: ");
display.print(distance);
display.println(" cm");
display.display();
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Wait for a short period before the next measurement
delay(500);
}
This code is designed to run on the Arduino Nano and performs the following functions:
- Initializes the OLED display and the HC-SR04 sensor.
- In the main loop, it triggers the ultrasonic sensor to measure the distance and reads the response.
- Calculates the distance based on the time interval of the echo.
- Displays the calculated distance on the OLED screen.
- Outputs the distance to the Serial Monitor for debugging purposes.