Logo

An ESP32-Powered DC Motor Control

What is a DC Motor Control System?

A DC Motor Control System allows you to regulate the speed and direction of a DC motor using electronic components like H-Bridge drivers (e.g., L298N) and microcontrollers. It is commonly used in robotics, automation, and IoT applications where mechanical motion is needed.

Working Principle of DC Motor Control

A DC motor converts electrical energy into mechanical rotation. The L298N driver uses an H-bridge circuit to change the polarity of the voltage applied to the motor, allowing control of direction. PWM (Pulse Width Modulation) signals from the ESP32 help in controlling the motor speed.

  • ESP32 sends control signals (HIGH/LOW) to IN1 and IN2 pins of L298N for direction control.
  • ESP32 sends a PWM signal to the ENA pin of the motor driver to control speed.
  • The L298N then supplies the required current to the motor to drive it in the desired direction and speed.

Formula: Speed (%) = (PWM Value / 255) * 100

Components Required

  • ESP32 development board
  • L298N Motor Driver Module
  • DC Motor (3V–12V range)
  • External power supply (e.g., 9V battery or adapter)
  • Jumper wires
  • Breadboard (optional)

Pin Configuration of L298N with ESP32

  • IN1: Motor direction control pin 1
  • IN2: Motor direction control pin 2
  • ENA: Enable pin for motor A, used for PWM speed control
  • GND: Common ground
  • VCC: External motor power supply (e.g., 9V battery)

Make sure the motor power supply is connected to the L298N '12V' terminal, not directly to ESP32, to avoid damage.

Wiring DC Motor to ESP32 via L298N

  • ENA -> GPIO 5 (PWM output)
  • IN1 -> GPIO 18
  • IN2 -> GPIO 19
  • GND -> GND
  • VCC (L298N) -> External 9V Power Supply

Arduino Code for ESP32 DC Motor Control

1#define ENA 5
2#define IN1 18
3#define IN2 19
4
5void setup() {
6  pinMode(ENA, OUTPUT);
7  pinMode(IN1, OUTPUT);
8  pinMode(IN2, OUTPUT);
9  Serial.begin(115200);
10}
11
12void loop() {
13  // Rotate motor forward
14  digitalWrite(IN1, HIGH);
15  digitalWrite(IN2, LOW);
16  analogWrite(ENA, 180); // speed control (0–255)
17  delay(3000);
18
19  // Stop motor
20  analogWrite(ENA, 0);
21  delay(1000);
22
23  // Rotate motor backward
24  digitalWrite(IN1, LOW);
25  digitalWrite(IN2, HIGH);
26  analogWrite(ENA, 150);
27  delay(3000);
28
29  // Stop motor
30  analogWrite(ENA, 0);
31  delay(1000);
32}

Code Explanation (Line-by-Line)

  • #define ENA 5: Defines GPIO 5 as the PWM pin for controlling motor speed.
  • digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);: Sets the motor to rotate in the forward direction.
  • analogWrite(ENA, 180);: Applies 70% speed to the motor using PWM.
  • delay(3000);: Keeps the motor running for 3 seconds.
  • analogWrite(ENA, 0);: Stops the motor.

Applications

  • Smart vehicle and robot movement
  • Conveyor belt automation
  • IoT-based fans and pumps
  • Automated curtain systems
  • DIY smart home automation

Conclusion

Controlling a DC motor with ESP32 using the L298N driver is a fundamental skill for embedded developers and robotics engineers. With proper wiring and simple code, both direction and speed of the motor can be controlled effectively, opening up various project possibilities in automation and IoT.