Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Nano Ultrasonic Distance Sensor with OLED Display

View and Edit Project In Cirkit DesignerImage of Arduino Nano Ultrasonic Distance Sensor with OLED Display

Circuit Documentation

Summary

This circuit interfaces an Arduino Nano with an OLED display and an HC-SR04 Ultrasonic Distance Sensor. The distance measured by the sensor is displayed on the OLED screen. The Arduino Nano serves as the central microcontroller, handling the sensor data and updating the display.

Component List

  1. Arduino Nano

    • Description: A small, complete, and breadboard-friendly 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
  2. OLED 1.3" Display

    • Description: A 1.3-inch OLED display module.
    • Pins: GND, VCC, SCL, SDA
  3. HC-SR04 Ultrasonic Distance Sensor

    • Description: An ultrasonic sensor used for distance measurement.
    • Pins: VCC, TRIG, ECHO, GND
  4. Comment

    • Description: A placeholder for comments or notes in the circuit design.
    • Pins: None

Wiring Details

Arduino Nano

OLED 1.3" Display

HC-SR04 Ultrasonic Distance Sensor

Code Documentation

/*
 * 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 initializes the OLED display and the ultrasonic sensor, measures the distance using the sensor, and displays the measured distance on the OLED screen as well as prints it to the Serial Monitor. The loop continuously measures and updates the distance every 500 milliseconds.