Arduino Nano 33 IoT - Electromagnetic Lock

The electromagnetic lock (also called magnetic lock, maglock, or EM lock) is an important part of the door locking system. This guide shows how to use the Arduino Nano 33 IoT to control the electromagnetic lock.

Arduino Nano 33 IoT Electromagnetic Lock

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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

Electromagnetic Lock Pinout

The electromagnetic lock is made up of two parts:

  • Armature plate: This part is fixed to the door’s moving section.
  • Electromagnet: This part is mounted on the door frame. It has two wires that connect to a power source.

When the door is shut, two parts touch each other.

Electromagnetic Lock Pinout

How Electromagnetic Lock Works

  • When the electromagnet is turned on, it pulls the armature plate and locks. When it is turned off, it does not pull the armature plate and stays unlocked.

The electromagnetic lock runs on high power (12V, 24V, or 48V, for example). Because of this, we need to add a relay between the lock and the Arduino Nano 33 IoT pin. Check out the Arduino Nano 33 IoT Relay tutorial.

If we attach the electromagnetic lock to a relay that is normally open and use an Arduino Nano 33 IoT pin to control it:

  • When the Arduino Nano 33 IoT pin is low, the relay is turned off and the door stays unlocked.
  • When the Arduino Nano 33 IoT pin is high, the relay is turned on and the door becomes locked.

Wiring Diagram between Electromagnetic Lock and ES32

The wiring diagram between Arduino Nano and 33 IoT Electromagnetic Lock

This image is created using Fritzing. Click to enlarge image

The diagram below shows the same connections without a breadboard:

The wiring diagram between Arduino Nano and 33 IoT Electromagnetic Lock  (No Breadboard)

This image is created using Fritzing. Click to enlarge image

Jumper wires can come loose when you move the project. Simply plug the Arduino Nano 33 IoT into a Screw Terminal Expansion Board for Arduino Nano. Below is the wiring diagram with the screw terminal board.

The wiring diagram between Arduino Nano and 33 IoT Electromagnetic Lock Screw Terminal

This image is created using Fritzing. Click to enlarge image

Note: on the Arduino Nano 33 IoT, the 5V pin is the VUSB pin. It gives 5V only after you close the VUSB solder jumper on the bottom of the board, and only when the board runs on USB power.

Arduino Nano 33 IoT Code

The code below locks and unlocks the door every five seconds.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-electromagnetic-lock */ #define RELAY_PIN 2 // The Arduino Nano 33 IoT pin connected to the electromagnetic lock via the relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, HIGH); // lock the door delay(4000); digitalWrite(RELAY_PIN, LOW); // unlock the door delay(4000); }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button to compile and send the code to the Arduino Nano 33 IoT board.
  • Bring the armature plate close to the electromagnet.
  • Watch how the armature plate is pulled toward the electromagnet.

Relay vs MOSFET Module for the Electromagnetic Lock

This tutorial uses a relay to switch the electromagnetic lock. A 12V electromagnetic 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 that stays ON for a long time

An electromagnetic lock stays ON all the time to keep the door locked. The MOSFET module handles this well: its current is small, so it stays cool. Both turn on when the pin is HIGH, so the code stays the same. The lock has a coil inside, so put the diode across it: the stripe side (cathode) to the lock +, the other side to the lock −. The 3.3V pin of the Arduino Nano 33 IoT is enough to drive the module.

No tutorial uses both a relay and a MOSFET module for an electromagnetic lock yet. The closest is the Arduino Nano 33 IoT - Water/Liquid Valve tutorial, because a valve also has a coil and is only ON or OFF. For the MOSFET module wiring and safety tips, see the Arduino Nano 33 IoT - MOSFET Module tutorial.

Video Tutorial

Arduino Nano 33 IoT - Button Controls Electromagnetic Lock

Check out the Arduino Nano 33 IoT - Button Controls Electromagnetic Lock 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!