Arduino Nano - Button Control Electromagnetic Lock

The tutorial instructs you how to use Arduino Nano and button to control the door lock by using an electromagnetic lock. When the button is pressed, the door will be unlocked for a specific time (e.g. 10 seconds). After that, it will be locked again.

We will progress by two steps from easy to difficult:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

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

If you are unfamiliar with electromagnetic locks and buttons (including pinouts, functionality, and programming), the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and Button Electromagnetic Lock

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Button Controls Electromagnetic Lock Without Debouncing

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-control-electromagnetic-lock */ #define BUTTON_PIN 8 // The Arduino Nano pin connected to button's pin #define RELAY_PIN 2 // The Arduino Nano pin connected to relay's pin int prev_button_state; // The previous state of button int button_state; // The current state of button void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode digitalWrite(RELAY_PIN, HIGH); // lock the door button_state = digitalRead(BUTTON_PIN); } void loop() { prev_button_state = button_state; // save the last state button_state = digitalRead(BUTTON_PIN); // read new state if(prev_button_state == HIGH && button_state == LOW) { Serial.println("The button is pressed"); digitalWrite(RELAY_PIN, LOW); // unlock the door in 10 seconds delay(10000); // 10 seconds digitalWrite(RELAY_PIN, HIGH); // lock the door again } }

Detailed Instructions

  • Connect an Arduino Nano to a PC using a USB cable.
  • Open the Arduino IDE, select the appropriate board and port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino Nano.
  • Place the armature plate near the electromagnet.
  • Press the button once.
  • Check out the attraction between the armature plate and the electromagnet for 10 seconds.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

※ NOTE THAT:

In practice, the code mentioned above may not always work correctly. To guarantee its proper functioning, we need to debounce for the button. Debouncing for the button is not a straightforward task for beginners. However, thanks to the ezButton library, it can be done easily.

Arduino Nano Code - Button Controls Electromagnetic Lock With Debouncing

Why is debouncing necessary?. See the Arduino Nano - Button Debounce tutorial for more information.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-control-electromagnetic-lock */ #include <ezButton.h> #define BUTTON_PIN 8 // The Arduino Nano pin connected to button's pin #define RELAY_PIN 2 // The Arduino Nano pin connected to relay's pin ezButton button(BUTTON_PIN); // create ezButton object for pin 7; void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode button.setDebounceTime(50); // set debounce time to 50 milliseconds digitalWrite(RELAY_PIN, HIGH); // lock the door } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); digitalWrite(RELAY_PIN, LOW); // unlock the door in 10 seconds delay(10000); // 10 seconds digitalWrite(RELAY_PIN, HIGH); // lock the door again } }

Detailed Instructions

  • Install the ezButton library. Refer to How To for instructions.
  • Open the code in the Arduino IDE and click the Upload button to upload it to the Arduino Nano.
  • Bring the armature plate close to the electromagnet and press the button once.
  • Check out the attraction between the armature plate and the electromagnet for 10 seconds.

※ NOTE THAT:

In the code above, we utilized the delay function. Consequently, we do not need to implement debouncing for the button. Nevertheless, we still offer the code with debouncing just in case you would like to execute additional tasks without using the delay function. Refer to How to use millis() instead of delay() for more information.

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!