Skip to content

Smart Door Lock Using Arduino Nano 33 IoT & SG92 Servo Motor

4 min read 3,769 views
Smart Door Lock Using Arduino Nano 33 IoT & SG92 Servo Motor

Smart home projects are becoming increasingly popular, and one of the most useful applications is a smart door lock. With just an Arduino Nano 33 IoT, a micro servo (SG92), and some simple code, you can build a WiFi-controlled locking system that works directly from your smartphone browser — no extra app required!

In this tutorial, we’ll guide you step by step through building your own IoT-based door lock.

https://youtu.be/YEelgakTbWU

PCBWay now offers a free purple solder mask at no extra charge, making your boards stand out in style. PCBWay provides high-quality PCB prototyping and assembly at an affordable price, starting at just $5 for 5 PCBs. With fast turnaround, great customer support, and easy online ordering, it’s a top choice for hobbyists and professionals alike.

Hardware Components

You'll need the following hardware components to get started:

QuantityComponentDescription
1Arduino Nano 33 IoTMicrocontroller board with built-in WiFi (ideal for IoT projects)
1SG92 Micro Servo MotorSmall servo motor used to control the lock mechanism
1BreadboardFor easy prototyping and connections
1Micro-USB CableTo power and program the Arduino
FewJumper WiresFor making the circuit connections

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

Arduino Nano 33 IoT PinSG92 Servo Motor PinDescription
D9Signal (Orange)Controls servo movement
5VVCC (Red)Powers the servo motor
GNDGND (Brown)Common ground connection

How the Project Works

The Arduino Nano 33 IoT connects to your home WiFi network using the WiFiNINA library. Once connected, it runs a small web server that can be accessed from any device (PC, tablet, smartphone) on the same network.

When you open the Arduino’s IP address in a browser, you’ll see a simple webpage with two buttons:

  • Lock 🔒 → Moves the servo to lock position (0°)
  • Unlock 🔓 → Moves the servo to unlock position (90°)

The webpage also displays the current lock status with icons for better visualization.

Arduino Code

#include <WiFiNINA.h>
#include <Servo.h>

// Replace with your WiFi credentials
char ssid[] = "Wi-Fi DCB367 2.4G";
char pass[] = "tsTSP2SG";

WiFiServer server(80);
Servo myServo;

// Servo positions
int lockPos = 0;     // locked
int unlockPos = 90;  // unlocked
String lockState = "LOCKED";  // initial state

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

  // Connect to WiFi
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nConnected to WiFi");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  server.begin();

  // Attach servo to pin 9
  myServo.attach(9);
  myServo.write(lockPos);
}

void loop() {
  WiFiClient client = server.available();
  if (client) {
    String request = client.readStringUntil('\r');
    client.flush();

    if (request.indexOf("/LOCK") != -1) {
      myServo.write(lockPos);
      lockState = "LOCKED";
    } else if (request.indexOf("/UNLOCK") != -1) {
      myServo.write(unlockPos);
      lockState = "UNLOCKED";
    }

    // --- Send HTTP response once ---
    client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
    client.println("Connection: close");
    client.println();

    // --- HTML Page ---
    client.println("<!DOCTYPE HTML>");
    client.println("<html>");
    client.println("<head>");
    client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
    client.println("<style>");
    client.println("body { font-family: Arial; text-align: center; margin-top: 50px; }");
    client.println("button { padding: 15px 30px; margin: 10px; font-size: 20px; border-radius: 10px; cursor: pointer; }");
    client.println(".lock { background: #e74c3c; color: white; }");
    client.println(".unlock { background: #2ecc71; color: white; }");
    client.println("img { width: 120px; margin: 20px auto; display: block; }");
    client.println("</style>");
    client.println("</head>");
    client.println("<body>");

    client.println("<h1>Smart Door Lock</h1>");
    
    // Show logo depending on state
    if (lockState == "LOCKED") {
      client.println("<img src='https://img.icons8.com/emoji/512/locked.png' alt='Locked'>");
      client.println("<h2>Status: <span style='color:red;'>LOCKED</span></h2>");
    } else {
      client.println("<img src='https://img.icons8.com/emoji/512/unlocked.png' alt='Unlocked'>");
      client.println("<h2>Status: <span style='color:green;'>UNLOCKED</span></h2>");
    }

    // Buttons
    client.println("<button class='lock' onclick=\"location.href='/LOCK'\">Lock</button>");
    client.println("<button class='unlock' onclick=\"location.href='/UNLOCK'\">Unlock</button>");

    client.println("</body></html>");
    client.stop();  // Close connection so page finishes loading
  }
}

Uploading & Testing

  1. Open the Arduino IDE and install WiFiNINA and Servo libraries (if not already installed).
  2. Replace the WiFi SSID and Password in the code with your own.
  3. Upload the code to your Arduino Nano 33 IoT.
  4. Open the Serial Monitor (9600 baud).
    • Wait for the "Connected to WiFi" message.
    • Note the IP Address displayed.
  5. Open that IP in your browser (make sure your device is connected to the same WiFi).
  6. You’ll see the control webpage with Lock and Unlock buttons.

Conclusion

With just a few components, we built a WiFi-enabled Smart Door Lock using Arduino Nano 33 IoT and SG92 servo motor. This project demonstrates how easy it is to control physical devices over a local network using a built-in web server.

If you liked this project, follow more on circuit-diy.com for tutorials, projects, and practical circuits for makers like you!

Share this project

Discussion

Be the first to comment

More projects you might like

More in Arduino Nano 33 IoT