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

ESP32 and L298N Motor Driver Controlled DC and Stepper Motors with Toggle Switch and Battery Power

View and Edit Project In Cirkit DesignerImage of ESP32 and L298N Motor Driver Controlled DC and Stepper Motors with Toggle Switch and Battery Power

Circuit Documentation

Summary

This circuit involves controlling two DC motors and a stepper motor using an ESP32 microcontroller. The L298N motor driver is used to drive the DC motors, while the ULN2003A breakout board is used to control the stepper motor. The circuit is powered by two 9V batteries, and toggle switches are used to control the power supply to the motors.

Component List

  1. DC Motor

    • Description: A standard DC motor.
    • Pins: pin 1, pin 2
  2. L298N DC Motor Driver

    • Description: A dual H-Bridge motor driver that allows control of two DC motors.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  3. 9V Battery

    • Description: A 9V battery used to power the circuit.
    • Pins: -, +
  4. Toggle Switch

    • Description: A switch used to control the power supply.
    • Pins: L1, COM, L2
  5. ESP32 (30 pin)

    • Description: A microcontroller with Wi-Fi and Bluetooth capabilities.
    • Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
  6. 28BYJ-48 Stepper Motor

    • Description: A 5V stepper motor.
    • Pins: BLUE, PINK, YELLOW, ORANGE, RED
  7. ULN2003A Breakout Board

    • Description: A driver board for the 28BYJ-48 stepper motor.
    • Pins: In 1, In 2, In 3, In 4, In 5, In 6, In 7, 0V, +5V, ON/OFF jumper switch, BLUE wire, PINK wire, YELLOW wire, ORANGE wire, RED wire

Wiring Details

DC Motor 1

DC Motor 2

L298N DC Motor Driver

9V Battery 1

9V Battery 2

Toggle Switch 1

Toggle Switch 2

ESP32 (30 pin)

28BYJ-48 Stepper Motor

ULN2003A Breakout Board

Documented Code

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

// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

void setup() {
  // sets the pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  
  // configure LEDC PWM
  ledcAttachChannel(enable1Pin, freq, resolution, pwmChannel);

  Serial.begin(115200);

  // testing
  Serial.print("Testing DC Motor...");
}

void loop() {
  // Move the DC motor forward at maximum speed
  Serial.println("Moving Forward");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor backwards at maximum speed
  Serial.println("Moving Backwards");
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor forward with increasing speed
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  while (dutyCycle <= 255){
    ledcWrite(enable1Pin, dutyCycle);   
    Serial.print("Forward with duty cycle: ");
    Serial.println(dutyCycle);
    dutyCycle = dutyCycle + 5;
    delay(500);
  }
  dutyCycle = 200;
}

This code is designed to control a DC motor connected to the ESP32 microcontroller. It sets up the necessary pins and PWM properties, and then runs a loop to move the motor forward and backward at different speeds.