Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsA solenoid bolt on a cabinet is released by a relay when a known tag is scanned. Three wrong tags in a row start a 30-second lockout, which stops someone trying a pile of cards. The green light shows when the bolt is free.
Low-voltage 12 V lock only. A hobby lock is a deterrent and not a security product.
Keep to low-voltage loads unless a qualified electrician wires the mains side in an earthed, closed enclosure.
Tick anything you already have and the total updates.
Connects the Uno to your computer to upload code and power it
13.56 MHz RFID/NFC reader that reads the unique ID of cards and key fobs held near it.
An electrically isolated switch that lets a 3.3/5 V pin turn a 12 V (or mains) load on and off.
Active buzzer with a built-in oscillator: apply power and it beeps at about 2.3 kHz.
Follow the colours: red is power, dark grey is ground, blue is a signal.
The solenoid runs from its own 12 V adapter through the relay, with a diode across it. Power the Uno separately by USB; join the grounds.
rfid_relay_cupboard_lock.inoVerify & upload in Electrolark// RFID cupboard lock with lockout
// Geekobot project library · board: Arduino Uno
// Wiring diagram, parts list and build steps: Geekobot project page "rfid-relay-cupboard-lock"
#include <SPI.h>
#include <MFRC522.h>
// ---- pins ----
const uint8_t RFID_SS = 2;
const uint8_t RFID_RST = 3;
const uint8_t RELAY_PIN = 4;
const uint8_t LED_GREEN = 5;
const uint8_t BUZZER_PIN = 6;
MFRC522 rfid(RFID_SS, RFID_RST);
const bool RELAY_ACTIVE_LOW = true; // most relay modules switch on when IN is LOW
// Add your tags here (their UIDs print in the Serial Monitor when scanned)
const byte ALLOWED[][4] = {
{0xDE, 0xAD, 0xBE, 0xEF},
{0x12, 0x34, 0x56, 0x78},
};
const unsigned long UNLOCK_MS = 4000; // how long the lock stays released
int wrongInARow = 0;
unsigned long lockoutUntil = 0;
void setRelay(bool on) { digitalWrite(RELAY_PIN, (on != RELAY_ACTIVE_LOW) ? HIGH : LOW); }
void setGreenLed(bool on) { digitalWrite(LED_GREEN, on ? HIGH : LOW); }
void setBuzzer(bool on) { digitalWrite(BUZZER_PIN, on ? HIGH : LOW); }
bool allowed(const byte* uid) {
for (unsigned int i = 0; i < sizeof(ALLOWED) / sizeof(ALLOWED[0]); i++)
if (memcmp(uid, ALLOWED[i], 4) == 0) return true;
return false;
}
void setup() {
Serial.begin(115200);
SPI.begin();
rfid.PCD_Init();
pinMode(RELAY_PIN, OUTPUT);
setRelay(false);
pinMode(LED_GREEN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
setBuzzer(false);
setRelay(false);
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
for (byte i = 0; i < rfid.uid.size; i++) { if (rfid.uid.uidByte[i] < 0x10) Serial.print('0'); Serial.print(rfid.uid.uidByte[i], HEX); Serial.print(' '); }
Serial.println();
if (millis() < lockoutUntil) { setBuzzer(true); delay(100); setBuzzer(false); rfid.PICC_HaltA(); return; }
if (rfid.uid.size >= 4 && allowed(rfid.uid.uidByte)) {
wrongInARow = 0;
setGreenLed(true); setRelay(true); setBuzzer(true); delay(120); setBuzzer(false);
delay(UNLOCK_MS);
setRelay(false); setGreenLed(false);
} else {
wrongInARow++;
setBuzzer(true); delay(500); setBuzzer(false);
if (wrongInARow >= 3) { lockoutUntil = millis() + 30000UL; wrongInARow = 0; Serial.println(F("Three wrong tags: locked out for 30 seconds")); }
}
rfid.PICC_HaltA();
}
The sketch above runs here on a simulated Arduino Uno. Move the sliders for the sensors and buttons it reads, and watch the Serial monitor, display and outputs change.
Check it is on 3.3 V and the SPI wires (SCK, MOSI, MISO) are on the hardware SPI pins.
Load must be on COM and NO, with its own supply in series.
Your module may be active-HIGH. Set RELAY_ACTIVE_LOW to false.
Some 5 V relay boards do not fully switch off with 3.3 V logic. Remove the JD-VCC jumper, feed JD-VCC from 5 V and VCC from 3.3 V, or use a relay board made for 3.3 V input.
LED is reversed; flip it. Check the 220 Ω resistor is in series.
Low-triggered module: invert the logic in setBuzzer().
Open the Serial Monitor at 115200 baud and check for messages. Make sure you chose the right board and port in the Arduino IDE.
Close other programs using the serial port, try another USB cable (some are charge-only) and press reset just before uploading.
Yes. Most of the code works unchanged on an ESP32 with the ESP32 board package. Watch for 5 V modules: ESP32 pins are 3.3 V, so 5 V output signals need a resistor divider.
The relay is rated for mains, but mains wiring is dangerous. Keep this build on low-voltage loads (12 V pumps, fans, LED strips), or have a qualified electrician fit the relay inside an earthed, closed enclosure.
Use a 5 V USB phone charger or a power bank into the board's USB port. Loads such as pumps, fans and solenoids need their own supply.
12 parts: Uno R3 CH340G ATmega328p Compatible Development Board, Arduino USB-A to USB-B Data Cable for UNO/MEGA, RC522 RFID Reader / Writer SPI Module with Card & Tag, 5V Relay Module with Optocoupler (1 channel), Through-Hole LED Pack (Pack of 10), Active Buzzer Module 3.3–5V, Resistor Assortment Kit (30 Values), 12V Electric Solenoid Door / Cabinet Lock, DC Power Adapter 12V 2A (5.5x2.1mm), Diodes (Pack of 10, 1N4007), plus 2 more. Tools: Digital Multimeter and Wire Stripper and Cutter. On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 2 hours. The build on this page has 10 steps.
No. This page has the complete, tested code (rfid_relay_cupboard_lock.ino). Install this library first: MFRC522.
Yes. The Run it panel under the code runs this exact sketch on a simulated board. Move the sliders or press the buttons for the sensors it reads, and the Serial monitor, display and outputs react as they will on the real build. It is a simulation, so real readings depend on your parts and wiring.
About ₹1,122 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
Active vs passive buzzers; Current-limiting resistors for LEDs; Isolation, relays and switching higher-power loads; RFID, UIDs and SPI.
Ideas that reuse this build: RFID door lock and Keypad door lock. They are listed under Take it further on this page.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Learning project. Check every circuit and the datasheet of each part before you power it up.
A simulation of the sketch above, so you know what to expect. Real readings vary with your parts and wiring.