Problems with BLDC motors

Hi, I am using Arduino to control a BLDC motor. It is working fine but I need to control the speed of the motor with the help of a potentiometer. I don't know if there are problems with my code but I can't seem to control the speed of the motor. Below you can find the code I used:

#include <LiquidCrystal.h>

//BLDC CONTROLLER PIN DECLARATION
int AA1=3;
int AA2=5;
int BB1=11;
int BB2=10;
int CC1=9;
int CC2=6;

int enable=2; //This is not used for now, The ESC is always enabled
int emfA=A0;
int emfB=A1;
int emfC=A2;

int fase=1;
int deltaA=0;
int emA=0;
int sum=0;

int IN=A3; //pin A3
int Delay=4000;

int it=0;
int it2=1;

static int delta= 0;
static int Lastdelta= -1;

unsigned long previousMillis = 0;

//LCD PIN DECLARATION FOR BATTERY DISPLAY

LiquidCrystal lcd(13,12,8,7,4,1);

int battPin = A4;
int delayTime = 1000;
int voltReading;
int volts;

 

void setup() {
//SETUP CODE FOR BLDC CONTROLLER

Serial.begin(250000);
pinMode(AA1,OUTPUT);
pinMode(AA2,OUTPUT);
pinMode(BB1,OUTPUT);
pinMode(BB2,OUTPUT);
pinMode(CC1,OUTPUT);
pinMode(CC2,OUTPUT);

pinMode(enable,OUTPUT);

pinMode(IN,INPUT);
pinMode(emfA,INPUT);
pinMode(emfB,INPUT);
pinMode(emfC,INPUT);

digitalWrite(enable,HIGH);
previousMillis = micros();

//SETUP CODE FOR LCD VOLTAGE DISPLAY
lcd.begin(16,2);
pinMode(battPin, INPUT);
Serial.begin(9600);

}

void loop() {

//MAIN CODE FOR BLDC CONTROLER
int emA = analogRead(emfA);
int emB = analogRead(emfB);
int emC = analogRead(emfC);
int sum = (emA+emB+emC)/3;

unsigned long currentMillis = micros(); //count time

if(currentMillis - previousMillis >= Delay)
{

previousMillis += Delay;

//Phase1 C-B
switch(fase){
case 1:
digitalWrite(AA1,LOW);
digitalWrite(AA2,LOW);
digitalWrite(BB1,LOW);
digitalWrite(CC2,LOW);
digitalWrite(BB2,HIGH);
digitalWrite(CC1,HIGH);
delta = emA-sum;

break;

//Phase2 A-B
case 2:
digitalWrite(AA2,LOW);
digitalWrite(BB1,LOW);
digitalWrite(CC1,LOW);
digitalWrite(CC2,LOW);
digitalWrite(AA1,HIGH);
digitalWrite(BB2,HIGH);
delta = emC-sum;
break;

//Phase3 A-C
case 3:
digitalWrite(AA2,LOW);
digitalWrite(BB1,LOW);
digitalWrite(BB2,LOW);
digitalWrite(CC1,LOW);
digitalWrite(CC2,HIGH);
digitalWrite(AA1,HIGH);
delta = emB-sum;
break;

//Phase4 B-C
case 4:
digitalWrite(AA1,LOW);
digitalWrite(AA2,LOW);
digitalWrite(BB2,LOW);
digitalWrite(CC1,LOW);
digitalWrite(BB1,HIGH);
digitalWrite(CC2,HIGH);
delta = emA-sum;
break;

//Phase5 B-A
case 5:
digitalWrite(AA1,LOW);
digitalWrite(BB2,LOW);
digitalWrite(CC1,LOW);
digitalWrite(CC2,LOW);
digitalWrite(AA2,HIGH);
digitalWrite(BB1,HIGH);
delta = emC-sum;
break;

//Phase6 C-A
case 6:
digitalWrite(AA1,LOW);
digitalWrite(BB1,LOW);
digitalWrite(BB2,LOW);
digitalWrite(CC2,LOW);
digitalWrite(CC1,HIGH);
digitalWrite(AA2,HIGH);
delta = emB-sum;
break;
}

if (Lastdelta < 0){
if (delta > 0)
{
Lastdelta=delta; //save the last delta
fase= fase + 1;
if (fase > 6)
{
fase = 1;
}
}
}//Zero cross from - to +

if (Lastdelta > 0){
if (delta < 0)
{
Lastdelta=delta;
fase= fase + 1;
if (fase > 6) {
fase = 1;
}
}
}//Zero cross from + to -

}//Case ends

 

int t =analogRead(IN); //From the potentiometer
Delay=map(t,0,1024,1,1000); //we obtain the delay speed using the potentiometer
//we map the values from 1 to 1000 microseaconds

//MAIN CODE FOR LCD VOLTAGE DISPLAY
voltReading = analogRead(battPin);
volts = (voltReading/1203)*100;
if (volts == 100)
{
lcd.setCursor(12,0);
lcd.print(volts);
lcd.print("%");
delay(delayTime);
lcd.clear();

Serial.print(volts);
Serial.print("%");
delay(delayTime);
}
else
{
lcd.setCursor(14,0);
lcd.print(volts);
lcd.print("%");
delay(delayTime);
lcd.clear();

Serial.print(volts);
Serial.print("%");
delay(delayTime);
}

} //loop ends

Is there a reason why you are not using the Servo library with the ESC ?

Hi,
Can you please post a circuit diagram?

What is the BLDC motor controller, link to specs/data.

What model Arduino are you using?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

This is direct code to control a BLDC using a 3-phase bridge and EMF sensing I think.

This project seems complicated to me. Maybe some of the links will be useful to you.

Can you tell us what Arduino board you are using (for speed of calculations)?

How fast are you currently able to spin the motor?
Is it possible you are not controlling it but just moving the rotor like a stepper motor?

You have several delays in your code. It seems unlikely the motor loop can survive such delays.

You are redefining Serial.begin(250000);
and I believe 250000 is an invalid speed.

Why? On my Arduino IDE v1.8.13 there are speeds .. 230400, 250000, 500000, 1000000 and 2000000.

Hi,

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

OK perhaps it is not an invalid speed but... these speeds are not reliable with a standard Arduino. However you've not indicated what board you are using.

I use a Chinese Arduino Nano clone with a CH340G chip. I don't know what OP uses. Why are other speeds unreliable? I've tried them and the communication works. I guess they depend on the capabilities of the USB2TTL chip - in this case CH340G.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.