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

ESP32-Controlled Robot with Environmental Sensing and Servo Actuation

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled Robot with Environmental Sensing and Servo Actuation

Circuit Documentation

Summary

This circuit is designed to control a Bluetooth-operated robot using an ESP32 microcontroller. The robot's movement is facilitated by two DC motors driven by an L298N motor driver. The circuit includes MQ2 and MQ135 gas sensors for environmental monitoring, a DHT22 sensor for temperature and humidity readings, and two Tower Pro SG90 servos to articulate the robot's head. The ESP32 microcontroller is programmed to interpret Bluetooth commands to control the robot's movements and to read sensor data, which is then transmitted over the serial connection.

Component List

Microcontrollers

Sensors

Actuators

Motor Driver

Power Management

Wiring Details

ESP32 38 PINS

ESP32 - CAM

MQ135 and MQ2 Gas Sensors

DHT22 Temperature and Humidity Sensor

Tower Pro SG90 Servos

L298N DC Motor Driver

XL6009E1 Boost Converter

DC Motors

Battery

Documented Code

/*
 * This Arduino Sketch is for a Bluetooth-controlled robot using ESP32.
 * The robot can move using two DC motors controlled by an L298N driver.
 * It has MQ2 and MQ135 gas sensors, a DHT22 temperature and humidity sensor,
 * and two servo motors to move the robot's head.
 */

#include <DHT.h>
#include <Servo.h>

// Motor pins
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int enable1Pin = 14;

// Servo pins
int servo1Pin = 19;
int servo2Pin = 21;

// Sensor pins
int mq2Pin = 5;
int mq135Pin = 4;
int dhtPin = 18;

// DHT22 setup
#define DHTTYPE DHT22
DHT dht(dhtPin, DHTTYPE);

// Servo setup
Servo servo1;
Servo servo2;

// PWM setup
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 170;

void setup() {
  // Motor setup
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  ledcSetup(pwmChannel, freq, resolution);
  ledcAttachPin(enable1Pin, pwmChannel);
  ledcWrite(pwmChannel, dutyCycle);

  // Servo setup
  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);

  // Sensor setup
  pinMode(mq2Pin, INPUT);
  pinMode(mq135Pin, INPUT);
  dht.begin();

  // Initialize Serial for Bluetooth communication
  Serial.begin(115200);
}

void loop() {
  // Read sensor values
  int mq2Value = digitalRead(mq2Pin);
  int mq135Value = digitalRead(mq135Pin);
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // Print sensor values to Serial
  Serial.print("MQ2: ");
  Serial.print(mq2Value);
  Serial.print(" MQ135: ");
  Serial.print(mq135Value);
  Serial.print(" Humidity: ");
  Serial.print(h);
  Serial.print("% Temperature: ");
  Serial.print(t);
  Serial.println("*C");

  // Check for Bluetooth commands
  if (Serial.available() > 0) {
    char command = Serial.read();
    handleCommand(command);
  }

  delay(100);
}

void handleCommand(char command) {
  switch (command) {
    case 'F': // Move forward
      digitalWrite(motor1Pin1, HIGH);
      digitalWrite(motor1Pin2, LOW);
      break;
    case 'B': // Move backward
      digitalWrite(motor1Pin1, LOW);
      digitalWrite(motor1Pin2, HIGH);
      break;
    case 'L': // Turn left
      servo1.write(45);
      break;
    case 'R': // Turn right
      servo1.write(135);
      break;
    case 'S': // Stop
      digitalWrite(motor1Pin1, LOW);
      digitalWrite(motor1Pin2, LOW);
      break;
    case 'U': // Move head up
      servo2.write(45);
      break;
    case 'D': // Move head down
      servo2.write(135);
      break;
    default:
      break;
  }
}

This code is responsible for initializing and controlling the various components of the robot. It sets up the motor and servo pins, initializes the sensors, and enters a loop where it reads sensor values and listens for Bluetooth commands to control the robot's movements and head position.