🔐 Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button)

Introduction

Welcome back, makers! 👋
If you enjoyed building Wi-Fi-controlled LEDs, let’s take things a step further into the world of physical security. Today, we’re working with solenoid locks — the same mechanism used in lockers, cabinets…


This content originally appeared on DEV Community and was authored by Mohamed Ahmed

Introduction

Welcome back, makers! 👋
If you enjoyed building Wi-Fi-controlled LEDs, let’s take things a step further into the world of physical security. Today, we’re working with solenoid locks — the same mechanism used in lockers, cabinets, and access-control systems.

In this project, you’ll learn how to lock and unlock a solenoid lock using:

  1. 1. Arduino Mega
  2. 2. A relay module
  3. 3. 12V power supply
  4. 4. Simple push button
  5. Breadboard
  6. Jumper wires(male to male and male to female)
  • By the end, you’ll push a button → the solenoid will pull → the lock will OPEN.
  • Push the button again → the solenoid releases → the lock goes back to LOCKED.

Let’s dive in!

🧰 What You’ll Need

Component Image Description
Arduino Mega Arduino Mega Microcontroller to read button and control relay
12V Solenoid Lock Solenoid Lock The physical locking mechanism
Relay Module (5V Control) Relay Module Acts as a switch to send 12V to the solenoid
12V Power Adapter 12V Adapter Powers the solenoid and Arduino
Push Button Push Button Triggers lock/unlock
Breadboard & Jumper Wires Breadboard For easy wiring

⚡ Step 1: Wiring Diagram

Here’s the wiring logic broken down clearly:

🔘 Button to Arduino

Component Arduino Pin Purpose
Button Lead 1 D2 Reads button press
Button Lead 2 GND Completes button circuit

🔁 Relay Module to Arduino

Component Arduino Pin Purpose
VCC 5V Powers relay coil
GND GND Ground
IN D8 Arduino signal to toggle solenoid

🔌 Power + Solenoid Connection

Component Connected To Purpose
12V Adapter Arduino Barrel Jack Powers Arduino and relay
12V Adapter + (Positive) Relay COM 12V source for switching
Solenoid Red Wire Relay NO Receives switched 12V
Solenoid Blue Wire GND Completes 12V circuit

💡 We use the relay because the solenoid draws more current than Arduino can provide.

💻 Step 2: Write the Code

Open your Arduino IDE and paste the following code:

// Define the pins used
const int buttonPin = 2;   
const int relayPin = 8;    

bool isLocked = true; 

void setup() {
  pinMode(relayPin, OUTPUT); 
  // Use INPUT_PULLUP for simpler button wiring
  pinMode(buttonPin, INPUT_PULLUP); 

  // SET INITIAL STATE: LOCK the door (Solenoid needs POWER ON to be locked)
  digitalWrite(relayPin, HIGH); 
}

void loop() {
  // Read the button state (it reads LOW when pressed)
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) { // Button pressed
    delay(50); // Debounce
    if (digitalRead(buttonPin) == LOW) {
      isLocked = !isLocked; // Toggle the state

      if (isLocked) {
        // LOCK: Solenoid ON (Power applied)
        digitalWrite(relayPin, HIGH); 
      } else {
        // UNLOCK: Solenoid OFF (Power removed)
        digitalWrite(relayPin, LOW); 
      }

      // Wait for the button to be released 
      while(digitalRead(buttonPin) == LOW){
        // Do nothing 
      }
    }
  }
}

🧠 Explanation

  • The button uses INPUT_PULLUP, meaning:
    • Released → HIGH
    • Pressed → LOW
  • When pressed, Arduino sets relay HIGH → relay sends 12V to solenoid → it unlocks.
  • When pushed again → relay turns OFF → the solenoid locks again.

🧪 Step 3: Test the Setup

  1. Power the Arduino using your 12V adapter.
  2. Open the Serial Monitor — you should see "Solenoid Lock System Ready".
  3. Press the button:
    • You should hear a click from the relay.
    • Solenoid retracts (unlock).
  4. Press the button again:
    • Solenoid extends (lock).

🔐⚡ If everything is wired correctly — congratulations, you’ve built your own electromechanical locking system!


This content originally appeared on DEV Community and was authored by Mohamed Ahmed


Print Share Comment Cite Upload Translate Updates
APA

Mohamed Ahmed | Sciencx (2025-11-19T09:11:53+00:00) 🔐 Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button). Retrieved from https://www.scien.cx/2025/11/19/%f0%9f%94%90-control-a-solenoid-lock-with-arduino-mega-using-a-relay-push-button/

MLA
" » 🔐 Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button)." Mohamed Ahmed | Sciencx - Wednesday November 19, 2025, https://www.scien.cx/2025/11/19/%f0%9f%94%90-control-a-solenoid-lock-with-arduino-mega-using-a-relay-push-button/
HARVARD
Mohamed Ahmed | Sciencx Wednesday November 19, 2025 » 🔐 Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button)., viewed ,<https://www.scien.cx/2025/11/19/%f0%9f%94%90-control-a-solenoid-lock-with-arduino-mega-using-a-relay-push-button/>
VANCOUVER
Mohamed Ahmed | Sciencx - » 🔐 Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/19/%f0%9f%94%90-control-a-solenoid-lock-with-arduino-mega-using-a-relay-push-button/
CHICAGO
" » 🔐 Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button)." Mohamed Ahmed | Sciencx - Accessed . https://www.scien.cx/2025/11/19/%f0%9f%94%90-control-a-solenoid-lock-with-arduino-mega-using-a-relay-push-button/
IEEE
" » 🔐 Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button)." Mohamed Ahmed | Sciencx [Online]. Available: https://www.scien.cx/2025/11/19/%f0%9f%94%90-control-a-solenoid-lock-with-arduino-mega-using-a-relay-push-button/. [Accessed: ]
rf:citation
» 🔐 Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button) | Mohamed Ahmed | Sciencx | https://www.scien.cx/2025/11/19/%f0%9f%94%90-control-a-solenoid-lock-with-arduino-mega-using-a-relay-push-button/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.