Drones and ROVs and Arduinos

A journey building drones and ROVs and other open hardware and Arduino based stuff

How to control a brushless motor through a ESC with Arduino

Electronic speed control (most commonly known as ESC) are nasty beasts: not from the controlling software point of view but for the way they need to be powered up, and because they need to be calibrated.

Especially the calibration is important because otherwise the ESC doesn’t know which are the limits of the transmitter, and it cannot control precisely the speed of the motor.

Before putting a ESC in any complex Arduino project, it is better to get used to how a ESC works using a very simple sketch.

But before seeing the code, let’s see how to wire it up, because care must be taken otherwise nothing will work.

Wiring up the circuit and powering the ESC

Usually ESCs need a voltage higher than the one provided by the Arduino from his 5V pin: typically they need 2 LiPo cells (around 8V). To achieve that all the circuit must be powered from an external power supply connected directly to the ESC and not via the Arduino, which will be powered by the BEC circuit of the ESC. To make that happen it’s enough to connect the red and black of the control connector to the 5V and GDN of the Arduino board.

The rest of the circuit is pretty easy: from pin 9 of the Arduino we have the signal for the ESC, and into pin 0, the voltage reading from the potentiometer comes in.

Let’s now see the code.

Arduino code

A ESC is made to receive inputs from a R/C transmitter, so it works as a servo: to control it you can use the Arduino Servo Library, so the test code is based on the servo library basic example published on the Arduino site.

```#include <Servo.h>

Servo esc;
int throttlePin = 0;

void setup()
{
esc.attach(9);
}

void loop()
{
throttle = map(throttle, 0, 1023, 0, 179);
esc.write(throttle);
}

```

It just takes the reading of the “throttle”, maps it from 0-1023 to 0-179 (analog reading to servo “degrees”) and then sends it to the ESC via the Servo library. Even in its extreme simplicity this sketch it very useful when you want to calibrate a new ESC to work with the Servo library of Arduino.

Calibration

Now that everything is setup, chances are that you motor might not rotate, or might rotate only forward, or might rotate only to 80% of the throttle (forward and reverse) and then stop: all these are symptoms of lack of calibration, or a calibration done with a different range of PWM signals.

More or less all ESCs have a similar calibration procedure:

• Power up the ESC while the having maximum forward throttle applied
• You’ll hear a tone and some beeps and after a while (usually 2 seconds) you’ll hear a confirmation tone and the led will blink a few times with a different color: this indicates that the ESC has measured the wavelenght of max throttle.
• At this point apply zero throttle (in a fwd/reverse ESC this means full throttle reverse), wait again few seconds for the tones and led to blink: full reverse measured.
• Then move to central (only for fwd/reverse ESCs) and wait again for the tone and blinks.
• Have fun with the motors

Another possibility is that even after that procedure, the motor will not turn immediately in reverse, or will only go 50% of forward speed: that’s because ESCs are programmable, and you can change many of their behaviors via either a programming card or via, again as with the calibration, hearing and decoding beeps and blinks.

A video is worth a thousands words, so here is a video I recorded to show the calibration procedure used with my ESC (sorry for the bad lighting and sound, my first instructional video).

72 comments on “How to control a brushless motor through a ESC with Arduino”

1. Thanks!
I never thought that servo example could be used for esc brushless. I tried, and it works. Thanks again!

2. manu
January 12, 2013

air can i have your mail ID …please send me how are you powering up 2 different power requiremnts of ESC and Adruino

3. Aaron
April 9, 2013

what size of potentiometer would i use for a 11.1v 3cell LiPo battery, and a 10Amp ESC? i thought it would be a 1 or 2 ohm if there is such a thing. (new-b, sorry)

• simonech
June 20, 2013

I just used one that was there, don’t remember its resistance

• eRCaGuy
August 21, 2015

Aaron, The potentiometer you use is based on the voltage of the Arduino, and the max current you want to allow through the pot, not the LiPo battery voltage or current to the motor. I recommend a 1k~10k pot (I like 10k the most), and nothing smaller than 200Ohms.

April 18, 2013

Hi, you have a very good tutorial, even I used to try my radio controlled car.
I’m doing a UGV (unmmaned ground vehicle) and I’ll use brushless motors.
Thanks for share
http://doingdrones.wordpress.com/2013/04/18/is-not-a-toy-is-a-racing-car-and-will-be-an-ugv/

May 4, 2013

Hi :)!
I’m facing a problem my friend! I did same steps and control worked fine but the quad copter didn’t fly… It seems the rpm not enough i think.. Am using 3cell lith. Battery 11.7 volt, 850 mahr..

Another problem when i gave the order to 4 motors to run.. 1 or 2 work and the others stopped!

• simonech
June 20, 2013

This is just a very small circuit to calibrate the ESC and test the motor.
A quad is much more complicate than this, with many more variables

6. maggiendl
May 31, 2013

Is there anyway to read the speed?

• simonech
June 20, 2013

Nope…

• bob
November 10, 2013

Of course you can. There are many possibilities… Display it on an LCD module, Serial print it to the terminal, Display it on a 7 segment display… the possibilities are endless!

• simonech
November 10, 2013

Yes, you can show it how you want, but cannot read the speed at which motor is spinning, unless you put an encoder on the motor.

• eRCaGuy
August 21, 2015

….actually….yes, you can read the motor speed without an encoder. I’ve done it. It’s hard, but when done properly it works great. I’m thinking about writing a library to read up to 8+ sensorless brushless motor RPMs simultaneously….with no encoders nor hall effect sensors. I’d write the library to be as easy as the servo library to use. This has a lot of commercial potential I think, so I’m debating licensing/selling a commercial licensed version of it…..not exactly sure yet. I could then also write a library that uses a closed-loop feedback control system (PID-based) to set an exact motor RPM. Ex: by calling setRPM(MOTOR1, 3000); the motor would automatically adjust to go to exactly 3000 RPM, regardless of your motor, battery, propeller, load, etc. This has tons of useful applications too….

• Zut Alors
August 8, 2014

Your motor has a specifc KV value (RPM/volt). If you read the voltage, you can calculate the RPM.

Just make sure you use a voltage divider circuit – your Arduino can only take 5V at the pins – so if you are using a 3S Lipo to power your ESC, you will need to divide the voltage by 2.5 to connect to your Arduino to read it. As it’s all relative, your calculation can include a factor to multiply by that is the same as the voltage divisor.

• B
March 10, 2015

i dont think its that easy, the efficiency of your motor woukd never be 100%
i was trying to measure the rpm’s, i was thinking on a mark on the propels and a optic sensor.

• eRCaGuy
August 21, 2015

Zut, it’s true that you can calculate it by reading the motor pulses, but it’s not the motor voltage you need, it’s the commutation frequency. This can be read by an Arduino, but it’s complicated. I’ve done it; thinking about writing a library…..it takes time; a library is a lot of work but makes is super easy to use.

7. Jacob Toop-Rose
August 25, 2013

Just spotted this. You refer to the output that the arduino is giving as a PWM signal. It isn’t (because of the library you are using). It is in fact a PPM signal. PWM signals are sketchy to use with ESC’s and could burn it out. Also powering the ESC of the arduino is a similarly bad idea ( you should not plug the ESC hot wire into the five volt pin) this will instead destroy your arduino given the right circumstances. I think most ESC have a onboard BEC to eliminate the need for this. You could be instructing people into making very expensive mistakes…

• t.K.
December 16, 2013

I’ve been using this basic circuit to lift prototype doggy door using gear reduction and brushless motors. I have had no problems. only thing I changed was applying the 5v from BEC to Vin pin like all other guides I’ve read recommend, and actually 7 to 9volt BECs would be better at Vin to be perfectly stable. Many many projects power the arduino in similar fashion.

• Jacob ( again)
April 1, 2014

I am so sorry and I realise only know what I said. It is not a PPM signal that is used it is PWM. I’m still not sure about the wire thing but just be careful. Again I’m so sorry.

• eRCaGuy
August 21, 2015

I disagree with you. I’ve written a PPM writer library, and have thoroughly studied the Arduino servo library, and have written code to read PPM and PWM signals (ex: download my Timer2_Counter library for a PWM-reader demo example file: http://www.electricrcaircraftguy.com/2014/02/Timer2Counter-more-precise-Arduino-micros-function.html). I have an oscilloscope, and have studied them both. The servo library does in fact output an RC PWM, *not* PPM signal.

If your ESC has a built-in BEC (voltage regulator), you *can* power the Arduino directly from the 5V line from the BEC. Many BECs, however, output 5.5V. Do NOT run a 5.5V power line to the 5V pin on the Arduino….that could ruin the Arduino.

Alternatively, you can power the Arduino via the 9V jack or Vin pin using 7~12V directly from your LiPo battery. Read more: http://www.electricrcaircraftguy.com/2014/02/quick-tip-4-ways-to-power-arduino.html and http://www.electricrcaircraftguy.com/2014/02/arduino-power-current-and-voltage.html

8. Sankha
September 13, 2013

Hello,
If you can tell me how to change moter rotating side. (Clockwise or ant clockwise )
Using ardiuno……because two moters are running one side and other two moters are runing other side in qudo copter.
Thank you

• simonech
October 23, 2013

You just need to invert the connection of the wires: switching any 2 of the 3 will do

• Bill
June 26, 2015

Motor

9. Arduinofreek
September 30, 2013

Thank you amigo !

10. Dominic
October 1, 2013

This is a simply excellent solution. We have been trying to find a solution for our throttle control ti use in an electric bike we are building using R/C motors. Whatever the person above has to say is wrong as the two systems are separately powered and as long as you don’t make the mistake of trying to cross over the power systems then there is no danger of blowing a board or anything else.
Thank you for sharing this…. I am most grateful

11. Hubert
November 6, 2013

Thanks for this post. Very nice.

Two questions:

1) Has anyone automated all the startup procedure? If so could you post the code?

2) What is the experience for the minimum controllable speed. Roughly speaking, could anyone say if it is 1%, 10%, 50% of maximum?

Many thanks

Hubert

• Psycholist666
November 6, 2013

The first point to make is that all ESC’s are different, some are programmable some are not, some come with a USB interface or you can buy one separately.
Most ESC’s will have a start up procedure as a built in “fail safe” system. I recently had a drone that you had to map out the throttle and cyclic controls twice each time you powered up. This was mainly because you have to reset the gyro system as well as tell the ESC the throttle map.
My current project is building an electric bike and I’m using two Phoenix Ice 2 ESC’s that require you to physically map the throttle each time you power up. So first it maps maximum throttle, 50% throttle and then minimum throttle. To least throttle response we get is at about 25% of the physical throttle turn below that we get nothing.
As for the percentage of of minimum controllable speed is dependant on many factors such as power used, motor rating, type of esc and type of throttle. ESC’s are quite complex as I said earlier it all depends on the type and manufacturer. I have met some RC Helicopter pilots who use some very funky set ups and massively over power the systems just to get better response for 3-D flying.
One note on using the Arduino for this: The Arduino simply puts out a pulsed signal the the ESC recognises so the ESC response will be dependant more on the throttle you use to tell the Arduino it’s position and the sensitivity of the ESC receiving the signal.
Hope this helps?

12. VespaBoy
November 11, 2013

Vi ringrazio per il vostro esempio, volevo sapere se la calibrazione e essenziale per qualunque ESC cioÃ¨ posso usare un minimo valore per osservare la direzione senza causare un incendio in cucina Ah Ah Ah ðŸ™‚

13. Hubert
November 12, 2013

Thanks for this. Could you post the part-numbers of the motor and ESC for info?

Many Thanks

Hubert

14. Jean M.
November 17, 2013

Thanks a lot, my project finally has a shot at actually getting made. Keep up the exemplary work! Coolest.

15. Pingback: Hacking the UMX part 2 | Botlets

16. sameer
January 23, 2014

hello sir,
i just tried your code its helpful to me but i need a controlled this motor through android mobile by using bluetooth

17. deab
March 21, 2014

hello can anyone here just let me know whether we can configure ESC’s using power suppy 12 V @ 3A or by using 12 V inverter battery. Thanks in advance

18. Hendra Pras
April 1, 2014

hello, I have some trouble with my ESC. I had programmed my ESC with program above. I sound bip from that ESC but my brushless didn’t work. anyone can help me???

• aminullah_min
April 9, 2014

if only sound bip. that is error occur. maybe supply no enough. try change with other supply 12V, 7.2Ah, 2A.

19. aminullah_min
April 9, 2014

can we add the circuit with other motor?? using another esc and motor but in same arduino. it is possible?? what about coding??

20. Christopher
June 24, 2014

How do you connect your ESC directly to power supply? Which kind of wire are you using? I only have the power supply I haven’t received the batteries yet so I need to power up my motor without the LiPo battery

21. Dom
July 25, 2014

The ESC should already have power leads attached which should be sized as to the Max amps that the ESC can or will draw. Some ESC’s come with connectors attached and some don’t. If there are no connectors you will have to find one that is able to cope with the demands of your system. RC hobbyists use a variety depending on the application, power supply and max amps draw. If just doing a test use Crocodile clips but also make sure that you are supplying enough amps for the motor especially if testing under load and croc clips won’t do that!

22. Pingback: Anonymous

23. amit singh
November 27, 2014

I am trying to build a quadcopter but i am facing problem in calibration of ESC. I have 4in 1 EMAX ESC. I am using Arduino UNO to calibrate the ESC. My code is:

#include

Servo esc;
int potpin = 0;// Pot pin
int val=0;
void setup()
{
esc.attach(9);// ESC attach to pin no 9
}
void loop()
{
val = map(val, 0, 1023, 0, 179);
esc.write(val);
}
by using this code i have calibrated 2 ESCs (esc1 and esc2) out of 4 and they are working fine but other 2 ESCs(esc 3 and esc4) are not calibrating properly although the ESC is giving the calibration confirmation tone but motors are not rotating.
my procedure is-
1- throttle the pot at full.
2- connect the li po and turn on the esc
3- after exact beep sending the minimum signal to ESC (by throttling pot to lowest position).
4- certain beeps come and finally calibration confirmation beep comes and ESC has calibrated. but motors are not rotating
Plz help me .

24. sid
January 22, 2015

I cant seem to calibrate…Just beeps once every 2 seconds… any coments

January 25, 2015

I came to know that inorder to fly the quad two props has to be propelled in CW and another two props in CCW. But how to achieve that ?? Any idea ??

26. jwmana
March 21, 2015

thank you for this important post ðŸ™‚
i have a question :
in my quad copter i used APM 2.5 controller .. so can i calibrate my ESC with arduino then connect ESC with APM and it works correctly ??

27. jose
March 29, 2015

Hi all

I have a issue on the calibration of my 4 ESC’s 2 of my motors speed more thant the others.

Why this is happen?

28. Jeegs
April 27, 2015

Thanks a lot. It’s very useful~

29. Mohit
June 4, 2015

Is there any way to run Bldc motor without any ESC on maximum speed ? I don’t want to use receiver and transmission. I have Turnigy XK1222.

• eRCaGuy
August 21, 2015

NO, Brushless DC motors require a precisely timed 3-phase commutation frequency (usually a trapezoidal wave is used for Brushless DC ESCs) to make them rotate even if the throttle is 100% and there is no superimposed throttling PWM frequency (I am speaking of true “analog-like” PWMing here, not RC signal PWM).

30. Anesh
June 13, 2015

didn’t work…

31. Sarvesh Thakur
July 5, 2015

hi all,
Is it possible to control ESCs through Bluetooth relay(6 or 8 channels)?

32. Kym
October 26, 2015

Hi there!

I also did power my 25A esc using an external power supply rated 12v 1.0A. It doesn’t work. with my brushless motor (2836 – 3200kv) connected, it doesn’t even make any beep code. I’ve tried to use my arduino uno to control the esc and did a lot of tutorials i’ve found out there for calibrating or arming, but nothing worked.

I suspect that my power supply doesn’t provide enough current. Does current really matter when using an external power supply? Thanks!

33. Kym Cainday
October 26, 2015

Hi there!

I also did power my 25A esc using an external power supply rated 12v 1.0A. It doesn’t work. with my brushless motor (turnigy 2836 – 3200kv) connected, it doesn’t even make any beep code. I’ve tried to use my arduino uno to control the esc and did a lot of tutorials i’ve found out there for calibrating or arming, but nothing worked.

I suspect that my power supply doesn’t provide enough current. Does current really matter when using an external power supply?

Thanks!

34. kkkkkkkkk
November 1, 2015

is controlling a brushed and a brushless esc the same? can i use the same method and coding to control a brushed esc and motor?

35. kkkkkkkkk
November 1, 2015

can i control a brushed esc and motor like this also?

• Tristan
December 24, 2015

Control a brushed ESC simply by applying a PWM signal to the motor with no ESC. reverse the polarity of the signal to change direction.

36. Vet
November 7, 2015

My esc needed 1000 – 2000 signals instead of 0-180,

otherwise GREATEST Piece of info I found for beginner how to tune calibrate and operate an ESC with arduino

37. Diwakar
December 14, 2015

I am not able to use it . I tried to see what input is given by THrottle : its always giving same reading even if change value of throttle….. i have used 5kpot as throttle

38. sooncheng
January 12, 2016

hi, i follow what exactly u said , but once i uploaded the code and connected the whole thing together , the motor will just produce beep sound , as i increase the value , the beep sound beep faster but the motor still didn’t turns (it turns sometimes as i reconnect the battery to the esc , but after i repluged the battery again, it stop works ), i also googled around but i still cant get the ans , everyone was using the same way as urs to run an brushless motor with a esc , i used a 1400kv brushless motor and a 30A esc which i brought from the china , is it the signal sent from the arduino isn’t match with the arduino that the esc needed , if is , how can i found out the frequency that the esc need , i didn’t have a oscilloscpe or a brushless motor controller to test the brushless motor out , what should i do ? thanks a lot for ur replying .

39. D
January 16, 2016

Hello,
I have four esc afro 12a with BEC and four brushless motors DYS BE 1806 2300kv.
Actually, I am a novice into the world of arduino and I would like to have some information :
if arduino uno is powered by a 11.1 V lipo battery and if it is also connected to the 5V pin of arduino uno, could esc be burnt ?
Furthermore, if arduino uno isn’t connected to the computer with a usb plug, it is needed to power arduino with a 9V battery ?
And I would like to know why esc has to be calibrated ? Actually what does calibration involve ?
Thanks.

40. Fiveby Tuesday
January 18, 2016

Thanks, worked perfectly. 850KV motor, SimonK 20A ESC and 11.1 Lipo

41. khalid
May 8, 2019

Hi, your video was very helpful, I run my OCDAY 120A, it was instantly fine. my test was made with 3s lipo batteries, but when I put 2 (3s lipo batteries) in series nothing happen, no beep from ESC and the motors don’t run. what could be the problem? thank you

• Gabriel Staples
May 8, 2019

According to GearBest the OCDAY 120A ESC is rated for 2~4S LiPo (https://us.gearbest.com/rc-car-parts/pp_009720719835.html). Putting 2x 3S LiPos in series makes a 6S LiPo (note that the “S” here stands for cells in ‘S’eries). So, you exceeded the voltage rating of your ESC and killed it. It’s dead.

42. khalid
May 8, 2019

thank you, it answers my request. I will go with for 4SLiPo. Great Help

Information

This entry was posted on November 24, 2012 by in Arduino Programming and tagged , , , .

simonech

I am a web software developer with a passion for exploring, both new places and new technologies. And I'm outdoor activities addict.

View Full Profile →