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

ESP32-Based Wi-Fi Controlled Servo Gate System with Pushbutton Activation

View and Edit Project In Cirkit DesignerImage of ESP32-Based Wi-Fi Controlled Servo Gate System with Pushbutton Activation

Circuit Documentation

Summary

This circuit is designed to control multiple servos and solid-state relays (SSRs) using an ESP32 microcontroller. The circuit includes five servos, five pushbuttons, two SSRs, and a 12V adapter. The ESP32 is programmed to read the state of the pushbuttons and control the servos and SSRs accordingly.

Component List

  1. ESP32 - 38 pins Expansion Board

    • Description: A microcontroller board with 38 pins, used for controlling various components in the circuit.
    • Pins: EN, SVP, SVN, P34, P35, P32, P33, P25, P26, P27, P14, P12, P13, SD2, SD3, CMD, 5V, P23, P22, TX, RX, P19, P18, P5, P17, P16, P4, P0, P2, P15, SD1, SD0, CLK, VCC, G, D22, D21, 3V, V, +, -
  2. Servo (5 units)

    • Description: A servo motor used for precise control of angular position.
    • Pins: GND, VCC, PWM
  3. Pushbutton (5 units)

    • Description: A simple pushbutton used for user input.
    • Pins: Pin 3 (out), Pin 4 (out), Pin 1 (in), Pin 2 (in)
  4. SSR-40A (2 units)

    • Description: A solid-state relay used for switching high-power devices.
    • Pins: -, +, Lin, Lout
  5. 12V Adapter

    • Description: A power adapter providing 12V DC.
    • Pins: VCC, GND

Wiring Details

ESP32 - 38 pins Expansion Board

Servo

Pushbutton

SSR-40A

12V Adapter

Code Documentation

#include <ESP32Servo.h>

#define NUM_GATES 5
#define MOTOR1_RELAY_PIN 12
#define MOTOR2_RELAY_PIN 14
#define MOTOR_DELAY 5000
#define SERVO_DELAY 10000

ESP32Servo servos[NUM_GATES];
int gatePins[NUM_GATES] = {25, 26, 27, 32, 33}; // Pins for the servos on the ESP32
int contactPins[NUM_GATES] = {16, 17, 18, 19, 23}; // Pins for the input contacts on the ESP32
bool gateStates[NUM_GATES] = {false, false, false, false, false};
int motor1State = LOW;
int motor2State = LOW;
int motorOrder = 0;

void setup() {
  for (int i = 0; i < NUM_GATES; i++) {
    servos[i].attach(gatePins[i]);
    pinMode(contactPins[i], INPUT_PULLUP);
  }
  pinMode(MOTOR1_RELAY_PIN, OUTPUT);
  pinMode(MOTOR2_RELAY_PIN, OUTPUT);
  digitalWrite(MOTOR1_RELAY_PIN, motor1State);
  digitalWrite(MOTOR2_RELAY_PIN, motor2State);
}

void loop() {
  int activeGates = 0;
  for (int i = 0; i < NUM_GATES; i++) {
    bool contactState = digitalRead(contactPins[i]) == LOW;
    if (contactState && !gateStates[i]) {
      gateStates[i] = true;
      servos[i].write(90); // Open servo
      delay(SERVO_DELAY);
    } else if (!contactState && gateStates[i]) {
      gateStates[i] = false;
      delay(SERVO_DELAY);
      servos[i].write(0); // Close servo
    }
    if (gateStates[i]) {
      activeGates++;
    }
  }

  if (activeGates > 0) {
    if (motor1State == LOW) {
      motor1State = HIGH;
      digitalWrite(MOTOR1_RELAY_PIN, motor1State);
    }
    if (activeGates > 1 && motor2State == LOW) {
      motor2State = HIGH;
      digitalWrite(MOTOR2_RELAY_PIN, motor2State);
    } else if (activeGates == 1 && motor2State == HIGH) {
      motor2State = LOW;
      digitalWrite(MOTOR2_RELAY_PIN, motor2State);
    }
  } else {
    if (motor1State == HIGH) {
      delay(MOTOR_DELAY);
      motor1State = LOW;
      digitalWrite(MOTOR1_RELAY_PIN, motor1State);
    }
    if (motor2State == HIGH) {
      motor2State = LOW;
      digitalWrite(MOTOR2_RELAY_PIN, motor2State);
    }
  }
}

void changeMotorOrder() {
  motorOrder = (motorOrder + 1) % 2;
}

This code is designed to control five servos and two SSRs using an ESP32 microcontroller. The servos are connected to specific pins on the ESP32, and their states are controlled based on the input from pushbuttons. The SSRs are used to control motors, which are activated based on the state of the servos. The code includes a setup function to initialize the pins and a loop function to continuously check the state of the pushbuttons and control the servos and SSRs accordingly.