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

ESP32-Controlled Self-Balancing Robot with MPU-6050 and L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled Self-Balancing Robot with MPU-6050 and L298N Motor Driver

Circuit Documentation

Summary

The circuit in question is designed to control a two-wheeled self-balancing robot. It utilizes an ESP32 microcontroller to read tilt angles from an MPU-6050 sensor and adjust the speed of two DC motors via an L298N motor driver to maintain balance. The circuit also includes a 2x 18650 battery pack for power, a piezo buzzer for audio feedback, various resistors for current limiting and pull-up/pull-down functions, a potentiometer for adjustable parameters, an NPN transistor for switching, a rocker switch for power control, LEDs for visual feedback, an LCD display for user interface, an HC-SR04 ultrasonic sensor for distance measurement, a WS2815 LED strip for lighting effects, an NTC thermistor for temperature sensing, and a 74HC595 shift register for expanding output pins. A 7805 voltage regulator is used to provide a stable 5V supply to certain components.

Component List

Active Components

Passive Components

Electromechanical Components

Wiring Details

ESP32 - 38 pins

L298N DC motor driver

MPU-6050

LCD Display 20x4 I2C

Piezo Buzzer

NPN-Transistor

74HC595

HC-SR04 Ultrasonic Sensor

WS2815 LED Strip

NTC (Thermistor)

2x 18650 Battery Pack

DC Motor

Rocker Switch

Resistor (1k Ohms, 200 Ohms, 10k Ohms)

Potentiometer

LED: Two Pin (red)

7805 Voltage Regulator

Documented Code

ESP32 Microcontroller Code (Instance 1)

/*
 * Arduino Sketch for a Two-Wheeled Self-Balancing Robot
 *
 * This code uses an MPU-6050 to detect the tilt angle of the robot and controls
 * two DC motors via an L298N motor driver to keep the robot upright. The ESP32
 * microcontroller reads the angle from the MPU-6050 and adjusts the motor speeds
 * accordingly to balance the robot.
 */

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

// Motor driver pins
const int ENA = 12;
const int ENB = 14;
const int IN1 = 26;
const int IN2 = 25;
const int IN3 = 33;
const int IN4 = 32;

// PID constants
float Kp = 2.0;
float Ki = 5.0;
float Kd = 1.0;

float setPoint = 0.0;
float input, output;
float errSum, lastErr;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) {
    Serial.println("MPU6050 connection failed");
    while (1);
  }
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

void loop() {
  int16_t ax, ay, az;
  int16_t gx, gy, gz;
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  float angle = atan2(ay, az) * 180 / PI;
  input = angle;
  float error = setPoint - input;
  errSum += (error * Ki);
  float dErr = (error - lastErr) * Kd;
  output = Kp * error + errSum + dErr;
  lastErr = error;
  setMotorSpeed(output);
  delay(10