ESP8266 - Solenoid Lock

The Solenoid Lock, also referred to as the Electric Strike Lock, can be used to lock and unlock cabinets, drawers, and doors. This tutorial will teach us how to control the solenoid lock using ESP8266.

An alternative to the Solenoid Lock is the Electromagnetic Lock. For more information, please refer to the ESP8266 - Electromagnetic Lock tutorial.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
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 Solenoid Lock

The Solenoid Lock Pinout

The Solenoid Lock consists of two wires:

  • The Positive (+) wire (red) should be connected to the 12V of a DC power supply
  • The Negative (-) wire (black) should be connected to the GND of a DC power supply
Solenoid Lock pinout

How It Works

  • When supplying power to the solenoid lock, the lock tongue is extended, resulting in the door being locked.
  • When NOT supplying power to the solenoid lock, the lock tongue is retracted, resulting in the door being unlocked.

※ NOTE THAT:

The solenoid lock typically requires a 12V, 24V or 48V power source. Consequently, it CANNOT be connected directly to an ESP8266 pin. A relay must be used to link the solenoid lock to the ESP8266 pin.

If the solenoid lock is connected to a relay (in a normally open mode):

  • When the relay is not activated, the door is unlocked
  • When the relay is activated, the door is locked

Connecting ESP8266 to a relay enables programming for ESP8266 to control the solenoid lock. For more information on relays, please refer to the ESP8266 - Relay tutorial.

Wiring Diagram

ESP8266 NodeMCU Solenoid Lock wiring diagram

This image is created using Fritzing. Click to enlarge image

ESP8266 Code

This code causes the door to be locked and unlocked every five seconds.

/* * 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-solenoid-lock */ #define RELAY_PIN 16 // ESP8266 pin GPIO16, which connects 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, LOW); // unlock the door delay(5000); digitalWrite(RELAY_PIN, HIGH); // lock the door delay(5000); }

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.
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the ESP8266.
  • Observe the state of the lock tongue.

ESP8266 - Button Controls Solenoid Lock

  • Diagram of Wiring
ESP8266 NodeMCU Solenoid Lock wiring diagram

This image is created using Fritzing. Click to enlarge image

  • Writing code for an ESP8266 board.
/* * 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-solenoid-lock */ #include <ezButton.h> #define BUTTON_PIN 12 // Arduino pin connected to button's pin #define RELAY_PIN 16 // ESP8266 pin GPIO16 connected to relay's pin ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 12; void setup() { Serial.begin(9600); // initialize serial 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

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.
  • Install the ezButton library. Refer to How To for instructions.
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button on the Arduino IDE to upload the code to the ESP8266.
  • Push the button once.
  • Observe the lock tongue's state for 10 seconds.

※ NOTE THAT:

In the code above, we used the delay function. As a result, we do not have to implement debouncing for the button. Nevertheless, we still included the code with debouncing in case you would like to carry out additional tasks without using the delay function. Check out How to use millis() instead of delay() for more information.

Video Tutorial

Creating videos is a time-consuming task. If you find video tutorials helpful for your learning, please let us know by subscribing to our YouTube channel . If there is high demand for videos, we will work on creating them.