Skip to main content

Build. Learn.
Innovate.

Controlling a DC Motor with a Motor Driver

Written By:

Published on:

Project Overview

This project introduces controlling the speed and direction of a DC motor using an L298N motor driver. Students will learn about motor drivers and Pulse Width Modulation (PWM).

Educational Goals

  • Understand how motor drivers are used to control motors.
  • Learn about DC motor operation.
  • Introduce Pulse Width Modulation (PWM) for speed control.
  • Control motor direction using digital signals.

Detailed Parts List

  • ESP32 – 1
  • L298N Motor Driver – 1
  • DC Motor 1.5-3V – 1 (or TT Motor)
  • External Power Supply for Motor (appropriate voltage for the motor, e.g., 3V or 5V) – 1
  • Breadboard – 1
  • Jumper Wires – Male-to-Male

Circuit Diagram Description

Connect the L298N motor driver to the ESP32. Connect the VCC and GND pins of the L298N logic power to a 5V and GND pin on the ESP32. Connect the external motor power supply to the motor power terminals on the L298N (usually labeled +12V and GND, but connect your motor voltage and GND). Connect the two terminals of the DC motor to one of the motor output channels on the L298N (e.g., OUT1 and OUT2). Connect the IN1 and IN2 (or IN3 and IN4 for the second channel) control pins of the L298N to digital GPIO pins on the ESP32. Connect the Enable pin (ENA or ENB) for the chosen motor channel to a PWM-capable GPIO pin on the ESP32.

Software Functionality

The MicroPython code will configure the IN pins as digital outputs to control the motor direction (high/low combinations for forward/reverse). The PWM-capable pin connected to the Enable pin will be configured for PWM output to control the motor speed by varying the duty cycle. The code will demonstrate setting the motor to run forward at a certain speed, then reverse at a different speed, and stopping the motor.

Web Interface Features

A web interface could provide buttons to control the motor direction (Forward, Reverse, Stop) and a slider or input field to adjust the motor speed.

Implementation Steps

  • Set up the MicroPython environment on the ESP32.
  • Connect the L298N motor driver, DC motor, and external power supply as described.
  • Write the MicroPython code to control the motor using the L298N.
  • Upload and run the code on the ESP32.
  • Observe the motor’s speed and direction changing.

Extensions and Challenges

  • Control two DC motors for a simple robot base.
  • Combine with the ultrasonic sensor for obstacle avoidance.
  • Use a potentiometer to control the motor speed.
  • Implement acceleration and deceleration.

Troubleshooting Guide

  • Motor not spinning: Check the motor power supply is connected correctly to the L298N and providing sufficient voltage for the motor. Ensure the logic power to the L298N is connected (5V). Verify the motor is connected to the correct output terminals. Check the control signals (IN and Enable) from the ESP32 are connected to the correct pins on the L298N and the code is setting them correctly.
  • Motor spinning only in one direction: Verify the IN pins are connected correctly and the code is switching between the appropriate high/low combinations.
  • Motor running at full speed only: Ensure the Enable pin is connected to a PWM-capable pin and the code is generating a PWM signal with a varying duty cycle.

Project Code

# This script demonstrates controlling the speed and direction of a DC motor
# using an L298N motor driver and the ESP32's PWM capabilities.

from machine import Pin, PWM
import time

# Define the GPIO pin numbers connected to the L298N motor driver.
# Replace these with the actual GPIO pins you are using.
# IN1 and IN2 control the direction of Motor A.
in1_pin = 17
in2_pin = 16
# ENA controls the speed of Motor A using PWM.
ena_pin = 18 # Must be a PWM-capable pin

# Create Pin objects for the direction control pins.
in1 = Pin(in1_pin, Pin.OUT)
in2 = Pin(in2_pin, Pin.OUT)

# Create a PWM object for the enable pin to control speed.
# Set the frequency for the PWM signal (e.g., 1000 Hz).
pwm_ena = PWM(Pin(ena_pin), freq=1000)

# Function to set the motor speed.
# speed should be a value between 0 and 1023 (for 10-bit PWM resolution, common on ESP32).
# 0 is stopped, 1023 is maximum speed.
def set_motor_speed(speed):
    # Ensure speed is within the valid range.
    speed = max(0, min(1023, speed))
    # Set the PWM duty cycle.
    pwm_ena.duty(speed)

# Function to set the motor direction.
# direction can be "forward", "reverse", or "stop".
def set_motor_direction(direction):
    if direction == "forward":
        in1.value(1) # Set IN1 high
        in2.value(0) # Set IN2 low
        print("Motor Direction: Forward")
    elif direction == "reverse":
        in1.value(0) # Set IN1 low
        in2.value(1) # Set IN2 high
        print("Motor Direction: Reverse")
    else: # direction == "stop" or any other value
        in1.value(0) # Set IN1 low
        in2.value(0) # Set IN2 low (brakes the motor)
        print("Motor Direction: Stop")
        # Alternatively, you could set speed to 0 to stop without braking:
        # set_motor_speed(0)

print("Starting DC motor control script...")

# Example usage:
# Run motor forward at medium speed.
set_motor_direction("forward")
set_motor_speed(512) # Medium speed (approx 50% duty cycle)
time.sleep(3) # Run for 3 seconds

# Stop the motor.
set_motor_direction("stop")
set_motor_speed(0) # Set speed to 0 explicitly to stop PWM output
time.sleep(2) # Wait for 2 seconds

# Run motor reverse at a different speed.
set_motor_direction("reverse")
set_motor_speed(768) # Higher speed (approx 75% duty cycle)
time.sleep(3) # Run for 3 seconds

# Stop the motor.
set_motor_direction("stop")
set_motor_speed(0)
time.sleep(2)

print("Motor control demonstration finished.")

# You can add a while loop here to continuously control the motor
# based on sensor input or other logic.
# For example:
# while True:
#     distance = measure_distance() # Assuming you have the ultrasonic sensor function
#     if distance < 20:
#         set_motor_direction("stop")
#         set_motor_speed(0)
#     else:
#         set_motor_direction("forward")
#         set_motor_speed(800)
#     time.sleep(0.1)

STEM Benefits

Science:

  • Electromagnetism (basic): Understanding how electric current creates a magnetic field to produce motion in a motor.
  • Electricity and Circuits: Learning about the power requirements of motors and how a motor driver handles higher currents.

Technology:

  • Actuators: Using a DC motor to create physical movement.
  • Motor Drivers (L298N): Understanding the role of an H-bridge in controlling motor direction and the need for a driver IC to interface a microcontroller with a motor.
  • Pulse Width Modulation (PWM): Learning how varying the duty cycle of a digital signal can control the motor’s speed.

Engineering:

  • System Design: Connecting the microcontroller, motor driver, motor, and power supply.
  • Control Systems (basic): Implementing open-loop control of motor speed and direction.
  • Power Management: Understanding the need for separate power for the motor and microcontroller.

Mathematics:

  • Proportions: Understanding the relationship between the PWM duty cycle and the motor speed.
  • Ratio (if using geared motors): While not explicitly calculated in this project, the concept of gear ratios and their effect on speed and torque is relevant for the TT motors.

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.