Introduction: Driving an ESC/Brushless-Motor Using Raspberry Pi
Hey there!!! In this instructable I will show you how to program an ESC and drive a brushless-motor in a desired speed using Raspberry Pi 3.
I recommend you to READ THE WHOLE INSTRUCTABLE BEFORE MAKING IT even though I'm typing load of crap here. I am making this instructable because I didn't find any tutorial for this topic, maybe because the ones who already knew how to do it were too lazy and selfish to help others. Also, make sure you are going through precautions at every step, its kinda important if you don't wanna screw up your life.
An Electronic Speed Controller or simply called ESC is an electronic circuit which is basically used to control a brushless-motor. It takes a signal from a micro-controller and convert it into three-phase output of limited voltage. So, It runs a brushless-motor by sending a sequence of output signals to it.
We can say that it is like a servo which also takes input in pulsing signals so we will use Pi's Pulse Width Modulation (PWM) system.
To use PWM in Raspberry Pi, we need to initialize PIGPIO library in daemon(which means running the process in background) which sends DMA timed pulses from Python. Then we will calibrate and arm the ESC and start giving some speed to the motor.
Just to make it clear, Arming is like telling the ESC "dude, you're good to go, start the motor now." just like you have to pull the barrel of a pistol and put the safety down before shooting. An ESC has a lot of modes but for the sake of this instructable we will not go in details but focus only in driving the motor.
Step 1: Precautions
1. Make sure you have that PIGPIO library installed, if not... you know what to do.
2.Don't be stupid like me and attach the prop adapter or the propeller to the motor, something bad can happen like really bad.
3. Do not hold the motor while its in motion, it will fly you off if you're too light or it could burn you down to ashes if you're too heavy which you probably are because brushless-motors are nasty beasts.
4. You're smart right.. I don't think I have to tell you about the result if you have bad connections. As it is said..
"Even the smallest things can trigger an avalanche."
5. Wrong battery polarity and you're done with your ESC.
6. WATCH FOR THE RED WIRE OF THE ESC, DO NOT USE IT.... consider it as your arch-nemesis.
7. Make sure powering your pi good, isn't that obvious.
Step 2: Required Items
Raspberry Pi (to control your ESC)
Brushless-Motor (.....obviously)
Electronic Speed Controller [ESC] (.....obviously)
Computer (if you are using Pi in headless mode)
Power Bank (to use Pi portably)
Li-Po Battery (.....obviously)
M-F wires (.....obviously)
Step 3: Connections
Connect the parts like the above picture. Yes! That RED WIRE OF THE ESC HAS NO FUNCTION (at least in this project). IT CAN BURN YOUR PI'S PROCESSOR, so watch out for that or get yelled by your mom !
You can connect the Motor wires however you want with the ESC i.e you can connect red wire to the third blue wire and black wire with second blue wire and so on. IF YOU WANT TO REVERSE THE ROTATION OF THE MOTOR, JUST SWAP THE RED WIRE WITH THE YELLOW WIRE.
DO NOT CONNECT THE BATTERY YET, you will do it while running the program. There's a reason behind everything, there's a reason that you are here looking at this screen, there's a reason that you're still alive.. and all that stuff.
I made these fritzing parts just for....... myself but you know "sharing is caring", download it from here :-
Step 4: Programming With Python
Download that python program below and execute it in your pi AFTER you have finished with the connections.
Watch the video for first time run, if you're confused..
If this program looks confusing to you actually I'm a pro you know but if it looks as if a noob had written this program, I'm learning ok...
When you will execute it, the program will give you five options like this :
calibrate OR manual OR control OR arm OR stop
You have to enter the option by typing the exact word. You can also switch among functions anytime.
The function of these options goes like these :-
calibrate
This function is for first time use or later if you see some throttle problems. Don't worry if you've done it again.
This option will automatically calibrate your motor's throttle, arm it and then switches to control function. The calibrate procedure goes as follows as in manual mode :-
MAX THROTTLE (2000)
Connect the battery
You will hear two beeps
Wait for a tune
MIN THROTTLE (700)
Wait for some time
STOP THROTTLE (0)
Wait for some time
MIN THROTTLE (700)
Wait for a sec
ARMED (>1000, to get motion)
manual
You can use this function if you want to control or program your ESC manually.
control
This function lets you control the speed of motor using 'a','d','q' and 'e'. This function requires arming of the ESC first.
arm
This function will let you arm your ESC automatically and will switch to control function.
The arming procedure goes as follows:-
MAX THROTTLE (2000)
Wait for a sec..
MIN THROTTLE (700)
Wait for a sec..
ARMED (>1000, to get motion)
stop
This function is to stop the ESC and the program. Suppose, you screw up something you can execute this function and stop everything.
Step 5: Go Beyond
"Once we accept our limits, we go beyond them." - Albert Einstein
Once you're done with your ESC and mastered it (kind of), its your turn to make your project out of it.
There are things I gotta say that :-
I made this instructable just for fun and with heart.
I do not intend to violate someone with my words.
I will not be responsible for any damage done to your parts.
Try to make your own program and experiment, it will give you more knowledge.
Comment below for any issues or problems you found regarding this instructable, I'm here to help.
More Instructables coming soon.... I guess?

Participated in the
Microcontroller Contest 2017
40 Comments
Question 10 months ago on Step 4
Did you change the ESC.py code from the original Instructable? The current download does not match your description nor the pinout for the Pi.
Question 10 months ago on Step 5
Hello. I'm currently making a drone with 4 brushless motors connected with a 4in1 ESC, connected to a Raspberry Pi Pico and a LiPo battery. I saw your code and find your code interesting to use for this application.
Unfortunately, the Thonny IDE throws the following traceback when trying to run the code:
"'sudo' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
File "E:\main2.py", line 6, in
import pigpio
ModuleNotFoundError: No module named 'pigpio'"
Even though pigpio is installed via the command prompt, it still throws this error. Can you help?
Question 1 year ago on Step 3
Hello!!,
I just wanted to know, in step 3, why aren't we connecting the middle(Red) wire to PI?
Answer 10 months ago
It is because the red wire will fry your processesor. It will destroy the Pi.
12 months ago
Thank you so much for sharing your fritzing parts!
Question 1 year ago
Wow great~! After followed this article, my unknown BL DC Motor works~!!! So excited~!
Thank you so much for posting it here~!
Just one question~!
How to brake the wheel? How to reverse the turning without re-connect the wires, just by code?
Question 2 years ago on Step 5
The project works great with my 1000kv bldc and 30 amp ESC but isnt it better to use one of the hardware pwm outputs that the pi has in gpio 12 and 13? I noticed that the max value can be set at 2500 maximum but if you use hardware pwm you can go as far as 4000.
Question 2 years ago
Raspberry Pi directly connect to ESC without any drone controller.It possible..?
Or else any other option RPi interface control for drone.
Anyone know please reply..
2 years ago
Thanks man, you are a lifesaver.
And by the way, I love your attitude.
5 years ago
I am wanting to run my ESC in reverse. Do you know how to do that?
Reply 2 years ago
Switch the red(positive term) and yellow(signal) wire barrels connections from the ESC to the motor.
3 years ago
If the code has been corrected where is it? I’m experiencing the same code problems
3 years ago
Hi. Great tutorial of (as you said) an strangely uncovered and contradictory topic. I wonder if this approach could work for a AC Single Phase with Capacitor Motor. I need to do speed control of this fan/engine. Any hints or components that can be suggested will be much appreciated. The engine that I need to speed control is this one:
https://marselsogutma.com/wp-content/uploads/2019/05/4E-350S%E5%B8%A6%E9%A3%8E%E9%87%8F%E5%9B%BE%E8%A7%84%E6%A0%BC%E4%B9%A6.pdf
3 years ago
Whenever I try to run the calibrate it always says <function calibrate at 0x754c8870> . I've checked to see if my power supply is working, I've made sure I have a voltage that goes to the ESC and the Motor that I'm using, I just can't get anything but that in python. Anyone have any tips on what to do?
(pigpio library is installed aswell. If you have any responses email me noe.fern145@gmail.com)
3 years ago
awsome tutorial
3 years ago
perfect, you explained that bit of BL motors i couldn't figure out, you have to CALIBRATE the esc first, then pulse it, life saver.
i am not python but now i know the procedure i can do this in nodejs/angular/react, thanks.
Tips:
1) Indent Format Error: file->open->esc.py in geanie and select all text, format->tabify region and file->save.
2) run from the command line rather than geanie F5 which moans about stuff use: $ python ESC.py
3) pigpio is usually already running so will generate an error = 'Can't initialise pigpio library' you can comment out the os.system line np
Reply 3 years ago
Hi,
past days I have been trying to use node js with pigpio.
But I can't get anything to happen when trying to calibrate. (( My ESC is calibrated by holding down it's button until led on it turns red (works when I do it with rc reciver) and then it blinks once do the calibration for forward and so on. )) For some reason the ESC gets stuck at the (first) red light when turning it on for calibration.
Do you have any suggestion or an example how you did it? :)
Question 3 years ago
Is there an updated version of the python file for python 3? I'm trying to run the current file using Python 3.5.3 and i'm getting a lot of errors.
Thanks
Question 4 years ago on Step 5
Hello sir,
i made same as you
But at time of connection of 4 ESC at a time program can't work
How i made program for 4 ESC runnung at a time
Answer 4 years ago
I changed this code into for 4 ESCs to calibrate and control: ESC pin numbers like ESC1 ESC2.. then ESC_Array = [.....]
theeen: for x in ESC_Array
in this video I used my code derived from this code:
and code is here: https://github.com/inceabdullah/Raspberry-Pi-Multiple-ESC-Calibration-for-Drone