Additional Information

Site Information

 Loading... Please wait...

Blog

ESP32 DC Motor PWM Speed Control (Pulse Width Modulation) - The Hidden Facts

Posted by Wilfried Voss on

Lately, I have worked on two projects requiring DC motor control per PWM (Pulse Width Modulation). What both projects had in common was a profound lack of documentation regarding PWM control, specifically the side effects that have a significant impact on the programming. 

This post aims to point to these quirks that may cause considerable headaches during the software development phase. In the first case, the task was to control a Thomson Eletrak Linear Actuator per Arduino Due. Based on my experience, I have written several posts addressing the learning curve and conclusions. These posts are: 

Luckily, my experience with the linear actuator control helped me to avoid similar delays with the second project, which required controlling up to four DC motors per CAN Bus, specifically SAE J1939. Here, I chose the ESP32 processor, which represents my first choice when it comes to development and prototyping.

To mention it upfront, programming PWM-based motor control using either the Arduino series of processors or the ESP32 is ridiculously easy. Also, I will not needlessly repeat information on PWM basics and the actual programming. These topics have been documented to a great degree, and I have listed references below. I used the same references successfully for my ESP32 project. However, as I mentioned previously, there are some quirks that have not been sufficiently explained in any of the examples I found online.

The Problem: The general perception is that you can easily control a DC motor anywhere between zero and maximum speed. That is, however, not the case. 

Note: In the past, I have been involved with programming motion controllers, but I am far from being a motion control expert. Any experienced motion control engineer may cringe, given my observations and conclusions. Nevertheless, my projects work. If you feel compelled to add a comment, please feel free to contact me (constructive contribution or criticism is appreciated). 

The problem is with the minimum speed, i.e., the DC motor cannot move below a certain minimum PWM duty cycle. 

I found a few comments, addressing this effect, in the posts listed below: 

For the [PWM] frequency we’re using, when you apply duty cycles smaller than 200, the motor won’t move and will make a weird buzz sound. So, that’s why we set a duty cycle of 200 at the start. 
Reference: ESP32with DC Motor - Control Speed and Direction | Random Nerd Tutorials...

Suppose the duty is zero, motor does not run and when duty cycle is 100 % the motor moves on maximum RPM. But this concept is not always right because motor starts running after giving some fixed voltage that is called threshold voltage.
Reference: DCMotor Speed Control Using Arduino & PWM - (duino4projects.com)...

According to the Thomson Electrak user manual (obviously the most professional explanation), the "PWM driver will not provide enough voltage for the motor to operate properly under 20% utilization, therefore 20% should be considered a lower limit for speed control."

However, even this last explanation does not address the full extent of the limitations involved. There is a second speed limit, somewhat higher than the minimum speed, at which the motor can run but cannot start when this speed is applied at motion start.The effect is the above-mentioned "buzzing," meaning the motor does not move.

According to the above-referenced comments, I call this the "threshold speed" (again, correct me if necessary). In the case of the linear actuator, the threshold speed also depends on the gear's condition, i.e., older gears require a higher threshold speed due to mechanical wear.

Note: The exact definition of minimum and threshold duty cycle differs from motor to motor. In my test setup, I played with the duty cycle settings to determine the limits.

Regarding the programming, you need to consider the following:

  1. When the required duty cycle (speed) is at or below the minimum speed, you need to stop the motor.
  2. When you need to start the motor at a speed between minimum and threshold speed, you need to apply a velocity higher than threshold for a short time, then continue with the initially required speed.

Item 2 is demonstrated in the programming sample found at  DC Motor Speed Control Using Arduino & PWM - (duino4projects.com):

analogWrite(out1, 255);    // Apply max. speed
delay(10);                          // Wait for 10 msec
analogWrite(out1, 175);    // Apply desired speed

This sample refers to an Arduino processor, but the basic algorithm is the same for the ESP32.

References:


Electronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled DevicesElectronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled Devices

Copperhill Technologies highly recommends using this book for your wireless application projects. Yes, many good books and free online resources are available these days, but this is the book we are using. It made our approach to Bluetooth, BLE, and WIFI a breeze. Programming wireless applications without hassles was fun, and we will share them on this web page.

Projects throughout the book utilize the wireless functionality and processing power of the ESP microcontrollers. Projects are built in the Arduino IDE, so you don't need to download other programming software. In addition, mobile apps are now ubiquitous, making the app build projects of the book very relevant, as are the web page design projects.

In Electronics Projects with the ESP8266 and ESP32, you'll see how easy and practical it is to access information over the internet, develop web pages, build mobile apps to remotely control devices with speech recognition, or incorporate Google Maps in a GPS route tracking app. More Information...

ESP32 Development Board with NMEA 2000 & NMEA 0183 HAT

The ESP32 processor, integrating peripherals such as UART, CAN Bus, WIFI, and Bluetooth, allows a wide range of applications, and the low price adds to its popularity.  There are various ESP32 development boards available in the market, such as the ESP32-WROOM-32, as shown to the left. However, compared to popular systems like the Raspberry Pi or [...]

Read More »


Extending ESP32 Functionality by Using Raspberry Pi HATs

The ESP32 processor, integrating peripherals such as UART, CAN Bus, WIFI, and Bluetooth, allows a wide range of applications, and the low price adds to its popularity.  There are various ESP32 development boards available in the market, such as the ESP32-WROOM-32, as shown to the left. However, compared to popular systems like the Raspberry Pi or [...]

Read More »


ESP32 Triple CAN Bus Application Through Adding Two MCP2515 Ports

The first question that may arise when talking about accessing the MCP2515 CAN Bus controller per ESP32 may be, "Why would you need an MCP2515 controller when the ESP32 comes with an internal CAN port?" Yes, I found this question in one of the online forums while researching this particular topic. The answer is easy: [...]

Read More »


ESP32 Project: USB to Bluetooth Gateway

The ESP32 processor allows the integration of Wi-Fi, Bluetooth, and Bluetooth LE (BLE) for a wide range of IoT (Internet of Things) applications. Using Wi-Fi ensures connectivity within a large radius. At the same time, Bluetooth allows the user to easily detect (with low-energy beacons) a module and connect it to an Android/iOS smartphone or [...]

Read More »


ESP32 Based CAN Bus, SAE J1939 and NMEA 2000 to Bluetooth Gateway

The following is a follow-up on our post  Controller Area Network (CAN), SAE J1939, NMEA 2000 Wireless Module for IoT and ELD Applications. The CAN Bus, SAE J1939 and NMEA 2000 to Bluetooth Gateway is the first product of a new series of wireless gateways for IoT (Internet of Things), ELD (Electronic Logging Devices), and other applications. At the [...]

Read More »


A New Generation of SAE J1939 Gateway, ECU Simulator, and Starter Kit

Like many other businesses, we have to deal with the global shortage of electronic components. For instance, the NXP LPC 17xx processors we used for our SAE J1939 gateways and the starter kit are not available at this time. Even worse, there is no reliable information if/when production resumes. This situation forced us to rethink [...]

Read More »


ESP32 Starter and Development Kits for Building Bluetooth and WiFi Enabled IoT Devices

The integration of Wi-Fi, Bluetooth, and Bluetooth LE allow a wide range of applications with ESP32 modules. Using Wi-Fi ensures connectivity within a large radius, while using Bluetooth allows the user to easily detect (with low-energy beacons) a module and connect it to a smartphone. The chips add priceless functionality and versatility to your applications [...]

Read More »


SAE J1939 Starter Kit and Network Simulator - New Hardware Variant Using the ESP32 Processor

Our SAE 1939 Starter Kit And Network Simulators allow the experienced engineer and the beginner to experiment with SAE J1939 data communication without connecting to a real-world J1939 network, i.e., a diesel engine. To establish a network, you need at least two nodes. That fact applies, especially to CAN/J1939 nodes. A single CAN Bus controller [...]

Read More »


CAN Bus Development with ESP32-WROOM32 Development Board

This post will demonstrate how to add a CAN Bus port to the ESP32-WROOM32 development board, i.e., regarding hardware and software. Download this post as PDF... As a matter of fact, we already offer a hardware utilizing the ESP32 processor and an onboard CAN Bus transceiver as shown in the image to the left. The ESP32 WiFi, Bluetooth [...]

Read More »