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. Arduino Mega
- 2. A relay module
- 3. 12V power supply
- 4. Simple push button
- Breadboard
- 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 | ![]() |
Microcontroller to read button and control relay |
| 12V Solenoid Lock | ![]() |
The physical locking mechanism |
| Relay Module (5V Control) | ![]() |
Acts as a switch to send 12V to the solenoid |
| 12V Power Adapter | ![]() |
Powers the solenoid and Arduino |
| Push Button | ![]() |
Triggers lock/unlock |
| Breadboard & Jumper Wires | 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
- Power the Arduino using your 12V adapter.
- Open the Serial Monitor โ you should see "Solenoid Lock System Ready".
- Press the button:
- You should hear a click from the relay.
- Solenoid retracts (unlock).
- Press the button again:
- Solenoid extends (lock).
๐โก If everything is wired correctly โ congratulations, youโve built your own electromechanical locking system!






Top comments (1)
Nice tutorial which is easy to follow