The circuit consists of an Arduino UNO microcontroller interfaced with two OLED 1.3" displays and two HC-SR04 Ultrasonic Sensors. The OLED displays are used for visual feedback, while the ultrasonic sensors are used to measure distances, likely for detecting the presence or absence of objects. The Arduino UNO controls the displays and reads data from the ultrasonic sensors. All components share a common ground (GND) and are powered by the Arduino's 5V output.
#include <Wire.h> // Include the Wire library for I2C communication
#include <Adafruit_GFX.h> // Include the Adafruit GFX library
#include <Adafruit_SSD1306.h> // Include the Adafruit SSD1306 library
#define SCREEN_WIDTH 128 // OLED screen width, in pixels
#define SCREEN_HEIGHT 64 // OLED screen height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // OLED display I2C address
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define pins for ultrasonic sensors
const int trigPin1 = 9; // Trigger pin for slot 1
const int echoPin1 = 10; // Echo pin for slot 1
const int trigPin2 = 11; // Trigger pin for slot 2
const int echoPin2 = 12; // Echo pin for slot 2
void setup() {
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
for (;;); // Infinite loop if OLED allocation fails
}
display.clearDisplay(); // Clear the display buffer
display.setTextSize(1); // Set text size to 1
display.setTextColor(SSD1306_WHITE); // Set text color to white
// Initialize ultrasonic sensor pins
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}
void loop() {
// Read distance for slot 1
float distance1 = getDistance(trigPin1, echoPin1);
// Read distance for slot 2
float distance2 = getDistance(trigPin2, echoPin2);
// Clear the display buffer
display.clearDisplay();
// Set cursor to the top-left corner
display.setCursor(0, 0);
display.setTextSize(1); // Set text size to 1
// Display slot 1 status
display.print("Slot 1: ");
if (distance1 < 10) { // Assuming a threshold of 10 cm for car detection
display.println("Not Free, Charging");
} else {
display.println("Free");
}
// Display slot 2 status
display.print("Slot 2: ");
if (distance2 < 10) { // Assuming a threshold of 10 cm for car detection
display.println("Not Free, Charging");
} else {
display.println("Free");
}
// Update the display with the buffer contents
display.display();
// Wait before taking next measurement
delay(500);
}
// Function to measure distance using ultrasonic sensor
float getDistance(int trigPin, int echoPin) {
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigger pin high for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
float distance = duration * 0.0344 / 2;
return distance;
}
This code initializes two OLED displays and two ultrasonic sensors connected to an Arduino UNO. It measures the distance using the ultrasonic sensors and displays whether a slot is "Free" or "Not Free, Charging" based on a threshold distance of 10 cm. The display is updated every 500 milliseconds with the latest status.