Sensored BLDC motor control with Arduino

UPDATE : May 12, 2015, I’ve added schematic after the Arduino sketch below

I couldn’t find much info on this on the net so I thought to share my experiment for those researching on this.

Hardware as follows
Arduino – Pro Micro 5V
Mosfet – STP75NF75
Half bridge driver – IR2103

Three PWM outputs are used in this design to pulse the upper mosfets while the bottom mosfets are either on or off.


//Knobs
const int analogInPin1 = A0; // Analog input pin that the potentiometer is attached to

int sensorValue1 = 0; // value read from the pot
int outputValue1 = 0; // value output to the P

//hall input
int hall_sensor_y = A3;
int hall_sensor_b = A2;
int hall_sensor_g = 15;

//buzzer and LED
int buzzer = 16;
int LED = 17;

float throttle = 0.0;

boolean MotorOff = false;

float Cn = 0;

//Motor setup
const int y_motor_lout = 5;
const int y_motor_pwm_hout = 6;
const int b_motor_lout = 7;
const int b_motor_pwm_hout = 9;
const int g_motor_lout = 8;
const int g_motor_pwm_hout = 10;

enum WheelDirection {
DIR_FORWARD,
DIR_BACKWARD,
DIR_STOP
};

// MOTOR DRIVE
void MoveWheel(WheelDirection (dir), float (speed)) {
if (MotorOff) return;

//empty all motor registers
//half bridge driver, hi part is active high
//lo part is active low
analogWrite(y_motor_pwm_hout, 0);//set motor to stop
analogWrite(b_motor_pwm_hout, 0);
analogWrite(g_motor_pwm_hout, 0);

digitalWrite(y_motor_lout, HIGH);
digitalWrite(b_motor_lout, HIGH);
digitalWrite(g_motor_lout, HIGH);

int hall_y = digitalRead(hall_sensor_y);
int hall_b = digitalRead(hall_sensor_b);
int hall_g = digitalRead(hall_sensor_g);

//Serial.print("\r\n");
//Serial.print(hall_y);
//Serial.print(hall_b);
//Serial.print(hall_g);

if (dir == DIR_STOP) {
// do nothing
} else if (dir == DIR_FORWARD) {
if (hall_y == 0 && hall_b == 0 && hall_g == 1) {//001
analogWrite(b_motor_pwm_hout, speed);
digitalWrite(g_motor_lout, LOW);
} else if (hall_y == 1 && hall_b == 0 && hall_g == 1) {//101
analogWrite(b_motor_pwm_hout, speed);
digitalWrite(y_motor_lout, LOW);
} else if (hall_y == 1 && hall_b == 0 && hall_g == 0) {//100
analogWrite(g_motor_pwm_hout, speed);
digitalWrite(y_motor_lout, LOW);
} else if (hall_y == 1 && hall_b == 1 && hall_g == 0) {//110
analogWrite(g_motor_pwm_hout, speed);
digitalWrite(b_motor_lout, LOW);
} else if (hall_y == 0 && hall_b == 1 && hall_g == 0) {//010
analogWrite(y_motor_pwm_hout, speed);
digitalWrite(b_motor_lout, LOW);
} else if (hall_y == 0 && hall_b == 1 && hall_g == 1) {//011
analogWrite(y_motor_pwm_hout, speed);
digitalWrite(g_motor_lout, LOW);
}
} else if (dir == DIR_BACKWARD) {
if (hall_y == 0 && hall_b == 0 && hall_g == 1) {//001
analogWrite(g_motor_pwm_hout, speed);
digitalWrite(b_motor_lout, LOW);
} else if (hall_y == 1 && hall_b == 0 && hall_g == 1) {//101
analogWrite(y_motor_pwm_hout, speed);
digitalWrite(b_motor_lout, LOW);
} else if (hall_y == 1 && hall_b == 0 && hall_g == 0) {//100
analogWrite(y_motor_pwm_hout, speed);
digitalWrite(g_motor_lout, LOW);
} else if (hall_y == 1 && hall_b == 1 && hall_g == 0) {//110
analogWrite(b_motor_pwm_hout, speed);
digitalWrite(g_motor_lout, LOW);
} else if (hall_y == 0 && hall_b == 1 && hall_g == 0) {//010
analogWrite(b_motor_pwm_hout, speed);
digitalWrite(y_motor_lout, LOW);
} else if (hall_y == 0 && hall_b == 1 && hall_g == 1) {//011
analogWrite(g_motor_pwm_hout, speed);
digitalWrite(y_motor_lout, LOW);
}
}
delay(10);
}

void setup()
{
//Serial.begin(115200);

pinMode(y_motor_lout, OUTPUT);
pinMode(y_motor_pwm_hout, OUTPUT);
pinMode(b_motor_lout, OUTPUT);
pinMode(b_motor_pwm_hout, OUTPUT);
pinMode(g_motor_lout, OUTPUT);
pinMode(g_motor_pwm_hout, OUTPUT);

pinMode(buzzer, OUTPUT);
pinMode(LED, OUTPUT);

pinMode(hall_sensor_y, INPUT);
pinMode(hall_sensor_b, INPUT);
pinMode(hall_sensor_g, INPUT);

//half bridge driver, hi part is active high
// lo part is active low
analogWrite(y_motor_pwm_hout, 0);//set motor to stop
analogWrite(b_motor_pwm_hout, 0);
analogWrite(g_motor_pwm_hout, 0);

digitalWrite(y_motor_lout, HIGH);
digitalWrite(b_motor_lout, HIGH);
digitalWrite(g_motor_lout, HIGH);

}

void loop()
{
// read the tuning knob value:
for(int i=0;i<5;i++){ sensorValue1 += analogRead (analogInPin1); } sensorValue1 = sensorValue1/5; // map tuning knob to the range of the analog out: outputValue1 = map(sensorValue1, 0, 1023, -90, 90); //PID CALC Cn = outputValue1; //MOTOR DRIVE WheelDirection dir; if (Cn > 0)
dir = DIR_FORWARD;
else if (Cn < -0) dir = DIR_BACKWARD; else dir = DIR_STOP; throttle = abs(Cn); MoveWheel(dir, throttle); }

May 12, 2015 - Hi, due to quite a few requests on the circuit I've decided to draw it out and post it here. The components for the half-bridge driver is picked up from http://www.irf.com/technical-info/appnotes/an-978.pdf . The schematic below shows that the motor is driven by 24V power source, you can use higher voltage as long as it meets the rating of the MOSFET. Guys, please use the schematic at your own risk and please take necessary precaution when working with high voltage. I suggest test your circuit and motor at low voltage first before supplying full voltage. Again, I should stress that this is how I do it in respect to my hardware (Arduino) limitation, your project should use this as a base and expand to fix it's flaws. Cheers

DSC03573

42 thoughts on “Sensored BLDC motor control with Arduino

  1. aidilj,
    very cool controller. I don’t think I’ve ever seen anyone else write Arduino code to do that (probably because the Infineon-based motor controller are 1) so cheap and 2) everywhere).

    I am building an autonomous kayak around a BLDC scooter motor (http://www.edn.com/design/analog/4368430/The-24V-300W-BLDC-Kollmorgen-motor-integrated-controller).

    I am an accountant by training, and just turned 50, so software engineering isn’t second nature to me. I am however fascinated with the idea of letting people from around the world use my diy plans to build web-enabled solar-powered kayaks that can transmit still pictures, video, audio and gps information via the internet. kind of a “constellation” of floating instrumentation around the world.

    so far, I have built a prototype of an outboard motor using a modified version of the scooter motor shown in the link above. I basically removed the built-in motor controller so that the phase and hall sensor wires can be hooked up to an external motor controller. While this works well on the benchtop, I would prefer software (via Arduino) control of the motor, which would also allow the guidance system (also Arduino-based) to control both rudder position and motor on/off, forward/reverse, and speed.

    I am going to order the parts to test our you Arduino-based controller on my set-up. In the meanwhile, I was wondering if you have ever tried implementing speed control into your sketch via Arduino, versus using the potentiometers you have shown. I came across this example of BLDC speed control:
    http://www.instructables.com/id/Interfacing-Brushless-DC-Motor-BLDC-With-Arduino/

    I figured that since you authored the BLDC motor controller, that you might be able to modify your sketch to implement speed control as well.

    Thanks again, and I will be glad to share any additional information you might be interested in regarding my project (pics of the motor, the kayak with solar array, the steering system, etc.)

    Vince

  2. HELLO I PROBLEM TO run

    Arduino: 1.8.2 (Windows 8.1), Board: “Arduino/Genuino Uno”
    sketch_jul15a:140: error: ‘dir’ was not declared in this scope

    dir = DIR_FORWARD;

    ^

    sketch_jul15a:141: error: ‘else’ without a previous ‘if’

    else if (Cn < -0) dir = DIR_BACKWARD; else dir = DIR_STOP; throttle = abs(Cn); MoveWheel(dir, throttle); }

    ^

    exit status 1
    'dir' was not declared in this scope

  3. I added ‘dir’ at the end of the enum sub:

    enum WheelDirection {
    DIR_FORWARD,
    DIR_BACKWARD,
    DIR_STOP
    } dir;

    that seems to work now!

      1. void loop()
        {
        // read the tuning knob value:
        for(int i=0;i 0)

        if(sensorValue1 = sensorValue1/5)
        {
        dir = DIR_FORWARD;
        }
        else if (Cn < -0)
        {
        dir = DIR_BACKWARD;
        }
        else dir = DIR_STOP;
        throttle = abs(Cn);
        MoveWheel(dir, throttle);
        }

  4. I appreciate you posting this code. It provided a great starting place for my own Arduino based BLDC controller

  5. NICE PRJECT.
    ONE SMALL CLARIFICATION REQUIRED
    1. PULSE WIDTH VARITION IS NOT THERE FOR FULL TIME OF HALL SENSOR.
    2. HOW YOU HAVE KEPT SAME FREQUENCY FOR ALL THE PWM PINS USED PL EXPLAIN..
    THANKS IN ADVANCE
    RAVI
    INDIA

  6. Check out the arduino/examples for pot reading on an analog pin.
    middle pin of pot to an analog pin (a0-a5)
    outer pins of pot to 5v and the other pin grounded thru a resistor (5-10k) or the uno board will be drained of its 5v. With 3v, the map line values “out: outputValue1 = map(sensorValue1, 0, 1023, …” would need adjustment or the full range to 1023 can’t be selected.
    Notice the hall effect data pins are on arduino 15,A2,A3 in schematic.
    This is a cool start to hooking up arduino to ebikes. My contoller is like most it looks so has 12 mosfets p75nf75- 4 for each wire on a large aluminum heat sink.
    A few IRFP4468PBF’s triggered from arduino, i’m thinking.
    I like the idea of being able to have full control of hub wheels via programing.
    Nice page thanks.

  7. Hello Aidil!
    Thanks for sharing the schematic for the BLDC motor drive. I’ve used it to run the BLDC motor I’m using in my project. I faced some issues when I tried to implement this exact circuit. The motor runs in the proper direction as per the code, but there is a lot of shuddering noise and vibration. I’m using the IR2104 gate driver IC instead of the IR2013, and my controller is Arduino Uno. Rest of the circuit is exactly as yours.

    Specifications of my motor are – 60V BLDC Hub motor, 3 Hall sensors, 500W power rating.

    Do you have any idea as to why this might be happening?

    Thanks again!

    1. hi.. did you figure out.. kindly, tell me will it work , i just order 75nf75 mosfet, ir2103 , 3.3 ohm resistor ..

  8. I am a student and working on this BLDC drive project. I need the schematic for this project. kindly help me with giving your schematic.

    1. hai, lets discuss with me about control BLDC sensored. im do Last Project like that, im form indonesia. please massage to my account FB/IG. FB : Rochim / IG : mnrochim. thanks

Leave a Reply to jhobatan Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.