Arduino UNO R4 - Solenoid Lock
This tutorial instructs you how to use Arduino UNO R4 and the solenoid lock to lock/unlock your door, cabinet, or drawer. In detail, We will learn:
- How to connect the solenoid lock to an Arduino UNO R4.
- How to program the Arduino UNO R4 to control the solenoid lock.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | |
| 1 | × | DIYables STEM V4B IoT Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Solenoid Lock
The solenoid lock, also called the electric strike lock, is used to lock or unlock items like cabinets, drawers, and doors.
Pinout
The solenoid lock has two wires:
- Connect the positive (+) red wire to the 12V DC power supply.
- Connect the negative (-) black bit to the ground (GND) of the DC power supply.

How It Works
- When the solenoid lock has power, the lock tongue (strike) moves out ⇒ the door is locked.
- When the solenoid lock has no power, the lock tongue (strike) pulls back ⇒ the door is unlocked.
※ NOTE THAT:
The solenoid lock typically needs a 12V, 24V, or 48V power supply. We MUST NOT connect the solenoid lock directly to the Arduino UNO R4 pin. Instead, connect it through a relay to the Arduino UNO R4 pin.
If we connect the solenoid lock to a relay in the normally open setting:
- If the relay is open, the door is unlocked.
- If the relay is closed, the door is locked.
You can connect an Arduino UNO R4 to a relay to program it to control a solenoid lock. Find out more about using a relay with Arduino UNO R4 in our tutorial Arduino UNO R4 - Relay Tutorial.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Jumper wires are good for a quick test, but not for a final build. The Arduino UNO R4 works with both the Proto Screw Terminal Block Shield and the Screw Shield V2. Both turn the header pins into screw terminals. The wiring stays the same. Only the wires now go to the screw terminals, as shown below.

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Uno R4 and other components.
Arduino UNO R4 Code
This code will lock and unlock the door every 5 seconds.
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the solenoid lock to the Arduino Uno R4 according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy the code and open it with Arduino IDE
- Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4
- Check the position of the lock tongue
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 UNO R4 - Water/Liquid Valve tutorial. The wiring for a solenoid lock is in the Arduino UNO R4 - MOSFET Module tutorial.