Arduino MicroPython Solenoid Lock
This tutorial teaches you how to use an Arduino with MicroPython and a solenoid lock to secure doors, cabinets, or drawers. We will cover:
- How to connect a solenoid lock to an Arduino.
- How to program the Arduino with MicroPython to control the solenoid lock.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
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 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.
How It Works
- When the solenoid lock receives power, the locking bar extends to lock the door.
- When the solenoid lock is not powered, the locking bar pulls back to unlock the door.
※ NOTE THAT:
The solenoid lock typically needs a 12V, 24V, or 48V power supply. Don't connect the solenoid lock directly to the Arduino pin; instead, use a relay to connect it to the Arduino.
If the solenoid lock is connected to a relay set to normally open:
- When the relay is open, the door stays unlocked.
- When the relay is closed, the door stays locked.
You can connect an Arduino to the solenoid lock through the relay and program the Arduino to control the lock. If you're new to relay or MicroPython programming for the Arduino, I recommend checking out the below tutorial:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino MicroPython Code
This code will lock and unlock the door every five seconds.
Detailed Instructions
Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Make sure MicroPython firmware is installed on your Arduino board.
- If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
- Connect the Arduino board to the solenoid lock according to the provided diagram.
- Connect the Arduino board to your computer with a USB cable.
- Open Thonny IDE and go to Tools Options.
- Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
- Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
- Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
- Save the MicroPython code to your Arduino by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device and name the file main.py.
- Click the green Run button (or press F5) to execute the code.
- Check the position of the lock tongue.