#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define screen dimensions and OLED reset pin
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_SDA 21
#define OLED_SCL 22
// Define PIR sensor and LED pins
#define PIR_PIN 3
#define LED_PIN 2
// Create an instance of the OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the OLED display with the correct I2C address
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Infinite loop to halt the program if display initialization fails
}
// Clear the display and set initial text properties
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
// Set up PIR sensor and LED pin modes
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
// Print initial message to the display
display.println("Setup complete!");
display.display();
delay(2000); // Pause for 2 seconds
}
void loop() {
int motionDetected = digitalRead(PIR_PIN); // Read PIR sensor value
// Clear the display for the new message
display.clearDisplay();
display.setCursor(0, 0);
if (motionDetected == HIGH) {
display.println("Motion detected!");
digitalWrite(LED_PIN, HIGH); // Turn on LED
} else {
display.println("No motion detected.");
digitalWrite(LED_PIN, LOW); // Turn off LED
}
// Display the updated message
display.display();
// Delay for a second before the next reading
delay(1000);
}