Use ESP32 to Control PWM ESCs for BLDC Motors
by radicalroundcat in Circuits > Arduino
70 Views, 0 Favorites, 0 Comments
Use ESP32 to Control PWM ESCs for BLDC Motors
I'm building a drone with a custom flight controller based off of the ESP32-S3 chip. It also uses very common ESCs straight from AliExpress. I needed to be able to send the proper signals to such an ESC, but that proved to be more complicated than I thought it would be.
However, after reading the datasheet of the motor, using the ESC actually became very simple, and I got it to work :) So will you, after this tutorial.
I will cover:
- Which PWM values to use
- Using the ledc api, which is exclusive to the esp32 family. Don't try this with an ATmega or something..
- How to arm the ESC
- Wiring connections
Supplies
1x - ESP32-S3-DevKit (Or any esp32 variant of your choice)
1x - Brushless (40A) Electronic Speed Controller
1x - BLDC motor
1x - 2-4S LiPo battery
Some jumper wires
This instructable uses PlatformIO because I like it more than the Arduino IDE. It should be easily portable to the latter, however.
Here is a datasheet of an ESC similar to the one I'm using. Keep in mind that some specs on there may be off due to it being a random document that was pulled from the internet. However, it does provide some valuable information about how to send signals to the ESC.
Wiring
You will notice a thin orange, red, and brown wire on the ESC. Those are for Signal, 5v and GND respectively.
⚠️ DO NOT plug the red wire into the esp32. It OUTPUTS 5V and will cause a catastrophic failure, probably.
- One exception to this is if you want to power the esp32 through the 5v pin, but your USB port should not be plugged in when doing so.
Plug the signal wire into any GPIO of your choice, just make sure its safe to be used, and plug the brown wire to GND.
Connect your motor to the ESC.
Connect your esp32 to your computer.
Don't plug the LiPo battery in yet.
If you have a propeller, don't connect it, otherwise, you'll die.
Learn How the ESC Works
I always thought ESCs were dumb machines that just outputted whatever speed that was fed to them. They're actually very intelligent. They communicate by making your motor beep, and also have safety features.
My ESCs use PWM for signaling. It's in the title of the instructable.
By standards, the frequency for communication is 50Hz, or one cycle every 20milliseconds.
The throttle speeds are dictated by the pulse length of each cycle, where 1 millisecond pulses (or a 5% duty cycle) corresponds to minimum throttle, and 2 milliseconds (or a 10% duty cycle) mean maximum throttle.
This is similar to many hobby servo motors you can easily find, such as the MG90s. Having these can be handy for testing code.
Before feeding the signals to the ESC, you'll need to know about, arming them.
The ESC can be armed by being fed 1 millisecond pulses once the battery is plugged in.
Above is approximately what the signal should look like on an oscilloscope.
Programming
Create a project in the IDE of your choice. To reiterate, I'll be using PlatformIO.
If you also happen to use PlatformIO, here are my build flags:
A big part of the code will use the LEDC api, which is basically a pwm library.
We can start by defining some basic parameters
Now we have to calculate the frequency values for ledcWrite. The formula is pulse length (in microseconds) times resolution divided by cycle length.
Next, we'll arm the ESC in the setup() function
Now you can make the motor move to your heart's content in the loop function!!
Testing It
Flash the code onto the board. Once you turn the microcontroller on, you have no more than 5 seconds to plug in your battery. It should do a happy sequence of beeps and start spinning.
Always unplug the battery first, before turning off the esp32.
Enjoy your motor!