Arduino MicroPython Electromagnetic Lock

This tutorial instructs you how to use an Arduino with MicroPython to control an electromagnetic lock for securing and unlocking your door. Specifically, we will cover:

Arduino MicroPython electromagnetic lock

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

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 Electromagnetic Lock

The electromagnetic lock, or maglock, is commonly used to secure doors. It often works with various devices like switches, fingerprint scanners, RFID/NFC readers, keypads, or software on computers and mobile devices to control access to doors and buildings.

Pinout

An electromagnetic lock is made up of two parts:

  • An electromagnet with two pins
  • An armature plate
Electromagnetic Lock Pinout

How It Works

  • When the electromagnet is powered on, an electric current flows through it, generating a magnetic field that pulls the armature plate toward it, locking the door.
  • When the electromagnet is powered off, the magnetic field disappears, releasing the armature plate and unlocking the door.

※ NOTE THAT:

The electromagnetic lock typically needs a 12V, 24V, or 48V power supply, so it can't be connected directly to an Arduino pin. Instead, it should be connected to the Arduino through a relay.

If the electromagnetic lock is connected to a relay set to 'normally open':

  • When the relay is open, the door remains unlocked.
  • When the relay is closed, the door stays locked.

You can connect an Arduino to a relay to control the electromagnetic lock. For more details on how to connect a relay to an Arduino, check out the Arduino MicroPython Relay tutorial.

To install the lock, attach the armature plate to the door or window (the moving part) and mount the electromagnet to the door frame (the stationary part). Make sure these two parts come into contact when the door is closed.

Wiring Diagram

The wiring diagram between Arduino MicroPython Electromagnetic Lock

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code

This code makes the door lock and unlock every 5 seconds.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-electromagnetic-lock """ from machine import Pin import time RELAY_PIN = 'D5' # The Arduino Giga WiFi pin D5 connected to the Electromagnetic 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

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 electromagnetic 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.
  • Place the armature plate close to the electromagnet.
  • Check out how the armature plate is attracted to the electromagnet.

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!