Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Nano-Based Password-Protected Solenoid Lock with Rotary Encoder

View and Edit Project In Cirkit DesignerImage of Arduino Nano-Based Password-Protected Solenoid Lock with Rotary Encoder

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 pushbutton for resetting the password, LEDs for status indication, a buzzer for audio feedback, and a relay to control the solenoid lock. The circuit also includes a step-down buck converter to provide 5V power to the components.

Component List

  1. 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
  2. 12V Solenoid Lock

    • Description: Electromechanical lock that operates on 12V.
    • Pins: VCC, GND
  3. 5V Relay

    • Description: Relay module to control high voltage devices.
    • Pins: Normally Open, Common terminal, Normally Closed, In, GND, VCC
  4. 2.1mm Barrel Jack with Terminal Block

    • Description: Power input connector.
    • Pins: POS, NEG
  5. Pushbutton

    • Description: Momentary pushbutton switch.
    • Pins: Pin 3 (out), Pin 4 (out), Pin 1 (in), Pin 2 (in)
  6. LED: Two Pin (Red)

    • Description: Red LED for status indication.
    • Pins: cathode, anode
  7. LED: Two Pin (Green)

    • Description: Green LED for status indication.
    • Pins: cathode, anode
  8. Step Down Buck Converter

    • Description: Converts higher voltage to 5V.
    • Pins: IN +, IN - GND, OUT +, OUT - GND
  9. 1N4007 Rectifier Diode

    • Description: Diode for rectification.
    • Pins: Cathode, Anode
  10. Resistor (470 Ohms)

    • Description: Resistor with 470 Ohms resistance.
    • Pins: pin1, pin2
  11. Buzzer

    • Description: Audio feedback device.
    • Pins: PIN, GND
  12. Rotary Encoder

    • Description: Rotary encoder for user input.
    • Pins: GND, +, SW, DT, CLK

Wiring Details

Arduino Nano

12V Solenoid Lock

5V Relay

2.1mm Barrel Jack with Terminal Block

Pushbutton

LED: Two Pin (Red)

LED: Two Pin (Green)

Step Down Buck Converter

1N4007 Rectifier Diode

Resistor (470 Ohms)

Buzzer

Rotary Encoder

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 position
  if (encoderPos != lastEncoderPos) {
    updatePassword(encoderPos);
    lastEncoderPos = encoderPos;
  }

  delay(50);  // Small delay to avoid excessive processing
}

// Function to update the password based on encoder rotation
void updatePassword(int position) {
  if (position > 0 && enteredPassword.length() < 3) {
    enteredPassword += String(position);  // Add digit based on rotation
    Serial.print