Raspberry Pi Pico - Solenoid Lock

This guide shows you how to lock or unlock doors, cabinets, or drawers using the Raspberry Pi Pican and a solenoid lock. We will learn:

Raspberry Pi Pico Solenoid Lock

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following kits:

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, also known as the electric strike lock, is used to lock or unlock things like cabinets, drawers, and doors.

Pinout

The solenoid lock has two wires.

  • Attach the red wire with the plus (+) sign to the 12V DC power source.
  • Attach the black wire with the minus (-) sign to the ground (GND) of the DC power source.
Solenoid Lock Pinout

How It Works

  • When the solenoid lock is powered, the locking bar moves out, locking the door.
  • When the solenoid lock is not powered, the locking bar retracts, unlocking the door.

※ NOTE THAT:

The solenoid lock usually requires a 12V, 24V, or 48V power supply. Do not connect the solenoid lock directly to the Raspberry Pi Pico pin. You should connect it using a relay to the Raspberry Pi Pico pin.

If we link the solenoid lock to a relay that is set to normally open:

  • When the relay is open, the door can be opened.
  • When the reconciliation is closed, the door remains locked.

You can link a Raspberry Pi Pico to a relay to set it up for managing a solenoid lock. Learn more about how to use a relay with Raspberry Pi Pico in our guide Raspberry Pi Pico - Relay Tutorial.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico solenoid lock

This image is created using Fritzing. Click to enlarge image

Here is the same wiring without a breadboard:

The wiring diagram between Raspberry Pi and Pico solenoid lock  (No Breadboard)

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

This code will lock and unlock the door every five seconds.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-solenoid-lock """ from machine import Pin import time RELAY_PIN = 1 # The Raspberry Pi Pico pin GP1 connected to the Solenoid Lock via relay pin # Initialize the relay pin as an output relay = Pin(RELAY_PIN, Pin.OUT) # Infinite loop while True: relay.value(1) # lock the door for 5 seconds time.sleep(5) relay.value(0) # unlock the door for 5 seconds time.sleep(5)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the solenoid lock according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check the position of the lock tongue.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

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 Pico pin is HIGH (value(1)), 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 Pico - Water/Liquid Valve tutorial. The wiring for a solenoid lock is in the Raspberry Pi Pico - MOSFET Module tutorial.

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!