Build a Smart Home
with RFID Lock + Auto Light & Fan
Tap your RFID card to unlock the door, then watch the lights and fan turn on and off all by themselves — a real smart home, built by you!
How Does a Smart Home Work? 🤔
The RFID Card is a Digital Key!
Just like a metal key fits into a lock, an RFID card has a tiny chip with a unique ID number. When you tap it near the reader, it "whispers" its secret number using radio waves. If the Raspberry Pi recognizes that number on its approved list, the door unlocks!
The Light Sensor is Like Your Eyes Adjusting to Darkness
An LDR (Light Dependent Resistor) changes its electrical resistance based on how much light hits it — bright room = low resistance, dark room = high resistance. Raspberry Pi reads this value and automatically switches the light ON when it gets dark, just like how you'd flip a switch yourself!
The Temperature Sensor Decides When You Need the Fan
A temperature sensor constantly measures the room's warmth. When it gets too hot — say above 30°C — Raspberry Pi tells a relay (an electronic switch) to turn the fan ON automatically. No need to get up and switch it on yourself!
Three Smart Modules in One Home 🧩
Parts You'll Need 🛒
All available at electronics shops or online stores like Robu.in, Robocraze, or Amazon. Ask a parent or teacher to help you buy them!
Why Raspberry Pi instead of Arduino? Raspberry Pi is a tiny full computer — it can run Python easily, connect to WiFi, and handle multiple sensors at once (RFID + temperature + light) without needing extra modules. That makes it perfect for a multi-feature smart home project!
Building the Model House 🏗️
Prepare a sturdy cardboard box
Use a medium-to-large cardboard box as the house shell. Cut out a front door opening and a window opening on one side.
Build the swinging door
Cut a door-shaped piece of cardboard slightly smaller than the opening. Attach it to the servo's rotating arm using a thin rod or stiff wire so the servo can swing it open and shut.
Mount the RFID reader near the door
Fix the RC522 reader on the outside wall next to the door, at a height where a card can be tapped easily — like a real smart doorbell panel!
Add two "rooms" inside
Use a cardboard divider to split the inside into two sections. Mount the LED bulb in one room's ceiling and the small fan in the other room's ceiling.
Place sensors in the right spots
Mount the LDR sensor near the window (so it can sense outside light) and the temperature sensor inside the fan's room (so it can sense room heat).
House the Raspberry Pi outside the model
Keep the Raspberry Pi, relay module, and breadboard mounted on a small board next to (or behind) the house model, with wires running through small holes into each room.
This is a model, not real mains wiring! Our project uses low-voltage 5V components only (LED bulb, small DC fan). Never connect this project to real household electricity (220V/110V) — that requires a licensed electrician and proper safety equipment!
Circuit Diagram & Wiring
Safety first! Always shut down and unplug the Raspberry Pi before changing any wires. RFID reader uses SPI communication — double-check pin positions carefully, as wrong wiring can damage the GPIO pins!
Complete Wiring Table
| # | From | To | Wire | Purpose |
|---|---|---|---|---|
| 1 | RC522 SDA(SS) | GPIO8 (CE0) | BLUE | Chip select |
| 2 | RC522 MOSI | GPIO10 | PURPLE | SPI data out |
| 3 | RC522 MISO | GPIO9 | YELLOW | SPI data in |
| 4 | RC522 SCK | GPIO11 | BLUE | SPI clock |
| 5 | RC522 RST | GPIO25 | GREEN | Reset pin |
| 6 | RC522 3.3V | Pi 3.3V (Pin 1) | ORANGE | Power (3.3V ONLY!) |
| 7 | RC522 GND | Pi GND | BLACK | Ground |
| 8 | LDR Signal | GPIO17 | YELLOW | Light level reading |
| 9 | LDR VCC / GND | 5V / GND | ORANGE / BLACK | Power sensor |
| 10 | DHT11 Data | GPIO4 | GREEN | Temperature reading |
| 11 | DHT11 VCC / GND | 5V / GND | ORANGE / BLACK | Power sensor |
| 12 | Servo Signal | GPIO18 | BLUE | Door lock rotation |
| 13 | Servo VCC / GND | 5V / GND | ORANGE / BLACK | Power servo |
| 14 | Relay CH1 (Light) IN | GPIO22 | RED | Light on/off control |
| 15 | Relay CH2 (Fan) IN | GPIO23 | ORANGE | Fan on/off control |
| 16 | Relay VCC / GND | 5V / GND | ORANGE / BLACK | Power relay module |
| 17 | Buzzer Signal | GPIO27 | BLUE | Access denied beep |
| 18 | Buzzer VCC / GND | 5V / GND | ORANGE / BLACK | Power buzzer |
RC522 needs 3.3V, NOT 5V! The RFID reader module will get damaged if connected to the Pi's 5V pin. Always double-check you're using the 3.3V pin (physical pin 1) for the RC522's power.
Setting Up the Software
Flash Raspberry Pi OS onto the microSD card
Use the official Raspberry Pi Imager tool (from raspberrypi.com/software) on your computer to write Raspberry Pi OS onto the microSD card, then insert it into the Pi.
Enable SPI interface
Open a terminal on the Pi and run sudo raspi-config → Interface Options → SPI → Enable. This is required for the RFID reader to work. Reboot after enabling.
Install required Python libraries
Run these commands in the terminal:
sudo pip3 install mfrc522 RPi.GPIO Adafruit_DHT spidev
Create a folder for your project
Run mkdir smart_home && cd smart_home to keep your code organized.
Register your RFID card's unique ID
Run a simple test script (from the mfrc522 library examples) that prints the card's ID number when scanned. Note down this number — you'll add it to the "approved cards" list in our main code!
The Python Code
Save this as smart_home.py inside your project folder, then run it with sudo python3 smart_home.py (sudo is needed for GPIO access).
# ============================================================= # SMART HOME -- RFID Door Lock + Auto Light + Auto Fan # MakeMindz | Kids Raspberry Pi Robotics Project # RFID unlocks the door, LDR controls the light, # and a temperature sensor controls the fan automatically! # ============================================================= import RPi.GPIO as GPIO import time from mfrc522 import SimpleMFRC522 import Adafruit_DHT # -- PIN DEFINITIONS -- LDR_PIN = 17 RELAY_LIGHT= 22 RELAY_FAN = 23 SERVO_PIN = 18 BUZZER_PIN= 27 DHT_PIN = 4 DHT_SENSOR= Adafruit_DHT.DHT11 # -- APPROVED CARD IDS -- # Replace these with YOUR card's ID (printed when you scan it) APPROVED_CARDS = [123456789012, 987654321098] # -- SETTINGS -- TEMP_THRESHOLD = 30 # degrees C - fan turns ON above this DOOR_OPEN_ANGLE = 90 DOOR_CLOSED_ANGLE = 0 # -- SETUP -- GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(LDR_PIN, GPIO.IN) GPIO.setup(RELAY_LIGHT, GPIO.OUT) GPIO.setup(RELAY_FAN, GPIO.OUT) GPIO.setup(BUZZER_PIN, GPIO.OUT) GPIO.setup(SERVO_PIN, GPIO.OUT) servo = GPIO.PWM(SERVO_PIN, 50) # 50Hz for servo servo.start(0) reader = SimpleMFRC522() def set_servo_angle(angle): # Move servo to a given angle (0-180) duty = 2 + (angle / 18) GPIO.output(SERVO_PIN, True) servo.ChangeDutyCycle(duty) time.sleep(0.5) GPIO.output(SERVO_PIN, False) servo.ChangeDutyCycle(0) def unlock_door(): print("Access granted! Door unlocking...") set_servo_angle(DOOR_OPEN_ANGLE) time.sleep(4) # Stay open for 4 seconds set_servo_angle(DOOR_CLOSED_ANGLE) print("Door locked again.") def deny_access(): print("Access denied! Unknown card.") for _ in range(3): GPIO.output(BUZZER_PIN, GPIO.HIGH) time.sleep(0.2) GPIO.output(BUZZER_PIN, GPIO.LOW) time.sleep(0.1) def check_light(): # LDR reads HIGH in darkness, LOW in bright light (digital module) dark = GPIO.input(LDR_PIN) if dark: GPIO.output(RELAY_LIGHT, GPIO.HIGH) print("It's dark - Light turned ON") else: GPIO.output(RELAY_LIGHT, GPIO.LOW) print("It's bright - Light turned OFF") def check_temperature(): humidity, temp = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) if temp is not None: print(f"Temperature: {temp:.1f} C") if temp > TEMP_THRESHOLD: GPIO.output(RELAY_FAN, GPIO.HIGH) print("Too warm - Fan turned ON") else: GPIO.output(RELAY_FAN, GPIO.LOW) print("Cool enough - Fan turned OFF") # ============================================================= # MAIN LOOP # ============================================================= print("Smart Home Starting...") print("Ready! Tap your RFID card at the door.") try: while True: # -- Step 1: Check RFID for door access -- print("Scanning for card...") card_id, text = reader.read() if card_id in APPROVED_CARDS: unlock_door() else: deny_access() # -- Step 2: Check light sensor -- check_light() # -- Step 3: Check temperature sensor -- check_temperature() time.sleep(1) except KeyboardInterrupt: print("Smart Home shutting down safely...") finally: servo.stop() GPIO.cleanup()
How the Code Works — Step by Step
Reading the RFID Card
reader.read() waits until a card is tapped, then returns its unique ID number. We compare this ID against our APPROVED_CARDS list — just like a bouncer checking names on a guest list!
Controlling the Servo Lock
set_servo_angle() converts an angle (0-180°) into a PWM duty cycle the servo understands. unlock_door() swings it open, waits 4 seconds, then swings it shut again automatically.
Sensing Light with the LDR
check_light() reads the LDR module's digital output. Most LDR modules output HIGH when it's dark — so the code turns the light relay ON when darkness is detected, and OFF when it's bright.
Sensing Heat with DHT11
check_temperature() reads the current room temperature. If it's hotter than TEMP_THRESHOLD (30°C by default), the fan relay turns ON automatically.
The Main Loop — Keeping Watch Forever
The while True loop keeps checking the RFID reader, light sensor, and temperature sensor over and over, every second — making the home truly "smart" and always alert!
Terminal Output Example
Ready! Tap your RFID card at the door.
-----------------------------------------
Scanning for card...
Access granted! Door unlocking...
Door locked again.
It's dark - Light turned ON
Temperature: 31.0 C
Too warm - Fan turned ON
-----------------------------------------
Scanning for card...
Access denied! Unknown card.
Testing It Out 🧪
✅ Pre-Test Checklist
Run the script
Type sudo python3 smart_home.py and press Enter. You should see "Smart Home Starting..." appear.
Tap your registered card
Hold your RFID card near the RC522 reader. The door servo should swing open, terminal shows "Access granted!", and it closes again after 4 seconds.
Tap an unregistered card (or your hand)
The door should stay locked, the buzzer beeps 3 times, and terminal shows "Access denied!"
Cover the LDR sensor with your hand
Simulating darkness — the light relay should click ON. Uncover it, and the light should turn back OFF.
Warm up the DHT11 sensor
Cup your hands around it or breathe on it gently. Once it crosses 30°C, the fan relay should click ON automatically!
Troubleshooting Guide 🔧
❓ RFID reader not detected / "no module" error
Make sure SPI is enabled (sudo raspi-config) and you've rebooted afterward. Double-check SDA, MOSI, MISO, SCK, and RST pins match the wiring table exactly.
❓ Servo doesn't move or jitters constantly
Servos draw a lot of current — power it from a separate 5V supply if the Pi resets unexpectedly. Check the signal wire is on GPIO18 and GND is shared with the Pi.
❓ Light/fan relay clicks but doesn't power the bulb/fan
Check the relay's COM and NO terminals are correctly wired to your bulb/fan's power circuit. The Pi only controls the relay's switch — actual power for the bulb/fan comes from its own 5V source.
❓ DHT11 gives "sensor failure" errors
DHT11 sensors can be finicky — try reading 2-3 times in a row (the code already uses read_retry which retries automatically). Check the data pin isn't loose.
❓ "Permission denied" when running the script
GPIO access on Raspberry Pi needs administrator rights. Always run with sudo python3 smart_home.py, not just python3 smart_home.py.
❓ Light or fan stays ON all the time
Check your relay module's logic — some relay boards are "active LOW" (turn ON when signal is LOW, not HIGH). If yours behaves backwards, simply swap GPIO.HIGH and GPIO.LOW in the check_light() and check_temperature() functions.
Make It Even Smarter! 🚀
Frequently Asked Questions ❓
APPROVED_CARDS list in the code, separated by commas. You could have one for every family member!
Comments
Post a Comment