Solar-Powered ESP32
Security System
Build your own super-smart security robot with RFID card access and a laser trip-wire alarm — powered by the Sun! ☀️
🤔 What Are We Building?
Imagine a secret base that only lets you in if you tap the right RFID card — and shoots off an alarm if anyone sneaks through the laser beam without permission! That's exactly what we're making today. It even runs on solar power so it never needs batteries you plug in!
charges battery
brain thinks
checks card
watches door
LED alert!
🛍️ Parts You Need
Ask a grown-up to help you order these. Most are cheap and easy to find!
🔌 Circuit Connections
Follow these tables carefully. Always connect GROUND (GND) wires first!
☀️ Solar Power Circuit
| FROM | TO | WIRE |
|---|---|---|
| Solar Panel + | TP4056 IN+ | 🔴 Red |
| Solar Panel – | TP4056 IN– | ⚫ Black |
| TP4056 B+ | Battery + | 🔴 Red |
| TP4056 B– | Battery – | ⚫ Black |
| TP4056 OUT+ | MT3608 IN+ | 🔴 Red |
| MT3608 OUT+ | ESP32 VIN | 🔴 Red (5V) |
| MT3608 OUT– | ESP32 GND | ⚫ Black |
⚙️ Set MT3608 boost module output to exactly 5V using the tiny screwdriver before connecting to ESP32!
🪪 MFRC522 RFID Module → ESP32
| RFID Pin | ESP32 Pin | WIRE |
|---|---|---|
| SDA (SS) | GPIO 5 | 🟣 Purple |
| SCK | GPIO 18 | 🟡 Yellow |
| MOSI | GPIO 23 | 🔵 Blue |
| MISO | GPIO 19 | 🟠 Orange |
| IRQ | Not used | — |
| GND | GND | ⚫ Black |
| RST | GPIO 4 | ⚪ White |
| 3.3V | 3.3V | 🔴 Red (3.3V only!) |
⚠️ The RFID module uses 3.3V — NOT 5V! You'll fry it if you use 5V.
🔴 Laser + LDR (Trip-Wire Circuit)
| Component | ESP32 Pin | Notes |
|---|---|---|
| Laser + | GPIO 26 | via 220Ω resistor |
| Laser – | GND | |
| LDR pin 1 | 3.3V | one side |
| LDR pin 2 | GPIO 34 (ADC) | analog read |
| LDR–GND junction | GND | via 10kΩ resistor |
💡 LEDs & Buzzer → ESP32
| Component | ESP32 Pin | Resistor |
|---|---|---|
| 🟢 Green LED (+) | GPIO 25 | 220Ω |
| 🔴 Red LED (+) | GPIO 27 | 220Ω |
| 🔊 Buzzer (+) | GPIO 14 | none needed |
| All (–) / cathode | GND |
☀️ SOLAR PANEL (6V) [+]────────────►[TP4056 IN+] [–]────────────►[TP4056 IN–] │ [TP4056 B+]──►🔋 Battery (+) [TP4056 B–]──►🔋 Battery (–) │ [TP4056 OUT+]──►[MT3608 IN+] [MT3608]──► 5V OUT │ ┌───────────────────────────┘ │ ┌─── 3.3V │ │ ▼ ▼ ╔══════════════════╗ ║ ESP32 DevKit ║ ║ VIN ◄── 5V ║──► GPIO26 ──┐ ║ GND ◄── GND ║ │ 220Ω ║ 3.3V ──────────╫─────────┐ ▼ ║ GPIO34 (ADC) ◄──╫── LDR ──┤ [LASER]───►🔴 beam ──►[LDR] ║ ║ │ ║ GPIO5 ──SDA───►║RFID │ ║ GPIO18 ──SCK───►║MFRC └── 10kΩ ──► GND ║ GPIO23 ──MOSI──►║522 ║ GPIO19 ◄─MISO──║ ║ GPIO4 ──RST───►║ ║ GPIO25 ──220Ω──►🟢 Green LED ──► GND ║ GPIO27 ──220Ω──►🔴 Red LED ──► GND ║ GPIO14 ─────────►🔊 Buzzer ──► GND ╚══════════════════╝
🛠️ Step-by-Step Build Guide
Install Arduino IDE & ESP32 Board
Download Arduino IDE from arduino.cc. Then go to File → Preferences and paste this URL into "Additional Boards Manager URLs":
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
Then go to Tools → Board → Boards Manager, search "esp32" and install it. Select "ESP32 Dev Module" as your board.
Install Required Libraries
Go to Sketch → Include Library → Manage Libraries and install:
📚 MFRC522 by GithubCommunity
📚 ESP32 built-in (comes with the board package)
Set Up the Solar Charging Circuit
Connect the solar panel to the TP4056 charger IN+ and IN– pins. Connect the battery to B+ and B–. Adjust the MT3608 boost converter output to exactly 5.0V using a multimeter before connecting to the ESP32. This keeps your ESP32 safe! ⚡
Wire the RFID Module
The RFID module connects via SPI (a special language chips use to talk to each other). Follow the table above carefully. Remember: only use 3.3V for the RFID module, never 5V! Use purple/yellow/blue/orange wires to remember which is which.
Build the Laser Trip-Wire
Place the laser diode on one side of your "door" or box opening. Place the LDR (light sensor) exactly opposite, so the laser beam hits it. Add a 10kΩ resistor between the LDR output and GND to create a "voltage divider" — this lets the ESP32 read how much light is hitting the sensor.
When someone steps through and blocks the laser, the LDR reading drops — that triggers the alarm! 🚨
Connect LEDs & Buzzer
Always put a 220Ω resistor in series with each LED (between the ESP32 pin and the LED's long leg). Without it, too much current flows and burns the LED! The short leg of each LED goes to GND.
Find Your RFID Card's Secret UID
Before uploading the main code, upload a simple UID scanner sketch to find your card's unique ID. Open Serial Monitor (115200 baud) and tap your RFID card — you'll see something like UID: A3 2F 91 B7. Write it down! 📝
#include <SPI.h> #include <MFRC522.h> #define SS_PIN 5 #define RST_PIN 4 MFRC522 rfid(SS_PIN, RST_PIN); void setup() { Serial.begin(115200); SPI.begin(); rfid.PCD_Init(); Serial.println("Tap your RFID card!"); } void loop() { if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return; Serial.print("UID: "); for (byte i = 0; i < rfid.uid.size; i++) { Serial.print(rfid.uid.uidByte[i], HEX); Serial.print(" "); } Serial.println(); rfid.PICC_HaltA(); }
Upload the Main Security Code
Copy the full code below. Replace 0xA3, 0x2F, 0x91, 0xB7 with your actual card's UID bytes. Then click Upload! ⬆️
💻 Full Project Code
// ══════════════════════════════════════════════════════════════ // 🤖 Solar-Powered ESP32 Security System // RFID Access Control + Laser Trip-Wire Alarm // For Kids Robotics Projects! // ══════════════════════════════════════════════════════════════ #include <SPI.h> #include <MFRC522.h> // ─── PIN DEFINITIONS ─────────────────────────────────────────── #define SS_PIN 5 // RFID SDA/SS #define RST_PIN 4 // RFID RST #define LASER_PIN 26 // Laser diode control #define LDR_PIN 34 // Light sensor (ADC) #define GREEN_LED 25 // Access granted #define RED_LED 27 // Access denied / alarm #define BUZZER_PIN 14 // Buzzer // ─── SETTINGS ────────────────────────────────────────────────── #define LASER_THRESHOLD 500 // ADC below this = beam broken! #define ALARM_DURATION 3000 // Alarm plays for 3 seconds #define ACCESS_DURATION 2000 // Green light for 2 seconds // ─── RFID OBJECT ─────────────────────────────────────────────── MFRC522 rfid(SS_PIN, RST_PIN); // ─── AUTHORISED CARD UID ─────────────────────────────────────── // 👇 CHANGE THESE TO YOUR OWN CARD'S UID BYTES! byte authorisedUID[] = {0xA3, 0x2F, 0x91, 0xB7}; const int UID_SIZE = 4; // ─── STATE ───────────────────────────────────────────────────── bool systemArmed = true; bool alarmActive = false; unsigned long alarmStart = 0; unsigned long accessStart = 0; bool showingAccess = false; // ═══ SETUP ═══════════════════════════════════════════════════ void setup() { Serial.begin(115200); Serial.println("🤖 Security System Starting..."); // Initialise SPI & RFID SPI.begin(); rfid.PCD_Init(); Serial.println("✅ RFID Ready"); // Set pin modes pinMode(LASER_PIN, OUTPUT); pinMode(GREEN_LED, OUTPUT); pinMode(RED_LED, OUTPUT); pinMode(BUZZER_PIN, OUTPUT); pinMode(LDR_PIN, INPUT); // Turn laser ON at startup digitalWrite(LASER_PIN, HIGH); Serial.println("🔴 Laser armed!"); // Quick startup beep startupBeep(); Serial.println("🔐 System Armed. Tap card or break laser to test!"); } // ═══ MAIN LOOP ════════════════════════════════════════════════ void loop() { // ── 1) Check if alarm timer has ended ────────────────────── if (alarmActive && (millis() - alarmStart > ALARM_DURATION)) { stopAlarm(); } // ── 2) Check if access-granted display should end ────────── if (showingAccess && (millis() - accessStart > ACCESS_DURATION)) { digitalWrite(GREEN_LED, LOW); showingAccess = false; systemArmed = true; // re-arm after access Serial.println("🔐 Re-armed!"); } // ── 3) Laser trip-wire check ─────────────────────────────── if (systemArmed && !alarmActive && !showingAccess) { int ldrValue = analogRead(LDR_PIN); if (ldrValue < LASER_THRESHOLD) { Serial.print("🚨 INTRUDER! LDR value: "); Serial.println(ldrValue); triggerAlarm("LASER BROKEN"); } } // ── 4) RFID card check ───────────────────────────────────── if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) { return; // no card detected, skip } checkCard(); rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } // ═══ FUNCTIONS ════════════════════════════════════════════════ // Check if tapped card matches the authorised UID void checkCard() { Serial.print("Card tapped! UID: "); for (byte i = 0; i < rfid.uid.size; i++) { Serial.print(rfid.uid.uidByte[i], HEX); Serial.print(" "); } Serial.println(); // Compare UID bytes bool match = (rfid.uid.size == UID_SIZE); if (match) { for (int i = 0; i < UID_SIZE; i++) { if (rfid.uid.uidByte[i] != authorisedUID[i]) { match = false; break; } } } if (match) { grantAccess(); } else { denyAccess(); } } // ✅ Access GRANTED void grantAccess() { Serial.println("✅ ACCESS GRANTED! Welcome!"); systemArmed = false; // disarm laser briefly alarmActive = false; showingAccess = true; accessStart = millis(); digitalWrite(RED_LED, LOW); digitalWrite(GREEN_LED, HIGH); // Happy beeps! ♪ tone(BUZZER_PIN, 880, 100); delay(150); tone(BUZZER_PIN, 1047, 100); delay(150); tone(BUZZER_PIN, 1319, 200); delay(200); noTone(BUZZER_PIN); } // ❌ Access DENIED void denyAccess() { Serial.println("❌ ACCESS DENIED!"); digitalWrite(RED_LED, HIGH); // Low sad beep tone(BUZZER_PIN, 200, 500); delay(600); noTone(BUZZER_PIN); delay(500); digitalWrite(RED_LED, LOW); } // 🚨 Trigger alarm (laser broken or bad card) void triggerAlarm(const char* reason) { Serial.print("🚨 ALARM: "); Serial.println(reason); alarmActive = true; alarmStart = millis(); digitalWrite(RED_LED, HIGH); tone(BUZZER_PIN, 1500); // continuous alarm tone } // 🔇 Stop alarm void stopAlarm() { alarmActive = false; noTone(BUZZER_PIN); digitalWrite(RED_LED, LOW); Serial.println("Alarm cleared."); } // 🎵 Startup beep sequence void startupBeep() { int notes[] = {523, 659, 784, 1047}; for (int i = 0; i < 4; i++) { tone(BUZZER_PIN, notes[i], 120); delay(160); } noTone(BUZZER_PIN); }
🔬 How Does It All Work?
📡 RFID — Invisible Radio Magic
Your RFID card has a tiny antenna coil inside (no battery!). When you bring it near the reader, the reader sends out a radio wave. This wave gives the card just enough energy to wake up and shout its secret UID number back. The ESP32 hears it and checks if it matches the allowed UID in the code.
🔴 The Laser Trip-Wire
The laser diode shoots a thin beam of light to the LDR sensor across the doorway. The LDR (Light Dependent Resistor) has low resistance when bright light hits it — giving a HIGH analog reading. When someone breaks the beam, the reading drops below our threshold (500), and — ALARM! 🚨
☀️ Solar Power Flow
Light hits the solar cells → electrons get excited → electricity flows! The TP4056 chip safely charges the 18650 lithium battery (same type as in some laptops). The MT3608 boosts the battery's 3.7V up to a steady 5V that the ESP32 needs. Free energy from the Sun! 🌞
🔧 Something Not Working?
🦺 Safety Rules for Young Makers
🚀 Cool Upgrades to Try
🏆 What You Learned Today
How chips talk to each other using clock signals
How resistors split voltage for sensor readings
How to safely charge batteries with solar power
How code tracks "armed", "alarm", "access" states

Comments
Post a Comment