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

1×Raspberry Pi 4 Model B
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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
Solenoid Lock pinout

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

The wiring diagram between Raspberry Pi and Solenoid Lock

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:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file solenoid_lock.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-solenoid-lock """ import RPi.GPIO as GPIO import time # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin controlled the solenoid lock via the relay module RELAY_PIN = 12 # Set the relay pin as an output pin GPIO.setup(RELAY_PIN, GPIO.OUT) try: # Run the loop function indefinitely while True: # Turn the relay ON (HIGH) to lock the door GPIO.output(RELAY_PIN, GPIO.HIGH) time.sleep(2) # Wait for 2 seconds # Turn the relay OFF (LOW) to unlock the door GPIO.output(RELAY_PIN, GPIO.LOW) time.sleep(2) # Wait for 2 seconds except KeyboardInterrupt: # If the user presses Ctrl+C, clean up the GPIO configuration GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 solenoid_lock.py
  • Check out the state of the lock tongue.

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Raspberry Pi - Button Controls Solenoid Lock

The below Python script for Raspberry Pi controls a door lock using a button. When you press the button, the door is unlocked for 5 seconds and then the door is locked again. You can repeat this process by pressing the button once more.

Detailed Instructions

  • Connect a button and solenoid lock to the Raspberry Pi like below wiring diagram
The wiring diagram between Raspberry Pi and button solenoid lock

This image is created using Fritzing. Click to enlarge image

  • Create a Python script file button_solenoid_lock.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-solenoid-lock """ import RPi.GPIO as GPIO import time BUTTON_PIN = 18 # GPIO pin connected to the button RELAY_PIN = 16 # GPIO pin controlled the solenoid lock via the relay module # Set up the GPIO pins GPIO.setmode(GPIO.BCM) GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(RELAY_PIN, GPIO.OUT) prev_button_state = GPIO.HIGH # HIGH means the button is not pressed initially try: # Lock the door initially GPIO.output(RELAY_PIN, GPIO.HIGH) while True: button_state = GPIO.input(BUTTON_PIN) if button_state == GPIO.LOW and prev_button_state == GPIO.HIGH: # Button is pressed (LOW means pressed due to pull-up resistor) print("The button is pressed") GPIO.output(RELAY_PIN, GPIO.LOW) # Unlock the door print("The door is unlocked") time.sleep(5) # Wait for 5 seconds GPIO.output(RELAY_PIN, GPIO.HIGH) # Lock the door again print("The door is locked again") # Update the previous button state prev_button_state = button_state except KeyboardInterrupt: print("Exiting...") GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 button_solenoid_lock.py
  • Press the button once.
  • Check out the lock tongue's state for 5 seconds.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!