Raspberry Pi - Solenoid Lock
The Solenoid Lock, also referred to as the Electric Strike Lock, can be used to secure/release cabinets, drawers, and doors. This tutorial instructs you how to manipulate the solenoid lock using Raspberry Pi.
An alternative to the Solenoid Lock is the Electromagnetic Lock. For more information, please refer to the Raspberry Pi - Electromagnetic Lock tutorial.
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
The Solenoid Lock Pinout
The Solenoid Lock has two wires:
- The Positive (+) wire (red) should be connected to the 12V of a DC power supply
- The Negative (-) wire (black) should be connected to the GND of a DC power supply

How It Works
- When the Solenoid Lock is powered, the lock tongue is extended, thus locking the door.
- When the Solenoid Lock is NOT powered, the lock tongue is retracted, thus unlocking the door.
※ NOTE THAT:
The solenoid lock typically requires 12V, 24V or 48V for operation. Therefore, it CANNOT be connected directly to a Raspberry Pi pin. A relay must be used to connect the solenoid lock to a Raspberry Pi pin.
If we connect the solenoid lock to the power source via a relay (in the normally open mode):
- When the relay is in an open state, the door will be unlocked.
- When the relay is in a closed state, the door will be locked.
We can connect the relay to Raspberry Pi and program Raspberry Pi to control the solenoid lock via relay. To find out more about relays, please refer to the Raspberry Pi - Relay tutorial.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code
The code below will cause the door to be locked and unlocked every 2 seconds.
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file solenoid_lock.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Check out the state of the lock tongue.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
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 Raspberry Pi pin is HIGH (GPIO.output(pin, GPIO.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 Raspberry Pi - Water/Liquid Valve tutorial. The wiring for a solenoid lock is in the Raspberry Pi - MOSFET Module tutorial.


