Arduino UNO R4 - Electromagnetic Lock

In this tutorial, we are going to learn how to use Arduino UNO R4 to control the electromagnetic lock to programmably lock/unlock your door.

Arduino UNO R4 electromagnetic lock

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

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. We appreciate your support.

Overview of Electromagnetic Lock

The electromagnetic lock, often called a maglock, is commonly used to secure doors. It typically works with various devices like switches, fingerprint readers, RFID/NFC readers, keypads, or applications on PCs and mobile devices to manage access to doors and building entrances.

Pinout

An electromagnetic lock has two parts:

  • One electromagnet, has two pins
  • One plate for the armature
Electromagnetic Lock Pinout

How It Works

  • When the electromagnet is on, electricity flows through it and produces a magnetic field that pulls the armature plate towards the electromagnet, causing it to lock. When the electromagnet is off, there is no magnetic field, and the armature plate does not get pulled towards the electromagnet, allowing it to unlock.

※ NOTE THAT:

The electromagnetic lock often needs 12V, 24V, or 48V power supply. Thus, we CANNOT connect the electromagnetic lock directly to an Arduino UNO R4 pin. Instead, we must connect it through a relay to the Arduino UNO R4 pin.

If we link the electromagnetic lock to a relay in the normally open setting:

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

You can connect Arduino UNO R4 to a relay to program it to operate an electromagnetic lock. To learn more about using a relay with Arduino UNO R4, visit Arduino UNO R4 - Relay tutorial.

To install it, fix the armature plate to the door or window (the part that moves) and attach the electromagnet to the door frame (the part that does not move). When you close the door, these two parts should touch each other.

Wiring Diagram

The wiring diagram between Arduino UNO R4 Electromagnetic Lock

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

This code locks and unlocks the door every 5 seconds.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-electromagnetic-lock */ #define RELAY_PIN 3 // The Arduino UNO R4 pin connected to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // lock the door delay(5000); digitalWrite(RELAY_PIN, LOW); // unlock the door delay(5000); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the electromagnetic lock to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code above and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to send the code to Arduino UNO R4.
  • Bring the armature plate near to the electromagnet.
  • Observe the attraction between the armature plate and the electromagnet.

Video Tutorial

Arduino UNO R4 - Button Controls Electromagnetic Lock

Visit the Arduino UNO R4 - 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!