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

ESP32-Controlled Dual DC Motor Driver Circuit with L298N

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled Dual DC Motor Driver Circuit with L298N

Circuit Documentation

Summary

This circuit is designed to control two DC motors using an ESP32 microcontroller and an L298N DC motor driver. The ESP32 microcontroller is programmed to gradually increase and decrease the speed of the motors in a loop. The motors are powered by a 7.4V power source, and the L298N motor driver interfaces between the ESP32 and the motors to control their direction and speed.

Component List

ESP32 Microcontroller

L298N DC Motor Driver

DC Mini Metal Gear Motor (x2)

7.4V Power Source

ESP32-C3-DevKitM-1

Wiring Details

ESP32 Microcontroller

L298N DC Motor Driver

DC Mini Metal Gear Motor (Motor 1)

DC Mini Metal Gear Motor (Motor 2)

7.4V Power Source

Documented Code

/*
 * This Arduino Sketch controls two DC motors connected to an L298N motor driver
 * using an ESP32 microcontroller. The motors will be slowly turned on and off
 * in a loop.
 */

// Define motor control pins
const int motor1Pin1 = 25;
const int motor1Pin2 = 26;
const int motor2Pin1 = 27;
const int motor2Pin2 = 14;
const int enable1Pin = 12;
const int enable2Pin = 13;

void setup() {
  // Set all the motor control pins to outputs
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  pinMode(enable2Pin, OUTPUT);

  // Initialize motors to be off
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  analogWrite(enable1Pin, 0);
  analogWrite(enable2Pin, 0);
}

void loop() {
  // Slowly turn on motor 1
  for (int speed = 0; speed <= 255; speed++) {
    analogWrite(enable1Pin, speed);
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    delay(20);
  }

  // Slowly turn off motor 1
  for (int speed = 255; speed >= 0; speed--) {
    analogWrite(enable1Pin, speed);
    delay(20);
  }

  // Slowly turn on motor 2
  for (int speed = 0; speed <= 255; speed++) {
    analogWrite(enable2Pin, speed);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
    delay(20);
  }

  // Slowly turn off motor 2
  for (int speed = 255; speed >= 0; speed--) {
    analogWrite(enable2Pin, speed);
    delay(20);
  }
}

This code is designed to run on the ESP32 microcontroller and controls the speed and direction of two DC motors via the L298N motor driver. The setup() function initializes the motor control pins and sets the motors to be off. The loop() function contains two for-loops that gradually increase and decrease the PWM signal to the enable pins, which controls the speed of the motors. The motors are set to move in one direction by setting one control pin to HIGH and the other to LOW.