Arduino Nano - Electromagnetic Lock
This tutorial instructs you how to use Arduino Nano to control the electromagnetic lock, which is also known as magnetic lock, maglock, or EM lock.
An alternative for the electromagnetic lock is the solenoid lock. For more information, check out the Arduino Nano - Solenoid Lock tutorial.
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 Electromagnetic Lock
The electromagnetic lock is a popular choice for door locking. It is often used in conjunction with a switch, fingerprint reader, RFID/NFC reader, keypad, or PC/mobile application to regulate access to doors, gates, and other entry points.
The Electromagnetic Lock Pinout
An electromagnetic lock comprises of two parts:
- An electromagnet, which has two pins
- An armature plate
How It Works
- When the electromagnet is powered, a current passing through it generates a magnetic flux that results in the armature plate being drawn to the electromagnet, thus creating a locking action.
- However, when the electromagnet is NOT powered, there is no magnetic flux and the armature plate does NOT attract to the electromagnet, leading to an unlocking action.
※ NOTE THAT:
The electromagnetic lock typically requires 12V, 24V or 48V power supply, so it CANNOT be connected directly to an Arduino Nano pin. A relay must be used to link the lock to the pin.
If we attach the electromagnetic lock to a relay (in the normally open mode):
- When the relay is not activated, the door will be unlocked
- When the relay is triggered, the door will be locked
Connecting Arduino Nano to a relay allows us to program it to control an electromagnetic lock. To find out more, take a look at the Arduino Nano - Relay tutorial.
For installation, the armature plate should be affixed to the door/window (the part that moves), and the electromagnet should be attached to the door frame (the part that stays in place). The two components will be in contact when the door is shut.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino Nano Code for Controlling EM Lock
The code below causes the door to be locked and unlocked every 5 seconds.
Detailed Instructions
- Copy the above code and open it using the Arduino IDE.
- Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
- Place the armature plate near the electromagnet.
- Check out the attraction between the armature plate and the electromagnet.