Arduino Mega - Solenoid Lock
This guide shows you how to use the Arduino Mega board and a solenoid lock to lock or unlock a door, cabinet, or drawer. Here is what we will learn:
- How to connect a solenoid lock to an Arduino Mega.
- How to write code for the Arduino Mega to control the solenoid lock.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Solenoid Lock
A solenoid lock, also called an electric strike lock, locks and unlocks cabinets, drawers, and doors.
Pinout
A solenoid lock uses two wires.
- Connect the red wire (positive) to the 12V DC power supply.
- Connect the black wire (negative) to the ground (GND) of the power supply.

How It Works
- When the electric lock has power, the latch extends and the door is locked.
- When there is no power, the latch retracts and the door is unlocked.
※ NOTE THAT:
The solenoid lock usually needs a power supply of 12V, 24V, or 48V. Do not connect the lock directly to an Arduino Mega pin. Instead, use a relay between the lock and the Arduino Mega pin.
If we connect the solenoid lock to a relay that is normally open:
- If the switch is off, the door is unlocked.
- If the switch is on, the door is locked.
Connect an Arduino Mega to a relay to control a solenoid lock. Learn more about using a relay with Arduino Mega in our guide: Arduino Mega - Relay Tutorial.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Arduino Mega Code
This program locks and unlocks the door every five seconds.
Detailed Instructions
Follow these steps one by one.
- Connect the solenoid lock to the Arduino Mega as shown in the diagram.
- Connect the Arduino Mega to your computer with a USB cable.
- Open the Arduino IDE on your computer.
- Choose the correct board (Arduino Mega) and the right COM port.
- Copy the code and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
- Check the lock bolt position.
Relay vs MOSFET Module for the Solenoid Lock
This tutorial uses a relay to switch the solenoid lock. A 12V solenoid lock runs on DC, so a MOSFET module can also switch it.
| Relay | MOSFET Module | |
|---|---|---|
| Lock control | lock or unlock | lock or unlock |
| Switching sound | a click every time | no sound |
| Switching time | slow (milliseconds) | very fast |
| Wear | the contacts get old with use | no moving parts to wear |
| Works with | AC and DC locks | DC locks only |
| Flyback diode | most relay modules do not need one | must add one (1N4007 or 1N5819) |
| Isolation | yes | yes, with a PC817 optocoupler |
| Choose it for | a simple, common setup | a silent lock, or a door that opens many times a day |
A solenoid lock is only locked or unlocked, so the MOSFET module does not give extra control here. Its gains are: no click, and nothing wears out. Both turn on when the pin is HIGH, so the code stays the same. Do not keep the lock unlocked for a long time, or it gets hot. This is the same with a relay.
A solenoid valve works the same way as a solenoid lock. To see how to wire and code one with both a relay and a MOSFET module, see the Arduino Mega - Water/Liquid Valve tutorial. The wiring for a solenoid lock is in the Arduino Mega - MOSFET Module tutorial.