Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Controlled Servo and Relay Door Lock System

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled Servo and Relay Door Lock System

Circuit Documentation

Summary

This circuit integrates a servo motor, a single-channel 5V 10A relay, and an ESP32 Devkit V1 microcontroller. The ESP32 controls the servo motor and the relay based on serial input commands. The servo can rotate to specific angles, and the relay can switch a connected load on or off. This setup could be used for applications such as remote-controlled locking mechanisms or automation systems where both position control and power switching are required.

Component List

Servo Motor

ESP32 Devkit V1

1-Channel Relay (5V 10A)

Wiring Details

Servo Motor

ESP32 Devkit V1

1-Channel Relay (5V 10A)

Documented Code

//Sun 21 sept 2024 810pm
//ESP32 with servo and relay
#include <ESP32Servo.h>
int choice = 0;
int incomingByte = 0; // for incoming serial data

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int servoPin = 23;
int pos = 0;    // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33

// ESP32 pin GPIO16, which connects to the solenoid lock via the relay
#define RELAY_PIN 2

void setup() {
  // initialize digital pin as an output.
  pinMode(RELAY_PIN, OUTPUT);

  // Allow allocation of all timers
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  myservo.setPeriodHertz(50);    // standard 50 hz servo
  myservo.attach(servoPin, 1000, 2000); 
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  myservo.write(0);   // set servo to 0 degree

  Serial.println("Key in 0 - to rotate 90 degree, 1 to rotate 180 degree");
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    choice = incomingByte - '0'; // convert from decimal to integer value 1 = 49, 2 = 50

    // say what you got:
    Serial.print("I received: ");
    Serial.print(choice);

    if (choice == 0) {
      Serial.println(" 90 degree");
      for (pos = 0; pos <= 90; pos += 1) {
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15 ms for the servo to reach the position
      }
      digitalWrite(RELAY_PIN, HIGH); // unlock the door
      delay(5000); // open for 5 seconds
      Serial.println(" UNLOCK");

      for (pos = 90; pos >= 0; pos -= 1) {
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15 ms for the servo to reach the position
      }

      digitalWrite(RELAY_PIN, LOW);  // lock the door
      Serial.println(" LOCK");

    } else if (choice == 1) {
      Serial.println(" 180 degree");
      for (pos = 0; pos <= 180; pos += 1) {
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15 ms for the servo to reach the position
      }
      digitalWrite(RELAY_PIN, HIGH); // unlock the door
      delay(5000); // open for 5 seconds
      Serial.println(" UNLOCK");
      for (pos = 180; pos >= 0; pos -= 1) {
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15 ms for the servo to reach the position
      }

      digitalWrite(RELAY_PIN, LOW);  // lock the door
      Serial.println(" LOCK");
    }
  }
}

This code is designed to run on an ESP32 microcontroller. It initializes a servo motor and a relay, and listens for serial input to control the servo's position and the relay's state. The servo can be set to rotate to either 90 or 180 degrees, and the relay can be toggled to simulate a locking mechanism.