Arduino - Control Door Lock via Web
In this tutorial, we will learn how to use Arduino to control a door lock through a web interface that can be accessed using a web browser on a computer or smartphone. Here's how it all works:
- We will program Arduino to act as a web server
- When you type the IP address of your Arduino into the web browser, the web browser makes HTTP request to Arduino.
- Arduino sends back to the web browser with a web page. The web page will show:
- Whether the door is currently locked or unlocked.
- A button to unlock the door.
- Another button to lock the door.
- If this is the first time you use Arduino Uno R4, see how to setup environment for Arduino Uno R4 on Arduino IDE.
- Open the Library Manager by clicking on the Library Manager icon on the left side of the Arduino IDE.
- Search for Web Server for Arduino Uno R4 WiFi and locate the Web Server library created by DIYables.
- Click on the Install button to add the Web Server library.
- Copy the above code and open with Arduino IDE
- Change the wifi information (SSID and password) in the code to yours
- Click Upload button on Arduino IDE to upload code to Arduino
- Open the Serial Monitor
- Check out the result on Serial Monitor.
- You will see an IP address, for example: 192.168.0.2. This is the IP address of the Arduino Web Server
- Open a web browser and enter one of the three formats below into the address bar:
- Please note that the IP address may be different. Make sure to check the current value on the Serial Monitor.
- Additionally, you will observe the following output on the Serial Monitor.
- Check the door lock state
- You will see the web page of Arduino board on the web browser as below:

Hardware Required
Or you can buy the following kits:
1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | |
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .
Additionally, some links direct to products from our own brand, DIYables .
About Solenoid Lock and Arduino Uno R4
If you're not familiar with Arduino Uno R4 and solenoid lock (like their pinout, how they work, and how to program them), you can learn all about them in the following tutorials:
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Arduino Code
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-controls-door-lock-via-web
*/
#include <UnoR4WiFi_WebServer.h>
#define RELAY_PIN 7 // Arduino pin connected to the solenoid lock via relay
const char HTML_CONTENT[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html>
<head>
<link rel="icon" href="data:,">
<title>Door Control</title>
</head>
<body>
<h1>Door Control System</h1>
<p>The door is <span style="color: red;">%DOOR_STATUS%</span></p>
<br>
<a href="/door/unlock">UNLOCK</a>
<br><br>
<a href="/door/lock">LOCK</a>
</body>
</html>
)rawliteral";
// WiFi credentials
const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // change your network SSID (name)
const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // change your network password
// Door state
int doorState = LOW;
// Create web server instance
UnoR4WiFi_WebServer server;
// Handler for the main door control page
void sendHTML(WiFiClient& client) {
String status = (doorState == LOW) ? "LOCKED" : "UNLOCKED";
String response = HTML_CONTENT;
response.replace("%DOOR_STATUS%", status);
server.sendResponse(client, response.c_str());
}
void handleDoor(WiFiClient& client, const String& method, const String& request, const QueryParams& params, const String& jsonData) {
sendHTML(client);
}
// Handler for unlock command
void handleUnlock(WiFiClient& client, const String& method, const String& request, const QueryParams& params, const String& jsonData) {
doorState = HIGH;
digitalWrite(RELAY_PIN, doorState);
Serial.println("Unlock the door");
sendHTML(client);
}
// Handler for lock command
void handleLock(WiFiClient& client, const String& method, const String& request, const QueryParams& params, const String& jsonData) {
doorState = LOW;
digitalWrite(RELAY_PIN, doorState);
Serial.println("Lock the door");
sendHTML(client);
}
void setup() {
Serial.begin(9600);
delay(1000);
// Initialize relay pin
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, doorState);
Serial.println("Arduino Uno R4 WiFi - Door Lock Web Control");
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Configure routes
server.addRoute("/", handleDoor);
server.addRoute("/door/unlock", handleUnlock);
server.addRoute("/door/lock", handleLock);
// Start server
server.begin();
Serial.println("\n=== Web Server Ready! ===");
Serial.print("Visit: http://");
Serial.println(WiFi.localIP());
}
void loop() {
server.handleClient();
}
Quick Steps

COM6
Arduino Uno R4 WiFi - Door Lock Web Control
Connecting to YOUR_WIFI_SSID
connected!
IP address: 192.168.0.254
Starting web server on IP: 192.168.0.254
=== Web Server Ready! ===
Visit: http://192.168.0.254
Autoscroll
Clear output
9600 baud
Newline
192.168.0.2
192.168.0.2/door/unlock
192.168.0.2/door/lock
COM6
Arduino Uno R4 WiFi - Door Lock Web Control
Connecting to YOUR_WIFI_SSID
connected!
IP address: 192.168.0.254
Starting web server on IP: 192.168.0.254
=== Web Server Ready! ===
Visit: http://192.168.0.254
Method: GET
Requested path: /
Client disconnected
Method: GET
Requested path: /door/unlock
Unlock the door
Client disconnected
Method: GET
Requested path: /door/lock
Lock the door
Client disconnected
Autoscroll
Clear output
9600 baud
Newline

If you desire to enhance the web page's appearance with an impressive graphic user interface (UI), you can refer to the Arduino - Web Server tutorial for inspiration and guidance.