ESP8266 - Button Control Electromagnetic Lock

The tutorial instructs you how to use an ESP8266 and a button to control an electromagnetic lock. When the button is pressed, ESP8266 unlock the door for a period of time (e.g. 10 seconds). After that, the door will be locked again.

We will go from easy to difficult by two steps:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×Push Button
1×(Optional) Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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 Button and Electromagnetic Lock

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

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Button Electromagnetic Lock

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 Code - Button Controls Electromagnetic Lock Without Debouncing

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-button-control-electromagnetic-lock */ #define BUTTON_PIN D1 // The ESP8266 pin connected to button's pin #define RELAY_PIN D8 // The ESP8266 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); // Configure the ESP8266 pin to the input pull-up mode pinMode(RELAY_PIN, OUTPUT); // Configure the ESP8266 pin to the 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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect an ESP8266 to a computer using a USB cable.
  • Open the Arduino IDE, select the appropriate board and port.
  • Copy the code and open it in the Arduino IDE.
  • Hit the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
  • Bring the armature plate close to 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 does not always work correctly. To ensure it functions properly, we need to debounce for the button. Debouncing for the button can be tricky for those just starting out. Fortunately, the ezButton library makes it much simpler.

ESP8266 Code - Button Controls Electromagnetic Lock With Debouncing

What is the purpose of debouncing?. ⇒ See ESP8266 - Button Debounce tutorial to understand why debouncing is necessary.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-button-control-electromagnetic-lock */ #include <ezButton.h> #define BUTTON_PIN D1 // The ESP8266 pin connected to button's pin #define RELAY_PIN D8 // The ESP8266 pin connected to relay's pin ezButton button(BUTTON_PIN); // create ezButton object for pin D8 void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(RELAY_PIN, OUTPUT); // Configure the ESP8266 pin to the 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

  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • 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 ESP8266.
  • 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. As a result, there is no need for debouncing the button. Nevertheless, we still provide the code with debouncing just in case you would like to do more tasks without using the delay function. Check out How to use millis() instead of delay() for more information.

Video Tutorial