BLDC motor controlled using arduino

Hello everyone!
I’m working on a project and I have worked on this code but it has some issues. As its my first time Idk what I’m doing wrong.

I’ll post the code here but basically changing delay time isnt affecting the change time of phases and the pot isnt affecting speed of the motor.

The functions I was striving for were lcd display of battery level and pot changing speed of my motor.

#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

Please learn how to properly indent and present code in the forum.

Actually your motor performs 1 step per 2 seconds, enough time to verify proper signals to the driver. Are you sure that the phases are connected and controlled in the right sequential order? In the long run the Serial outputs may slow down motor motion even more.

What are the analog inputs? Do they work at such low step rate, and how high are their readings?

The pot only affects Delay, while the delay is controlled by delayTime. Also don’t confuse millis and micros.

Are you sure that you want to start with such a complicated program and wiring instead of something easier to understand and implement?

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