Difficulty: Beginner
Time Required: 1-2 hours
Cost: $10-15
ESP Board: ESP8266
What You’ll Need
| Component | Cost | Where to Buy |
|---|---|---|
| NodeMCU ESP8266 | $4-6 | Amazon |
| HC-SR501 PIR Sensor | $2-3 | Amazon |
| Buzzer | $1-2 | Amazon |
| LED | $1 | Amazon |
| Jumper Wires | $2 | Amazon |
Circuit Diagram
| ESP8266 Pin | Component |
|---|---|
| 3.3V | PIR VCC |
| GND | PIR GND |
| D1 (GPIO 5) | PIR OUT |
| D2 (GPIO 4) | Buzzer |
| D3 (GPIO 0) | LED |
The Complete Code
/*********************************************************************
* ESP8266 Motion Sensor Security System
*
* Detects motion and sends Telegram notifications
* Hardware: NodeMCU, HC-SR501 PIR, Buzzer, LED
*********************************************************************/
#include
#include
#include
// ============== WIFI ==============
const char* wifi_ssid = "YOUR_WIFI";
const char* wifi_password = "YOUR_PASSWORD";
// ============== TELEGRAM ==============
#define BOT_TOKEN "YOUR_BOT_TOKEN"
#define CHAT_ID "YOUR_CHAT_ID"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// ============== PINS ==============
#define MOTION_SENSOR D1
#define BUZZER_PIN D2
#define LED_PIN D3
// ============== CONFIG ==============
unsigned long lastMotionTime = 0;
const unsigned long ALERT_COOLDOWN = 30000; // 30 seconds
bool motionDetected = false;
void setup() {
Serial.begin(115200);
pinMode(MOTION_SENSOR, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
connectToWiFi();
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
Serial.println(F("Security system active!"));
}
void loop() {
if (digitalRead(MOTION_SENSOR) == HIGH) {
if (millis() - lastMotionTime > ALERT_COOLDOWN) {
triggerAlarm();
lastMotionTime = millis();
}
}
delay(100);
}
void triggerAlarm() {
Serial.println(F("MOTION DETECTED!"));
// Flash LED
for (int i = 0; i < 10; i++) {
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
}
// Sound buzzer
digitalWrite(BUZZER_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
// Send Telegram
String msg = "🚨 Motion detected at " + String(millis()/1000) + " seconds ago!";
bot.sendMessage(CHAT_ID, msg, "");
}
void connectToWiFi() {
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(" Connected!"));
}
Official Documentation
Frequently Asked Questions
Q: How do I adjust PIR sensitivity?
A: Use the sensitivity potentiometer on the PIR board. Turn clockwise to increase sensitivity.

