Skip to main content

Build. Learn.
Innovate.

Controlling a DC Motor with a Motor Driver

Written By:

Published on:

Project Overview

This project introduces students to controlling a DC motor using an L298N motor driver module and an ESP32 microcontroller. The project will help students understand how microcontrollers interface with higher-power components using drivers, and how PWM signals affect motor speed.

Educational Goals

  • Learn how to control motor direction and speed using GPIO and PWM
  • Understand the role of a motor driver and H-bridge
  • Write MicroPython code to drive and reverse motor rotation
  • Explore real-world applications such as robotic wheels and conveyor belts

Detailed Parts List

  • 1x ESP32 Dev Board (36-pin)
  • 1x L298N Motor Driver Module
  • 1x DC Motor (6V–12V)
  • External Power Supply (e.g., 9V battery or 12V adapter)
  • Jumper wires
  • Breadboard or motor mount base

Circuit Diagram Description

  • IN1 and IN2 connected to ESP32 GPIO pins to control motor direction
  • ENA connected to ESP32 PWM pin to control speed
  • Motor connected to OUT1 and OUT2 on L298N
  • External power supply connected to L298N power terminals

ESP32 GPIO26 —> IN1 (L298N)
ESP32 GPIO25 —> IN2 (L298N)
ESP32 GPIO14 —> ENA (L298N, PWM control)
OUT1 & OUT2 —> DC Motor
L298N GND —> ESP32 GND
L298N 12V —> External Power Source

Software Functionality

  • Send HIGH/LOW signals to IN1 and IN2 for direction
  • Generate PWM signal on ENA pin to control speed
  • Allow for stop, forward, and reverse control

Web Interface Features (Optional Extension)

  • Buttons to move motor forward, reverse, stop
  • Slider to adjust speed in real time
  • Dashboard-style controls for visual feedback

Implementation Steps

  1. Connect the motor driver and motor to ESP32 as per circuit diagram
  2. Flash MicroPython firmware to ESP32
  3. Upload “** to ESP32**
  4. Test motor directions and adjust speed
  5. (Optional) Add a web interface for remote control

Extensions and Challenges

  • Use an IR sensor or push button to trigger motor
  • Control two motors for differential drive robot
  • Automate direction changes based on distance sensor
  • Visualize speed with a simple bar graph using OLED or web dashboard

Troubleshooting Guide

  • Motor not spinning: Check power connections and motor terminals
  • Motor only moves in one direction: Check IN1 and IN2 logic levels
  • PWM has no effect: Confirm PWM pin assignment and duty cycle
  • ESP32 resets: Ensure shared GND and proper power supply to motor

Project Code


from machine import Pin, PWM
from time import sleep

# Define pins
in1 = Pin(26, Pin.OUT)
in2 = Pin(25, Pin.OUT)
pwm = PWM(Pin(14), freq=1000)
pwm.duty(0)  # Start with 0% speed

# Move forward
def forward(speed=512):
in1.on()
in2.off()
pwm.duty(speed)

# Move reverse
def reverse(speed=512):
in1.off()
in2.on()
pwm.duty(speed)

# Stop motor
def stop():
in1.off()
in2.off()
pwm.duty(0)

# Main loop
while True:
print("Forward")
forward(800)  # speed range: 0–1023
sleep(2)

print("Reverse")
reverse(800)
sleep(2)

print("Stop")
stop()
sleep(1)

STEM Benefits

  • Science: Explore the concept of electrical energy transforming into mechanical motion.
  • Technology: Understand how motor drivers serve as interfaces between microcontrollers and motors.
  • Engineering: Design and control electromechanical systems.
  • Math: Calculate duty cycles and understand the relationship between voltage, speed, and timing.

Project Code

Projects
ShowCase

Real-World Projects
with Code & Hardware

Insights, Ideas
& How-Tos

Help, Support, and
Common Questions

What types of projects can I find on your website?

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.