Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

BLDC motor controller code on arduino (my own ESC)

Status
Not open for further replies.

arbaazEE

New Member
Hey everyone!
I am making an ESC of my own we have been using Mosfets,created our power supply and driver circuits using opto couplers, and using H-brigde for switching the mosfets in perfect order to run the motor
everything is going fine except the programming part. I have been doing it for quite long. I have tried many combinations but the motor is not rotating. It gives a beep. once it moved for 7 turns. but it didnt completed the whole 12 turns.
We are using a BLDC motor of rating
Model: A2212/6T
RPM/V: 2200 kV
Current: 12 A/60 s
The arduino code is as follows please guide me what am I doing wrong


int a=1000;
int pin1=3;
int pin2=5;
int pin3=6;
int pin4=9;
int pin5=10;
int pin6=11;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);
pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
///////////////////////////a-1-6///////////
Serial.println("1-6");

analogWrite(pin1, 127);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
analogWrite(pin6, 127);
delay(a);

//////////// b- 1-4/////////////
Serial.println("1-4");

analogWrite(pin1, 127);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
analogWrite(pin4, 127);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);

delay(a);
///////////e- 5-2//////////////////////////

Serial.println("5-2");

digitalWrite(pin1,LOW);
analogWrite(pin2, 127);
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
analogWrite(pin5, 127);
digitalWrite(pin6,LOW);

//Serial.println("5-2");
delay(a);
////////////////d- 3-6////////////////////
Serial.println("3-6");
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
analogWrite(pin3, 127);
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
analogWrite(pin6, 127);
delay(a);
//////////////////f- 5-4////////////////////
Serial.println("5-4");
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
analogWrite(pin4, 127);
analogWrite(pin5,127);
digitalWrite(pin6,LOW);

delay(a);

// Serial.println("3-6");
/////////////////////c- 3-2/////////////////////
Serial.println("3-2");

digitalWrite(pin1,LOW);
analogWrite(pin2, 127);
analogWrite(pin3, 127);
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);

//Serial.println("3-2");
delay(a);

}
 

Attachments

  • BLDCmotor.ino
    2.1 KB · Views: 255
I know nothing of code writing so can't speak to that, but how are you picking up the rotor position with regard to the stator of the motor? If your micro doesn't know where the rotor position is, the code to run means nothing.
 
With RC ESC they do not use any physical sensors for commutation, not sure if they sense current feed back.
Only three 3ph conductors used.
Max.
 
Thanks shortbus= and MaxHeadRoom78 for your response
so we are using a 3 phase bldc motor with 12 poles and it is sensorless.
the motor works like there are 12 poles 4 of which makes one phase and hence makes the total of 3 phase
in order for motor to rotate seamlessly two of its opposite poles needs to be north and poles right next to them needs to be south poles in this way the stator and rotor will rotate
so I picked up the sequence that i am using in the code from a website which suggested that MOSFETs should have switching in the sequence as below
1-6, 1-4, 5-2, 3-6, 5-4, 3-2 it is also shown in the picture below
1motor.jpg
 
But as Max explained better than me, how are you determining where in the sequence your motor is?
Just starting the sequence when the rotor isn't in that spot won't make it turn. A regular store bought ESC senses where the rotor is then starts the switching sequence from that point.
 
What voltages are you getting on the motor windings in the high and low state?

As long as it has sufficient time, the rotor should pull to the first position the windings are energised at.

You can think of it rather like a stepper drive; at low speeds it will jump to a position to match the stator field, but for high speeds it must be gradually accelerated so it stays locked to the rotating stator field, or it just stay where it is or jitter randomly.
 
I've seen this in lots of arduino code and just wanted to suggest a change,
Code:
   int pin1=3;
   int pin2=5;
   int pin3=6;
   int pin4=9;
   int pin5=10;
   int pin6=11;
Defining pins as integers means the value is stored in memory and each time it's used it has to be fetched from memory which slows the code down.
Unless your pins need to move then the code will be quicker (and use less memory) is you define them as constants with,
Code:
#define pin1 3
#define pin2 5
etc.
Note no equals or semicolon.

Mike.
 
You can think of it rather like a stepper drive; at low speeds it will jump to a position to match the stator field, but for high speeds it must be gradually accelerated so it stays locked to the rotating stator field, or it just stay where it is or jitter randomly.

That's the thing that makes the old school motors and drives that use hall or other sensors to detect position better. At least in my opinion.
 
But as Max explained better than me, how are you determining where in the sequence your motor is?
Just starting the sequence when the rotor isn't in that spot won't make it turn. A regular store bought ESC senses where the rotor is then starts the switching sequence from that point.
shortbus= sir I think that's where i am stuck. Can you please suggest any way to sense the position of a sensorless motor. That will be very helpful
Till now we have just rotated motor by ourselves randomly after running the sequence just in hope that it might get into the right position and start rotating .
 
I've seen this in lots of arduino code and just wanted to suggest a change,
Code:
   int pin1=3;
   int pin2=5;
   int pin3=6;
   int pin4=9;
   int pin5=10;
   int pin6=11;
Defining pins as integers means the value is stored in memory and each time it's used it has to be fetched from memory which slows the code down.
Unless your pins need to move then the code will be quicker (and use less memory) is you define them as constants with,
Code:
#define pin1 3
#define pin2 5
etc.
Note no equals or semicolon.

Mike.
thank you very much sir, We sill surely be considering it now
 
shortbus= sir I think that's where i am stuck. Can you please suggest any way to sense the position of a sensorless motor. That will be very helpful
Till now we have just rotated motor by ourselves randomly after running the sequence just in hope that it might get into the right position and start rotating .

Read the quote from rjenkinsgb in post #8. You need to ramp up into a speed, this allows the sequence to find the position. You can't set a speed and hope it will work from that point, if the squence is going too fast it won't have the time for the rotor to start moving. Look at some videos of drones and how they start, the prop stutters slowly until the rotor breaks free and then it ramps up to speed from that point.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top