ESP8266 Toggle Lock System

The ESP8266 microcontroller serves as the foundation for the Toggle Lock System, designed to offer a remote-controlled locking mechanism. Utilizing basic electronic components, this system facilitates direct control of door locks through the simple press of a switch. It aims to enhance accessibility and security in both residential and commercial environments.

By employing microcontrollers to read from physical buttons and manage outputs such as relays and solenoids. The system is to deliver a robust and responsive locking solution. This is particularly designed to integrate existing door mechanisms.

Managed using the Arduino IDE, the program effectively controls components like buttons and relays connected to the ESP8266. At the heart of the program lies the ezButton library, which is pivotal for ensuring reliable switch operations. The setup involves the initialization of both the button and the relay. The relay acting as the central control point for the locking mechanism. Upon activation of the button, the solenoid will unlock and locked off.



Powering the Breadboard:
  • Connect the Power Adapter 12v:
  • Connect Jumper Wires to Breadboard:
    • The positive (red) jumper wire to the positive rail and negative (black) jumper wire to the negative rail of the 400 Crystal Breadboard.
  • Connect ESP8266 to Breadboard:
    • Connect the VIN pin to the positive rail and GND pin to the negative rail of the breadboard.
See also  Blynk: Making Your Own Mobile Platform for ESP8266
Setting Up the Relay:
  • Power the Relay:
    • Connect the DC+ (VCC) to the positive rail and DC- (GND) to the negative rail of the breadboard.
    • Connect the IN pin of the relay to pin D8 on the ESP8226 v2.
Configuring the Tactile Button:
  • Install the Tactile Button:
    • Connect one pin of the Tactile Button to pin D1 and the other pin to the GND pin on the ESP8266.
Connecting the Solenoid:
  • Connect Solenoid to Relay and Breadboard:
    • Connect the positive wire of the Solenoid Locked to the COM (common) and negative wire to the negative rail of the breadboard.
  • Complete Solenoid Circuit through Relay:
    • Connect the NO (Normally Open) terminal of the relay to the positive rail of the breadboard.
  • Install the ezButton library for the tactile button.
  • Use Multi Screw Set to screw the Relay 12v Octocoupler.
  • With these connections, it should operate effectively, allowing you to control the solenoid lock via the ESP8266, and manually using the tactile button.


This Arduino sketch is designed to control a lock (connected via a relay) using a button on an ESP8266 microcontroller. It uses the ezButton library to handle button inputs, which simplifies the process of debouncing the button. Debouncing is important as it ensures clean inputs are registered by the microcontroller. When the button is pressed, avoiding the false triggering that can occur due to the natural bouncing of electrical contacts when they close.

This code effectively creates a simple locking system where pressing a button toggles the lock state and the current state is indicated both by the relay’s output.

#include <ezButton.h>

#define BUTTON_PIN  D1 // The ESP8266 pin connected to the button's pin
#define RELAY_PIN   D8 // The ESP8266 pin connected to the relay's pin

ezButton button(BUTTON_PIN);  // create ezButton object for pin D1
bool locked = true; // Boolean to keep track of lock state

void setup() {
  Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor.
  pinMode(RELAY_PIN, OUTPUT); // Set ESP8266 pin to output mode
  button.setDebounceTime(50); // Set debounce time to 50 milliseconds

  digitalWrite(RELAY_PIN, HIGH); // Initially lock the door

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed()) {
    locked = !locked; // Toggle the lock state
    if (locked) {
      Serial.println("The door is locked");
      digitalWrite(RELAY_PIN, HIGH); // Lock the door
    } else {
      Serial.println("The door is unlocked");
      digitalWrite(RELAY_PIN, LOW); // Unlock the door


  • Component Isolation: Test each component independently to isolate the faulty part. This could involve swapping components like the relay or ESP8266 with known good units.
  • Connection Check: Make sure the solenoid’s connections to the relay and power supply are secure.
  • Reset and Reboot: Sometimes, simply resetting or rebooting the ESP8266 can resolve transient issues.
  • Serial Output: Check the serial monitor to see if the button press is being detected and is toggling the lock state variable.
  • Physical Inspection: Listen for a clicking sound from the relay when activated; absence of this sound could indicate a malfunction.
See also  How to Start with the Raspberry Pi (Part 2)


Reference Tutorial