BLDC jerkiness with PWM

Hello everyone,

Recently I have been trying to drive some BLDC’s to make a brushless gimbal. The BLDC’s do turn, but there is some jerkiness. I am using PWM to drive it. My code is based on the information provided here: http://elabz.com/bldc-motor-with-arduino-circuit-and-software/. I use the SN754410 as driver. This is my code:

const uint8_t sinarray[] = {255,255,255,255,255,255,254,254,254,253,253,253,252,252,251,251,250,249,249,248,247,247,246,245,244,243,242,241,240,239,238,237,236,234,233,232,231,229,228,227,225,224,222,221,219,218,216,214,213,211,209,208,206,204,202,201,199,197,195,193,191,189,187,185,183,181,179,177,175,173,171,169,167,165,163,161,158,156,154,152,150,147,145,143,141,139,136,134,132,130,128,125,123,121,119,116,114,112,110,108,105,103,101,99,97,95,92,90,88,86,84,82,80,78,76,74,72,70,68,66,64,62,60,58,56,54,53,51,49,47,46,44,42,41,39,37,36,34,33,31,30,28,27,26,24,23,22,21,19,18,17,16,15,14,13,12,11,10,9,8,8,7,6,6,5,4,4,4,3,3,2,2,2,1,1,1,0,0,0,0,0,0};
volatile int steps = 0;
volatile int analogDelay = 0;
int counter = 0;
volatile int ultSteps = 0;
#define motorPin1 3
#define motorPin2 5
#define motorPin3 11
#define motorPin1Reg OCR0B
#define motorPin2Reg OCR2A
#define motorPin3Reg OCR2B
volatile int val = map(analogRead(A0),0,1024,0,300);//2520 is one rotation
void setup() {
 pinMode(motorPin1, OUTPUT);
 pinMode(motorPin2, OUTPUT);
 pinMode(motorPin3, OUTPUT);
 pinMode(led, OUTPUT);
 Serial.begin(9600);
 //Set timer2 for two seperate PWM outputs on the ports 3 and 11
 TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
 TCCR2B = _BV(CS20);

 //Set timer0 for one pwm output on pin 5
 TCCR0A = _BV(COM0B1) | _BV(WGM00); //_BV(COM1A1)
 TCCR0B = _BV(CS00);
}

void loop() {
  if(counter >= 100){
   val = analogRead(A0);
   counter = 0;
  } 
  counter++;
  steps = ultSteps;
  while(steps > 360){
    steps -= 360;
  }
  //Set motorPin1's value
  int motorPin1Steps = steps;
  if(motorPin1Steps > 180){
    motorPin1Steps = 180 - (motorPin1Steps - 180);
  }
  motorPin1Reg = sinarray[motorPin1Steps];
  
  //Set motorPin1's value
  int motorPin2Steps = steps + 120;
  if(motorPin2Steps > 360){
    motorPin2Steps -= 360;
  }
   if(motorPin2Steps > 180){
    motorPin2Steps = 180 - (motorPin2Steps - 180);
  }
  motorPin2Reg = sinarray[motorPin2Steps];
  
  //Set motorPin1's value
  int motorPin3Steps = steps + 240;
  if(motorPin3Steps > 360){
    motorPin3Steps -= 360;
  }
   if(motorPin3Steps > 180){
    motorPin3Steps = 180 - (motorPin3Steps - 180);
  }
  motorPin3Reg = sinarray[motorPin3Steps];
  //Increase steps by 1
  if(ultSteps > val){
    ultSteps--;
  }
  if(ultSteps < val){
    ultSteps++;
  }
  delayMicroseconds(500);

  
}

Can anyone help me?!

Thanks in advance,

Jasper

Such a motor is going to be jerky without closed-loop control, close the loop and it will be more
well-behaved. Have you synchronized the counters you are using and selected phase-correct
modes BTW?