Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




BLDC motor using ar...
 
Share:
Notifications
Clear all

BLDC motor using arduino

2 Posts
2 Users
0 Likes
1,228 Views
(@vaneeza)
New Member
Joined: 3 years ago
Posts: 0
Topic starter  

Hey guys, I just started working with Arduino and Idk what the issue with my code is. The motor is running but on one speed only. I need the pot to increase/decrease the motor speed. This code is to try the lcd to show battery percentage too. Please, would appreciate any help anyone can give.

 

#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

 

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 10 years ago
Posts: 2549
 

I have no experience with motors, so I'm just guessing ...

I do see that your code is reading the potentiometer and stores the value in Delay (this would not be a great variable name as it can be confused with the delay function) ... should this be delayTime instead?

int t = analogRead(IN); //From the potentiometer

// Should this be "Delay" or "delayTime" ???
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);
}

   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: