DEV Community

Cover image for ๐Ÿ” Control a Solenoid Lock with Arduino Mega (Using a Relay & Push Button)
Mohamed Ahmed
Mohamed Ahmed

Posted on

๐Ÿ” 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, 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 
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  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!

Top comments (1)

Collapse
 
arumy56 profile image
arumy56

Nice tutorial which is easy to follow