Arduino Nano-Based Password-Protected Solenoid Lock with Rotary Encoder
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a 12V solenoid lock using an Arduino Nano. The system includes a rotary encoder for password entry, a relay to control the solenoid lock, LEDs for status indication, a buzzer for audio feedback, and a pushbutton for resetting the password. The circuit is powered by a 12V power supply, which is stepped down to 5V using a buck converter to power the Arduino and other components.
Component List
Arduino Nano
- Description: Microcontroller board based on the ATmega328P.
- Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK
12V Solenoid Lock
- Description: Electromechanical lock that operates on 12V.
- Pins: VCC, GND
5V Relay
- Description: Relay module to control high voltage devices.
- Pins: Normally Open, Common terminal, Normally Closed, In, GND, VCC
2.1mm Barrel Jack with Terminal Block
- Description: Power input connector for 12V power supply.
- Pins: POS, NEG
Pushbutton
- Description: Momentary pushbutton switch.
- Pins: Pin 3 (out), Pin 4 (out), Pin 1 (in), Pin 2 (in)
LED: Two Pin (red)
- Description: Red LED for status indication.
- Pins: cathode, anode
LED: Two Pin (green)
- Description: Green LED for status indication.
- Pins: cathode, anode
Step Down Buck Converter
- Description: Converts 12V to 5V.
- Pins: IN +, IN - GND, OUT +, OUT - GND
1N4007 Rectifier Diode
- Description: Diode for reverse voltage protection.
- Pins: Cathode, Anode
Resistor (470 Ohms)
- Description: Resistor for current limiting.
- Pins: pin1, pin2
Buzzer
- Description: Audio feedback device.
- Pins: PIN, GND
Rotary Encoder
- Description: Rotary encoder for password entry.
- Pins: GND, +, SW, DT, CLK
LED: Two Pin (yellow)
- Description: Yellow LED for status indication.
- Pins: cathode, anode
Wiring Details
Arduino Nano
- D2: Connected to Rotary Encoder CLK
- D3: Connected to Rotary Encoder DT
- D4: Connected to Rotary Encoder SW
- D5: Connected to 5V Relay In
- D6: Connected to Buzzer PIN
- D7: Connected to Pushbutton Pin 4 (out)
- D8: Connected to Red LED anode
- D9: Connected to Green LED anode
- D10: Connected to Yellow LED anode
- GND: Connected to Buzzer GND, Pushbutton Pin 1 (in), 5V Relay GND, Resistor pin2, Step Down Buck Converter OUT - GND, Rotary Encoder GND
12V Solenoid Lock
- VCC: Connected to 2.1mm Barrel Jack POS
- GND: Connected to 5V Relay Normally Open
5V Relay
- In: Connected to Arduino Nano D5
- GND: Connected to Arduino Nano GND
- VCC: Connected to Step Down Buck Converter OUT +
- Normally Open: Connected to 12V Solenoid Lock GND
- Common terminal: Connected to 2.1mm Barrel Jack NEG
2.1mm Barrel Jack with Terminal Block
- POS: Connected to 12V Solenoid Lock VCC, Step Down Buck Converter IN +, 1N4007 Rectifier Diode Cathode
- NEG: Connected to 5V Relay Common terminal, Step Down Buck Converter IN - GND
Pushbutton
- Pin 4 (out): Connected to Arduino Nano D7
- Pin 1 (in): Connected to Arduino Nano GND
LED: Two Pin (red)
- anode: Connected to Arduino Nano D8
- cathode: Connected to Resistor pin1
LED: Two Pin (green)
- anode: Connected to Arduino Nano D9
- cathode: Connected to Resistor pin1
Step Down Buck Converter
- IN +: Connected to 2.1mm Barrel Jack POS
- IN - GND: Connected to 2.1mm Barrel Jack NEG
- OUT +: Connected to 5V Relay VCC
- OUT - GND: Connected to Arduino Nano GND
1N4007 Rectifier Diode
- Cathode: Connected to 2.1mm Barrel Jack POS
- Anode: Connected to 5V Relay Normally Open
Resistor (470 Ohms)
- pin1: Connected to Red LED cathode, Green LED cathode, Yellow LED cathode
- pin2: Connected to Arduino Nano GND
Buzzer
- PIN: Connected to Arduino Nano D6
- GND: Connected to Arduino Nano GND
Rotary Encoder
- CLK: Connected to Arduino Nano D2
- DT: Connected to Arduino Nano D3
- SW: Connected to Arduino Nano D4
- GND: Connected to Arduino Nano GND
- +: Connected to Step Down Buck Converter OUT +
LED: Two Pin (yellow)
- anode: Connected to Arduino Nano D10
- cathode: Connected to Resistor pin1
Code Documentation
// Pin Definitions
const int pinEncoderA = 2; // Rotary Encoder A (CLK)
const int pinEncoderB = 3; // Rotary Encoder B (DT)
const int pinEncoderSW = 8; // Rotary Encoder SW (Push Button)
const int pinResetButton = 4; // Reset Push Button
const int pinGreenLED = 7; // Green LED (Unlocked)
const int pinRedLED = 6; // Red LED (Incorrect)
const int pinBuzzer = 12; // Buzzer
const int pinRelay = 5; // Relay (Solenoid Control)
// Variables for Password Entry
String enteredPassword = "";
String correctPassword = "123"; // Set your desired password here
// Rotary Encoder Variables
int encoderPos = 0;
int lastEncoderPos = 0;
void setup() {
// Set pin modes
pinMode(pinEncoderA, INPUT);
pinMode(pinEncoderB, INPUT);
pinMode(pinEncoderSW, INPUT_PULLUP); // Pull-up resistor for push button
pinMode(pinResetButton, INPUT_PULLUP); // Pull-up resistor for reset button
pinMode(pinGreenLED, OUTPUT);
pinMode(pinRedLED, OUTPUT);
pinMode(pinBuzzer, OUTPUT);
pinMode(pinRelay, OUTPUT);
// Initialize Serial Monitor for debugging
Serial.begin(9600);
// Ensure solenoid is off at startup
digitalWrite(pinRelay, LOW);
digitalWrite(pinGreenLED, LOW);
digitalWrite(pinRedLED, LOW);
digitalWrite(pinBuzzer, LOW);
}
void loop() {
// Read the rotary encoder
int encoderA = digitalRead(pinEncoderA);
int encoderB = digitalRead(pinEncoderB);
// Handle Rotary Encoder Rotation
if (encoderA == LOW && encoderB == HIGH) {
encoderPos++;
Serial.print("Encoder Position: ");
Serial.println(encoderPos);
} else if (encoderA == HIGH && encoderB == LOW) {
encoderPos--;
Serial.print("Encoder Position: ");
Serial.println(encoderPos);
}
// Check for Reset Button Press (D4)
if (digitalRead(pinResetButton) == LOW) { // Button pressed (LOW signal)
resetPassword(); // Reset entered password
delay(300); // Debounce delay
}
// Check for Rotary Encoder Button Press (SW)
if (digitalRead(pinEncoderSW) == LOW) {
if (enteredPassword.length() == 3) { // Password entered fully
if (enteredPassword == correctPassword) {
unlockSolenoid(); // Correct password entered
} else {
wrongPassword(); // Incorrect password entered
}
delay(300); // Debounce delay
}
}
// Update the entered password based on encoder