Go Down

Topic: help for newbie with existing code for brushless motors (Read 1 time) previous topic - next topic


hi all, my name is mike from new zealand,

i have been experimenting with microcontrollers on and off for a while,however i have not yet done any programming,but i thought id give it a try with aduino to get my feet wet.

i have a question for those that are able to help,

there is a code posted by a user on youtube to run a hdd motor through a model brushless esc,speed controlled via a pot. i have copied the sketch and tried to verify it, however it throws up some error codes,which i cannot copy and paste...i know i need to do some reading on how to program,but i just want to get this setup going to get started, as i plan to build an electric car speed controller that is 4wd and i think this code is a good one to build onto.

would someone be kind enough to run me through what i should be doing to make this operational?

for arguments sake, i could use A0 PIN 9 as an output to the controller and AREF PIN0 as the input for the pot.

thanks so much in advance!


DEMO sketch for arming and running a generic ESC brushless motor controller from an Arduino and using a potentiometer to vary the motor speed.

Tested and working.

19 Feb 2011

#include (Servo.h)

Servo myservo;

int val; //Value for motor speed.
int analogPin = 3; //Potentiometer on pin 3
int servoPin = 9; //ESC on pin 9
int armValue=20; //"ZERO" position for arming ESC. As some wont arm with a '0' value from the arduino. (may need to experiment)

void arm(){ //Arming sequence of sending a minimum or 'joystick zero' for 2 seconds to simulate radio gear is ON and operational. myservo.write(armValue);
delay(2500); //Delay for the ESC to activate. (may need to experiment)

void setup() { Serial.begin(9600);
myservo.attach(servoPin); arm();

void loop(){
int val = analogRead(analogPin); //Make a analogue reading. This is from 0-1023
val = map(val, 0, 1023, armValue, 180); //Scale input to use it with the servo ie from the arming value to 180.
myservo.write(val); //Send servo position - Motor speed to ESC.
Serial.println(val); delay(100);


Coding Badly

Change this...

Code: [Select]
#include (Servo.h)

...to this...

Code: [Select]
#include <Servo.h>

Please use [code] [/code] tags when posting code.


Plugging my own link, but have a look around this site -


lots of ESCs doing various things in RC Cars, all driven by Arduino

Duane B

Read this
then watch this



thanks for the info people, coding badly, you were right, all i did was change what you said and it was away...after much reading i can now write codes like the above...im surprised how quick im picking it up!



While I'm working out kinks from my own Arduino BLDC code ( sketch and video here ), I just wanted to throw my 2 cents in case you're still planning to come back to this.
I notices you're using Serial communication as well as delay(100)  for step timing and both are very slow processes that will mess up timing of the phase commutation.
On the video in my link you will see the motor twitching once every second - that's me sending RPM values back to the PC via USB. I send a rather long message (about 60 characters including description) but still, it's so slow it almost completely stops the rotor while the Arduino is taking to the PC. It's better not to have serial comms in the program if you're planning to reach beyond a couple hundreds of RPMs.
delay(100) between commutation steps is way too long . The motor has probably 24-36 commutation steps per one 360 degree revolution, so it will be 3.6 seconds 'till it finish one turn. also, while it's waiting for delay to expire it's not doing anything useful. So, your delays would have to be in (hundreds of) microseconds for anything measured in hundreds RPM and you would need to avoid using delay and  just check for the timing without stopping the processor to see when it's time for the next step.


Go Up