ESP32 Motor Control with PWM

Motor control is a critical feature in many electronic and mechanical systems, from robotics to ventilation systems. With the ESP32 microcontroller, controlling motor speed and direction is not only efficient but also flexible, thanks to its built-in Pulse Width Modulation (PWM) capabilities. In this tutorial, we’ll explore how to implement ESP32 Motor Control using the DRV8871 DC motor driver with MicroPython, including direction control.


Table of Contents


How PWM Works for ESP32 Motor Control

Pulse Width Modulation (PWM) is a technique where the width of a pulse is varied while keeping its frequency constant. The average power delivered to the motor is proportional to the duty cycle of the PWM signal, which determines its speed. By adjusting the duty cycle from 0% (motor off) to 100% (motor at full speed), you can achieve smooth and efficient motor control.

The ESP32’s PWM functionality is implemented through its LEDC (LED Control) module, which supports multiple channels and adjustable frequencies, making it ideal for motor control applications.

Image of the DRV8871 motor driver module showing labeled pins, including VM, GND, OUT1, OUT2, IN1, IN2, and PWM input.
Close-up of the DRV8871 motor driver module with labeled pins for easy reference.

Materials Required

  • ESP32 microcontroller
  • 12V DC motor
  • DRV8871 motor driver module
  • External power supply (12V)
  • Breadboard and jumper wires

Circuit Diagram

To control the motor speed and direction, the ESP32 generates PWM signals and logic-level signals, which are sent to the DRV8871 motor driver. The motor driver regulates the power delivered to the motor and handles direction control based on the input signals.

DRV8871 Motor Driver Specifications:

  • 6.5V to 45V motor power voltage
  • Up to 5.5V logic level on IN pins
  • 565mΩ Typical RDS(on) (high + low)
  • 3.6A peak current
  • PWM control
  • Current limiting/regulation without an inline sense resistor
  • Undervoltage lockout
  • Overcurrent protection
  • Thermal shutdown

Datasheet: Adafruit DRV8871 Brushed DC Motor Driver Breakout

Connections:

  • Connect the motor’s positive and negative terminals to the motor driver’s output terminals (OUT1 and OUT2).
  • VM Not connected (controller is powered by the motor power source)
  • Connect the motor driver’s GND pin to the ESP32’s GND.
  • Connect the motor driver’s IN1 pin to an ESP32 GPIO pin (e.g., GPIO 18) for direction control.
  • Connect the motor driver’s IN2 pin to another ESP32 GPIO pin (e.g., GPIO 19) for direction control.

Writing the MicroPython Code

Below is the sample MicroPython code for controlling the motor speed and direction:

from machine import Pin, PWM
from utime import sleep

in2 = PWM(Pin(18))     # Speed
in1 = Pin(19, Pin.OUT)  # Direction

in2.freq(500)         # set frequency. 1000 is default

while True:
    # Forward
    # in2:  1023 <-- Slower - Faster --> 0
    in1.on()
    print("Forward")
    in2.duty(0)  # Fast
    sleep(1)
    in2.duty(100)  # Slow
    sleep(1)
    in2.duty(200)  # Slower
    sleep(1)
    in2.duty(300)  # Slower x 2
    sleep(1)
    in2.duty(1023)  # stop
    sleep(1)

    # Reverse
    # in2:  0 <-- Slower - Faster --> 1023
    in1.off()
    print("Reverse")    
    in2.duty(900)  # Fast
    sleep(1)
    in2.duty(600)    # Slow
    sleep(1)
    in2.duty(300)    # Slower
    sleep(1)
    in2.duty(0)     # Stopped
    sleep(1)

Conclusion

By utilizing the ESP32’s PWM capabilities and the DRV8871 motor driver, you can efficiently control both the speed and direction of a DC motor for various applications. This project demonstrates the flexibility and power of MicroPython for motor control tasks. Experiment with different frequencies, duty cycles, and directions to fine-tune the motor’s performance for your specific needs.

Leave a Comment