r/ArduinoProjects • u/InternalVolcano • Jul 13 '24
Need suggestion to improve/fix code.
I am trying to make an Arduino based ESC for 3-phase BLDC motor, which is based on the first esc made by YouTuber Electronoobs. I don't understand most of the code, but I do know just enough to change the pin numbers in the code to match my circuit.
My ESC does run but it's very slow and have little torque. Rotating the potentiometer which according to code is supposed to change the speed of the motor, doesn't seem to do anything.
I know back EMF detection is working because when I hold the motor lightly it slows down, releasing it returns to the previous speed. I also added serial output of the back EMF detection, and on serial plotter, I can see the waves changing when I hold the motor lightly. I am also sure that this is not because of my circuit and unfortunately I didn't make a schematic of it either.
My motor is 600kv, power supply is 12v, the power supply isn't the limitation because the current doesn't exceed 2 amps, my power supply is rated for 5 amps. Unfortunately I don't have a commercial ESC to see whether my motor is the problem, but I also don't think there's any issue with my motor.
Here is the link to the original code and down below is the edited version to match my circuit. Thanks.
int AA1=5;
int AA2=3;
int BB1=9;
int BB2=6;
int CC1=10;
int CC2=11;
int enable=2; //This is not used for now, The ESC is always enabled
int emfA=A3;
int emfB=A2;
int emfC=A1;
int fase=1;
int deltaA=0;
int emA=0;
int sum=0;
int IN=A0;
int Delay=4000;
int it=0;
int it2=1;
static int delta= 0;
static int Lastdelta= -1;
unsigned long previousMillis = 0;
void setup() {
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();
}
void loop() {
int emA = analogRead(emfA);
int emB = analogRead(emfB);
int emC = analogRead(emfC);
int sum = (emA+emB+emC)/3;
unsigned long currentMillis = micros();
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
Serial.print(emA);
Serial.print(" ");
Serial.print(emB);
Serial.print(" ");
Serial.println(emC);
} //loop ends