bldc sensorless control using arduino

Photo of author

By Jackson Taylor

more data will publish after one month
reference – 

  • microchip an970
  • http://ww1.microchip.com/downloads/en/AppNotes/00857B.pdf

program

#define hall_blue 5 //hall sensor not used
#define hall_yellow 7
#define hall_green 6
#define timout_bemf 5000
//order G Y B
#define bemf_blue 8
#define bemf_yellow 9
#define bemf_green 10
#define blue_high  A1
#define blue_low   A0
#define yellow_high  A3
#define yellow_low   A2
#define green_high  A5
#define green_low   A4
int blue, green, yellow;
int count;
int state;
void setup()
{
  Serial.begin(115200);
  pinMode(bemf_blue, INPUT);
  pinMode(bemf_yellow, INPUT);
  pinMode(bemf_green, INPUT);
  hall_sensor_init();
  motor_pin_init();
  off_all();
  on_low_side_mosfet_and_charge_bootstrap();
  Serial.println(“haiu”);
  off_all();
  count = 0;
  while (count < 2)
  {
    run_motor_without_sensor_anti_clock ();
    count++;
  }
  off_all();  delay(1);
}
void loop()
{
  motor_run_clockwise_using_back_emf();
}
void run_motor_without_sensor_anti_clock(void)
{
    off_all();
   digitalWrite(yellow_high, HIGH);
  digitalWrite(green_low, HIGH);
  off_all_delay();
  digitalWrite(yellow_high, HIGH);
  digitalWrite(blue_low, HIGH);
  off_all_delay();
  digitalWrite(green_high, HIGH);
  digitalWrite(blue_low, HIGH);
  off_all_delay();
  digitalWrite(green_high, HIGH);
  digitalWrite(yellow_low, HIGH);
  off_all_delay();
   digitalWrite(blue_high, HIGH);
  digitalWrite(yellow_low, HIGH);
  off_all_delay();
  digitalWrite(blue_high, HIGH);
  digitalWrite(green_low, HIGH);
  off_all_delay();
 //
}
void run_motor_without_sensor (void)
{
  off_all_delay();
  digitalWrite(blue_high, HIGH);
  digitalWrite(yellow_low, HIGH);
  // off_all_delay();
  ///digitalWrite(blue_high, HIGH);
  // digitalWrite(green_low, HIGH);
  off_all_delay();
  digitalWrite(yellow_high, HIGH);
  digitalWrite(green_low, HIGH);
  // off_all_delay();
  // digitalWrite(yellow_high, HIGH);
  // digitalWrite(blue_low, HIGH);
  read_hall_sensors();
  off_all_delay();
  digitalWrite(green_high, HIGH);
  digitalWrite(blue_low, HIGH);
  off_all_delay();
  // digitalWrite(green_high, HIGH);
  // digitalWrite(yellow_low, HIGH);
  off_all();
}
void motor_run_clockwise_using_back_emf(void)
{
  read_bemf_sensors(); off_all();
  count = 0;
  while (blue == 1 && yellow == 0 && green == 1 && count < timout_bemf) //101
  {
    digitalWrite(blue_high, HIGH);
    digitalWrite(yellow_low, HIGH);
    read_bemf_sensors(); count++;
  }
  off_all(); count = 0;
  while (blue == 1 && yellow == 0 && green == 0 && count < timout_bemf) //100 -byg
  {
    digitalWrite(blue_high, HIGH);
    digitalWrite(green_low, HIGH);
    read_bemf_sensors(); count++;
  }
  off_all(); count = 0;
  while (blue == 1 && yellow == 1 && green == 0 && count < timout_bemf) //110
  {
    digitalWrite(yellow_high, HIGH);
    digitalWrite(green_low, HIGH);
    read_bemf_sensors(); count++;
  }
  off_all(); count = 0;
  while (blue == 0 && yellow == 1 && green == 0 && count < timout_bemf) //010
  {
    digitalWrite(yellow_high, HIGH);
    digitalWrite(blue_low, HIGH);
    read_bemf_sensors(); count++;
  }
  off_all(); count = 0;
  while (blue == 0 && yellow == 1 && green == 1 && count < timout_bemf) //011
  {
    digitalWrite(green_high, HIGH);
    digitalWrite(blue_low, HIGH);
    read_bemf_sensors(); count++;
  }
  off_all(); count = 0;
  while (blue == 0 && yellow == 0 && green == 1 && count < timout_bemf) //001
  {
    digitalWrite(green_high, HIGH);
    digitalWrite(yellow_low, HIGH);
    read_bemf_sensors(); count++;
  }
}
void off_all_delay(void)
{
  delayMicroseconds(2000);  PORTC = 0;
  /*
    digitalWrite(blue_low, LOW);
    digitalWrite(blue_high, LOW);
    digitalWrite(yellow_low, LOW);
    digitalWrite(yellow_high, LOW);
    digitalWrite(green_low, LOW);
    digitalWrite(green_high, LOW);
    //delayMicroseconds(1);
  */
}
void read_bemf_sensors(void)
{
  blue = digitalRead(bemf_blue);
  yellow = digitalRead(bemf_yellow);
  green = digitalRead(bemf_green);
  //display_sensor_data();
  //Serial.println(PINB & 7);
}
void motor_run_clockwise(void)
{
  off_all(); read_hall_sensors();
  while (blue == 1 && yellow == 0 && green == 1)//101
  {
    digitalWrite(blue_high, HIGH);
    digitalWrite(yellow_low, HIGH);
    read_hall_sensors();
  }
  off_all();
  while (blue == 1 && yellow == 0 && green == 0) //100 -byg
  {
    digitalWrite(blue_high, HIGH);
    digitalWrite(green_low, HIGH);
    read_hall_sensors();
  }
  off_all();
  while (blue == 1 && yellow == 1 && green == 0) //110
  {
    digitalWrite(yellow_high, HIGH);
    digitalWrite(green_low, HIGH);
    read_hall_sensors();
  }
  off_all();
  while (blue == 0 && yellow == 1 && green == 0) //010
  {
    digitalWrite(yellow_high, HIGH);
    digitalWrite(blue_low, HIGH);
    read_hall_sensors();
  }
  off_all();
  while (blue == 0 && yellow == 1 && green == 1)//011
  {
    digitalWrite(green_high, HIGH);
    digitalWrite(blue_low, HIGH);
    read_hall_sensors();
  }
  off_all();
  while (blue == 0 && yellow == 0 && green == 1)//001
  {
    digitalWrite(green_high, HIGH);
    digitalWrite(yellow_low, HIGH);
    read_hall_sensors();
  }
  off_all();
}
void fault(void)
{
  //—————————fault————————————-
  if (blue == 1 && yellow == 1 && green == 1) //000
  {
    off_all();
    do
    {
      Serial.println(“fault!!!!!”);
      read_hall_sensors(); display_sensor_data();
    } while (blue == 1 && yellow == 1 && green == 1);
  }
  else if (blue == 0 && yellow == 0 && green == 0) //111
  {
    off_all();
    do
    {
      Serial.println(“fault!!!!!”);
      read_hall_sensors(); display_sensor_data();
    } while (blue == 0 && yellow == 0 && green == 0);
  }
  //—————————fault————————————-
}
void off_all(void)
{
  PORTC = 0;
  /*
    digitalWrite(blue_low, LOW);
    digitalWrite(blue_high, LOW);
    digitalWrite(yellow_low, LOW);
    digitalWrite(yellow_high, LOW);
    digitalWrite(green_low, LOW);
    digitalWrite(green_high, LOW);
    //delayMicroseconds(1);
  */
}
void read_hall_sensors(void)
{
  blue = !digitalRead(hall_blue);
  yellow = !digitalRead(hall_yellow);
  green = !digitalRead(hall_green);
  // recharge_boot();
  //display_sensor_data();
}
void delay_for_phase(void)
{
  // Serial.write(‘a’);
  // delayMicroseconds(10);
}
void recharge_boot(void)
{ delayMicroseconds(10);
  PORTC = 0;
  delayMicroseconds(1);
  digitalWrite(blue_low, HIGH);
  digitalWrite(yellow_low, HIGH);
  digitalWrite(green_low, HIGH);
  delayMicroseconds(1);
  PORTC = 0;
}
void on_low_side_mosfet_and_charge_bootstrap(void)
{
  digitalWrite(blue_low, HIGH);
  digitalWrite(yellow_low, HIGH);
  digitalWrite(green_low, HIGH);
  delay(1000); off_all(); delay(1);
}
void motor_pin_init(void)
{
  pinMode(blue_high, OUTPUT);
  pinMode(blue_low, OUTPUT);
  pinMode(yellow_high, OUTPUT);
  pinMode(yellow_low, OUTPUT);
  pinMode(green_high, OUTPUT);
  pinMode(green_low, OUTPUT);
}
void display_sensor_data(void)
{
  //Serial.print(“b-“);
  Serial.print(blue);
  //Serial.print(“-y=”);
  Serial.print(yellow);
  //Serial.print(“-g=”);
  Serial.println(green);
}
void hall_sensor_init(void)
{
  pinMode(hall_blue, INPUT_PULLUP);
  pinMode(hall_yellow, INPUT_PULLUP);
  pinMode(hall_green, INPUT_PULLUP);
}
void test_fan_driver(void)
{
  off_all();
  digitalWrite(blue_low, HIGH);
  digitalWrite(yellow_low, HIGH);
  digitalWrite(green_low, HIGH);
  delayMicroseconds(1000); off_all();
  digitalWrite(blue_high, HIGH);
  digitalWrite(yellow_high, HIGH);
  digitalWrite(green_high, HIGH);
  delayMicroseconds(1000);
}