"DIY Intruder Alert System with ESP32, PIR & IR Sensor"


ESP32 Motion and Obstacle Detection System Project 

Introduction: 

I'll demonstrate a straightforward and entertaining do-it-yourself project in this blog post that I constructed with an ESP32 microcontroller, a PIR motion sensor, an IR obstacle sensor, and a tiny OLED display.In addition to displaying real-time alerts on an OLED screen, the device can detect human activity and nearby obstructions. Ideal for those new to embedded systems and the Internet of Things!

Components Used:
ESP32 Development Board 
Motion Sensor PIR
Infrared Obstacle Sensor
OLED Display (I2C-based, 0.96")
Wire jumpers
A breadboard

Principle of Operation:

The PIR sensor uses variations in infrared heat to identify motion.
The IR sensor uses infrared reflection to find obstructions in the vicinity.
The OLED display displays the following when either sensor is activated:
For PIR, "Motion Detected"
"Difficulty Ahead" (for IR)
The system displays an alert or warning if both are on.

Basic connections:

ComponentESP32 Pin
       PIR Sensor   D25 (or GPIO25)
       IR Sensor   D26 (or GPIO26)
      OLED SDA   D21
      OLED SCL   D22
     Power (VCC)            3.3V
    Ground (GND)  GND

Code for Arduino IDE:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int pirPin = 25;
int irPin = 26;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(irPin, INPUT);
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("OLED failed"));
    while(true);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,10);
  display.println("System Ready");
  display.display();
  delay(2000);
}

void loop() {
  bool motion = digitalRead(pirPin);
  bool obstacle = digitalRead(irPin) == LOW;

  display.clearDisplay();
  display.setCursor(0, 10);
  if (motion) {
    display.println("Motion Detected!");
  } else {
    display.println("No Motion.");
  }

  display.setCursor(0, 30);
  if (obstacle) {
    display.println("Obstacle Ahead!");
  } else {
    display.println("Clear Path.");
  }

  display.display();
  delay(500);
}

What I Discovered:
How to use ESP32 to communicate with many sensors
How to operate an I2C OLED screen
Fundamentals of managing data from digital sensors
Debugging problems such as display glitches, library failures, and wiring

Difficulties:  

OLED wasn't working, thus I had to manually wire it because it lacked header pins.
Unable to distinguish between the ESP32's Dx pins and GPIO numbers
The logic of the IR sensor was inverted (LOW = obstacle).

In conclusion:
Through the use of basic components, this project provided me with a practical introduction to embedded systems and the Internet of Things. I intend to make it even larger by:
Including a buzzer
WiFi integration for notifications
Using it as a miniature home security system

Setup of project:


Comments