BLDC motor 360 rotation

Hi,

I am using arduino to run BLDC motor. According to the code the motor constantly mover 360 degrees untill i give stop/off comand. But i just want to stop the motor after one 360 degree rotation. Below is my code for the motor. Please help me with this query.

Regards,
Mustufa Trivedi

//
// Slow and precise BLDC motor driver using SPWM and SVPWM modulation
// Part of code used from http://elabz.com/
// (c) 2015 Ignas Gramba www.berryjam.eu
//

const int EN1 = 5;
const int EN2 = 6;
const int EN3 = 7;

const int IN1 = 9;
const int IN2 = 10;
const int IN3 = 11;

const int LEDpin = 13;
char input= 255;

// SPWM (Sine Wave)
//const int pwmSin[] = {127, 138, 149, 160, 170, 181, 191, 200, 209, 217, 224, 231, 237, 242, 246, 250, 252, 254, 254, 254, 252, 250, 246, 242, 237, 231, 224, 217, 209, 200, 191, 181, 170, 160, 149, 138, 127, 116, 105, 94, 84, 73, 64, 54, 45, 37, 30, 23, 17, 12, 8, 4, 2, 0, 0, 0, 2, 4, 8, 12, 17, 23, 30, 37, 45, 54, 64, 73, 84, 94, 105, 116 };

/// SVPWM (Space Vector Wave)
//const int pwmSin[] = {128, 147, 166, 185, 203, 221, 238, 243, 248, 251, 253, 255, 255, 255, 253, 251, 248, 243, 238, 243, 248, 251, 253, 255, 255, 255, 253, 251, 248, 243, 238, 221, 203, 185, 166, 147, 128, 109, 90, 71, 53, 35, 18, 13, 8, 5, 3, 1, 1, 1, 3, 5, 8, 13, 18, 13, 8, 5, 3, 1, 1, 1, 3, 5, 8, 13, 18, 35, 53, 71, 90, 109};
const int pwmSin[] = {128, 132, 136, 140, 143, 147, 151, 155, 159, 162, 166, 170, 174, 178, 181, 185, 189, 192, 196, 200, 203, 207, 211, 214, 218, 221, 225, 228, 232, 235, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 249, 250, 250, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 253, 252, 252, 251, 250, 250, 249, 248, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 249, 250, 250, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 253, 252, 252, 251, 250, 250, 249, 248, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 235, 232, 228, 225, 221, 218, 214, 211, 207, 203, 200, 196, 192, 189, 185, 181, 178, 174, 170, 166, 162, 159, 155, 151, 147, 143, 140, 136, 132, 128, 124, 120, 116, 113, 109, 105, 101, 97, 94, 90, 86, 82, 78, 75, 71, 67, 64, 60, 56, 53, 49, 45, 42, 38, 35, 31, 28, 24, 21, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 24, 28, 31, 35, 38, 42, 45, 49, 53, 56, 60, 64, 67, 71, 75, 78, 82, 86, 90, 94, 97, 101, 105, 109, 113, 116, 120, 124};
//const int pwmSin[]= { 64,66,68,70,72,74,76,78,80,81,83,85,87,89,91,93,95,96,98,100,102,104,106,107,109,111,113,114,116,118,119,120,120,121,121,122,122,123,123,124,124,124,125,125,125,126,126,126,127,127,127,127,127,127,128,128,128,128,128,128,128,128,128,128,128,128,128,127,127,127,127,127,127,126,126,126,125,125,125,124,124,124,123,123,122,122,121,121,120,120,119,120,120,121,121,122,122,123,123,124,124,124,125,125,125,126,126,126,127,127,127,127,127,127,128,128,128,128,128,128,128,128,128,128,128,128,128,127,127,127,127,127,127,126,126,126,125,125,125,124,124,124,123,123,122,122,121,121,120,120,119,118,116,114,113,111,109,107,106,104,102,100,98,96,95,93,91,89,87,85,83,81,80,78,76,74,72,70,68,66,64,62,60,58,57,55,53,51,49,47,45,43,41,39,38,36,34,32,30,28,27,25,23,21,19,18,16,14,12,11,9,9,8,8,7,7,6,6,5,5,4,4,4,3,3,3,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5,6,6,7,7,8,8,9,9,9,8,8,7,7,6,6,5,5,4,4,4,3,3,3,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5,6,6,7,7,8,8,9,9,11,12,14,16,18,19,21,23,25,27,28,30,32,34,36,38,39,41,43,45,47,49,51,53,55,57,58,60,62};

int currentStepA;
int currentStepB;
int currentStepC;
int sineArraySize;
int increment = 0;
boolean direct = 1; // direction true=forward, false=backward
int run_flag=0;

//////////////////////////////////////////////////////////////////////////////

void setup() {

pinMode(LEDpin, OUTPUT);
digitalWrite(LEDpin, LOW); // LED OFF
delay(1000);
digitalWrite(LEDpin, HIGH); // LED ON
Serial.begin(9600);
Serial.print("start \n");

setPwmFrequency(IN1); // Increase PWM frequency to 32 kHz (make unaudible)
setPwmFrequency(IN2);
setPwmFrequency(IN3);

pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);

pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(EN3, OUTPUT);

digitalWrite(EN1, LOW);
digitalWrite(EN2, LOW);
digitalWrite(EN3, LOW);

sineArraySize = sizeof(pwmSin)/sizeof(int); // Find lookup table size
int phaseShift = sineArraySize / 3; // Find phase shift and initial A, B C phase values
currentStepA = 0;
currentStepB = currentStepA + phaseShift;
currentStepC = currentStepB + phaseShift;

sineArraySize--; // Convert from array Size to last PWM array number

int chek = 143/2;
Serial.print(chek);
}

//////////////////////////////////////////////////////////////////////////////

void loop() {

// if there's any serial available, read it:
if (Serial.available() > 0)
{
//Serial.println(Serial.read());
input = Serial.read();
//Serial.println(input-0x30, HEX);
//Serial.print("\t");
Serial.print(input);
Serial.print("\n");
}

switch(input)
{
case '1':
digitalWrite(LEDpin, LOW); // LED OFF

// analogWrite(IN1, 0);
// analogWrite(IN2, 128);
// analogWrite(IN3, 255);

break;

case '2':
digitalWrite(LEDpin, HIGH); // LED ON
run_flag =1;
// analogWrite(IN1, 50);
// analogWrite(IN2, 255);
// analogWrite(IN3, 100);

digitalWrite(EN1, HIGH);
digitalWrite(EN2, HIGH);
digitalWrite(EN3, HIGH);
Serial.print("All ON \n");
break;

case '3':
digitalWrite(LEDpin, LOW); // LED OFF

digitalWrite(EN1, LOW);
digitalWrite(EN2, LOW);
digitalWrite(EN3, LOW);

analogWrite(IN1, 0);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
run_flag =0;
Serial.print("All OFF \n");

break;

case '8' :
direct=1;
Serial.print("FORWRD \n");
break;

case '9' :
direct=0;
Serial.print("REVRSE \n");
break;
}

input =255;

if(run_flag ==1)
{
analogWrite(IN1, pwmSin[currentStepA]);
analogWrite(IN2, pwmSin[currentStepB]);
analogWrite(IN3, pwmSin[currentStepC]);

if (direct==true) increment = 1;
else increment = -1;

currentStepA = currentStepA + increment;
currentStepB = currentStepB + increment;
currentStepC = currentStepC + increment;

//Check for lookup table overflow and return to opposite end if necessary
if(currentStepA > sineArraySize) currentStepA = 0;
if(currentStepA < 0) currentStepA = sineArraySize;

if(currentStepB > sineArraySize) currentStepB = 0;
if(currentStepB < 0) currentStepB = sineArraySize;

if(currentStepC > sineArraySize) currentStepC = 0;
if(currentStepC < 0) currentStepC = sineArraySize;

/// Control speed by this delay
delay(1);
}
}

void setPwmFrequency(int pin) {
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | 0x01;
} else {
TCCR1B = TCCR1B & 0b11111000 | 0x01;
}
}
else if(pin == 3 || pin == 11) {
TCCR2B = TCCR2B & 0b11111000 | 0x01;
}
}

You need to stop the code when the phase has advanced the appropriate number of cycles corresponding
to one mechanical rotation.

I presume you are driving a sensorless BLDC in open-loop fashion? That has problems since you don't
know the required torque to overcome the load so have to set the current high enough for all eventualities,
which may be very power hungry.

Thanks mark for your reply. i really appreciate it :slight_smile: