Loading WURK...
Danny_David
Danny_David
Computer Engineering student skilled in Hardware, Networking, AI & Web3. I enjoy building, troubleshooting, and turning technical ideas into working solutions.

How to Build a Simple Wi-Fi Controlled Door Lock with ESP8266

**Blog Summary** In this guide, I show how I built a simple Wi-Fi controlled door lock using an ESP8266 and servo motor. I’ll walk through the components, wiring, Arduino code, Wi-Fi setup, and web interface used to lock and unlock the doo

Published on August 8, 20266 min read

How I Built a Simple Wi-Fi Controlled Door Lock with an ESP8266

I’ve always been interested in building small projects that combine electronics with something practical. One project I worked on was a simple door-lock system that I could control over Wi-Fi using an ESP8266 and a servo motor.

The idea is pretty simple: instead of physically turning the lock, I can open a webpage on my phone or laptop, press a button, and the servo moves the lock.

It’s not a commercial smart lock, but it’s a really good beginner project if you’re interested in IoT, Arduino, or embedded systems.

What I Used

For my setup, I used:

  • ESP8266 development board
  • Servo motor
  • Jumper wires
  • Breadboard
  • USB cable
  • Arduino IDE
  • Wi-Fi network
  • A simple door-lock mechanism

The ESP8266 handles the Wi-Fi and the webpage, while the servo does the physical movement.

How the Project Works

The basic flow is:

Phone/Laptop → Wi-Fi → ESP8266 → Servo → Door Lock

The ESP8266 runs a small web server. When I enter its IP address in my browser, I get a simple page with LOCK and UNLOCK buttons.

When I press one of the buttons, the ESP8266 receives the request and tells the servo where to move.

Step 1: Setting Up Arduino IDE

First, I installed the Arduino IDE and added support for the ESP8266 board.

After connecting the ESP8266 to my computer with a USB cable, I selected the correct board and COM port from the Arduino IDE.

One small problem beginners sometimes run into is the USB cable. If the computer doesn't detect the ESP8266, check whether you're using a data cable rather than a charging-only cable.

Step 2: Connecting the Servo

The servo normally has three wires:

  • Power
  • Ground
  • Signal

The signal wire goes to a GPIO pin on the ESP8266, while the power and ground are connected appropriately.

One thing I learned while working on this is that servo motors can draw more current than you might expect. If the servo keeps shaking or the ESP8266 suddenly restarts, don't immediately assume the code is wrong. Check the power supply.

Also, make sure the ESP8266 and the servo have a common ground.

Step 3: Programming the ESP8266

This is where the project starts getting interesting.

I programmed the ESP8266 to connect to my Wi-Fi and create a small webpage. The webpage contains buttons that send commands to the board.

Here is the basic code I used:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Servo.h>

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

ESP8266WebServer server(80);
Servo lockServo;

const int servoPin = D4;

void lockDoor() {
  lockServo.write(0);
  server.send(200, "text/html", "Door Locked");
}

void unlockDoor() {
  lockServo.write(90);
  server.send(200, "text/html", "Door Unlocked");
}

void handleRoot() {
  String page = "<html><body>";
  page += "<h1>Wi-Fi Door Lock</h1>";
  page += "<a href='/lock'><button>LOCK</button></a>";
  page += "<br><br>";
  page += "<a href='/unlock'><button>UNLOCK</button></a>";
  page += "</body></html>";

  server.send(200, "text/html", page);
}

void setup() {
  Serial.begin(115200);

  lockServo.attach(servoPin);
  lockServo.write(0);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);
  server.on("/lock", lockDoor);
  server.on("/unlock", unlockDoor);

  server.begin();
}

void loop() {
  server.handleClient();
}

You'll need to replace the Wi-Fi name and password with your own.

Step 4: Finding the IP Address

After uploading the code, I opened the Serial Monitor in Arduino IDE and set it to 115200 baud.

Once the ESP8266 connected to Wi-Fi, it displayed an IP address.

Something like:

192.168.1.10

I entered that address into my browser while connected to the same Wi-Fi network.

And there it was — my little door-control webpage.

Step 5: Testing the Lock

This was probably the most satisfying part.

I pressed UNLOCK on the webpage and watched the servo move.

Then I pressed LOCK, and it moved back.

If the servo moves in the opposite direction from what you want, you can simply change the angle in the code.

For example:

lockServo.write(90);

can be changed to another angle depending on how your particular setup is mounted.

The exact angle isn't universal because every physical lock mechanism will be positioned differently.

A Few Problems I Ran Into

Building it wasn't completely perfect from the beginning. 😅

The ESP8266 wouldn't connect

I checked my Wi-Fi name and password first. I also made sure I was using a 2.4 GHz network, since ESP8266 boards generally use 2.4 GHz Wi-Fi.

The servo was acting strangely

If your servo shakes or the ESP8266 keeps restarting, check the power supply. The servo may be drawing more current than the board can comfortably provide.

The webpage wouldn't open

I made sure my phone/laptop and ESP8266 were connected to the same Wi-Fi network. I also checked that I was using the correct IP address from the Serial Monitor.

The servo moved but the lock didn't

This is where the physical setup becomes important. The servo needs to be mounted properly so its movement actually moves the locking mechanism.

What I Like About This Project

What I enjoyed most was seeing different areas come together.

The ESP8266 handles the networking.

The webpage gives me the controls.

The servo converts the command into physical movement.

And the lock mechanism turns that movement into something useful.

That's what makes projects like this interesting to me. You start with a small idea, and before you know it, you're combining programming, electronics, networking, and mechanical design.

What You Can Add Later

Once the basic version is working, there are many ways to improve it.

You could add:

  • A password-protected webpage
  • A keypad
  • RFID access
  • A door sensor
  • Automatic locking
  • Remote notifications
  • User authentication
  • A better physical locking mechanism

But I would recommend getting the basic version working first before adding all of these.

Final Thoughts

If you're just getting started with IoT, this is a fun project to try.

You don't need an expensive smart-home system to learn how these things work. An ESP8266 and a servo can already teach you a lot about Wi-Fi, web servers, microcontrollers, and automation.

My biggest advice is don't try to build everything at once.

Get the ESP8266 connected to Wi-Fi first.

Then get the servo moving.

Then create the webpage.

Finally, connect everything together.

That's the approach that makes the project much easier to understand and troubleshoot.

And when you finally press a button on your phone and see the physical lock move, it feels pretty good. 😄

Conversation

Join the conversation

Share your thoughts with the author and other readers.

Comments0

Checking your account…

Latest comments

0
No comments yet. Be the first to share your thoughts.