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
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
OLED 1.3" Display
- Description: A 1.3-inch OLED display module.
- Pins: GND, VCC, SCL, SDA
HC-SR04 Ultrasonic Distance Sensor
- Description: An ultrasonic sensor used for distance measurement.
- Pins: VCC, TRIG, ECHO, GND
Comment
- Description: A placeholder for comments or notes in the circuit design.
- Pins: None
Wiring Details
Arduino Nano
GND is connected to:
- GND of OLED 1.3" Display
- GND of HC-SR04 Ultrasonic Distance Sensor
D9 is connected to:
- ECHO of HC-SR04 Ultrasonic Distance Sensor
D10 is connected to:
- TRIG of HC-SR04 Ultrasonic Distance Sensor
VIN is connected to:
- VCC of OLED 1.3" Display
- VCC of HC-SR04 Ultrasonic Distance Sensor
A5 is connected to:
- SCL of OLED 1.3" Display
A4 is connected to:
- SDA of OLED 1.3" Display
OLED 1.3" Display
GND is connected to:
- GND of Arduino Nano
VCC is connected to:
- VIN of Arduino Nano
SCL is connected to:
- A5 of Arduino Nano
SDA is connected to:
- A4 of Arduino Nano
HC-SR04 Ultrasonic Distance Sensor
GND is connected to:
- GND of Arduino Nano
VCC is connected to:
- VIN of Arduino Nano
TRIG is connected to:
- D10 of Arduino Nano
ECHO is connected to:
- D9 of Arduino Nano
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.
