Author Topic: Programming a DIY Arduino BLDC controller.  (Read 2371 times)

0 Members and 1 Guest are viewing this topic.

Offline RefrigeratorTopic starter

  • Super Contributor
  • ***
  • Posts: 1682
  • Country: lt
Programming a DIY Arduino BLDC controller.
« on: July 01, 2017, 12:06:48 pm »
After some messing around with blinky LEDs and LCDs i decided to make myself a BLDC controller.
I'm pretty much new to the whole Arduino thing, and it feels pretty ok thus far.
Since i'm new to the Arduino stuff i thought i might post my code here to see what people have to say about it.
X_H are high side outputs of the bridge.
X_L are for low side.
X_S are for sensing.
Steptime is the max time a step can take, basically keeps the whole thing moving, also helps start the rotation.
Serial.print in the whiles there just to show me how many times the while runs.
So far the code is as bare bones as it gets, and would run to max rpm ( if it worked  :D )
This particular code here is what i call the third revision.
The previous revision didn't work too well but it was all in the sense of the back EMF.
I haven't tried this code out yet so you can place your bets.
Code: [Select]
const int A_H = 7;
const int B_H = 8;
const int C_H = 9;
const int A_L = 10;
const int B_L = 11;
const int C_L = 12;
const int A_S = A0;
const int B_S = A1;
const int C_S = A2;
unsigned long Currtime = 0;
const long Steptime = 100;
void setup() {
  pinMode ( A_H, OUTPUT );
  pinMode ( B_H, OUTPUT );
  pinMode ( C_H, OUTPUT );
  pinMode ( A_L, OUTPUT );
  pinMode ( B_L, OUTPUT );
  pinMode ( C_L, OUTPUT );
  pinMode ( A_S, INPUT );
  pinMode ( B_S, INPUT );
  pinMode ( C_S, INPUT );
  digitalWrite ( A_H, LOW );
  digitalWrite ( B_H, LOW );
  digitalWrite ( C_H, LOW );
  digitalWrite ( A_L, LOW );
  digitalWrite ( B_L, LOW );
  digitalWrite ( C_L, LOW );
  Serial.begin (9600);
   Serial.print ( "print start" );
}
void loop() {
    // step 1/A -------------------------------------
  digitalWrite ( C_H, LOW );
  digitalWrite ( A_H, HIGH );
  Currtime = millis();
  while ( analogRead(C_S) < ( analogRead(A_S) + analogRead(B_S))/2 && millis() - Currtime < Steptime ){
    Serial.print ( "STEP 1" );
    Serial.print ( "\n" );
    }
    // step 2/B -------------------------------------
  digitalWrite ( B_L, LOW );
  digitalWrite ( C_L, HIGH );
  Currtime = millis();
  while ( analogRead(B_S) > ( analogRead(A_S) + analogRead(C_S))/2 && millis() - Currtime < Steptime ){
    Serial.print ( "STEP 2" );
    Serial.print ( "\n" );
    }
    // step 3/C -------------------------------------
  digitalWrite ( A_H, LOW );
  digitalWrite ( B_H, HIGH );
  Currtime = millis();
  while ( analogRead(A_S) < ( analogRead(B_S) + analogRead(C_S))/2 && millis() - Currtime < Steptime ){
    Serial.print ( "STEP 3" );
    Serial.print ( "\n" );
    }
    // step 4/D -------------------------------------
  digitalWrite ( C_L, LOW );
  digitalWrite ( A_L, HIGH );
  Currtime = millis();
  while ( analogRead(C_S) > ( analogRead(B_S) + analogRead(A_S))/2 && millis() - Currtime < Steptime ){
    Serial.print ( "STEP 4" );
    Serial.print ( "\n" );
    }
    // step 5/E -------------------------------------
  digitalWrite ( B_H, LOW );
  digitalWrite ( C_H, HIGH );
  Currtime = millis();
  while ( analogRead(B_S) < ( analogRead(C_S) + analogRead(A_S))/2 && millis() - Currtime < Steptime ){
    Serial.print ( "STEP 5" );
    Serial.print ( "\n" );
    }
    // step 6/F -------------------------------------
  digitalWrite ( A_L, LOW );
  digitalWrite ( B_L, HIGH );
  Currtime = millis();
  while ( analogRead(A_S) > ( analogRead(C_S) + analogRead(B_S))/2 && millis() - Currtime < Steptime ){
    Serial.print ( "STEP 6" );
    Serial.print ( "\n" );
    }
}
I have a blog at http://brimmingideas.blogspot.com/ . Now less empty than ever before !
An expert of making MOSFETs explode.
 

Offline jwm_

  • Frequent Contributor
  • **
  • Posts: 319
  • Country: us
    • Not A Number
Re: Programming a DIY Arduino BLDC controller.
« Reply #1 on: July 01, 2017, 02:51:53 pm »
Atmel has a few nice app notes on doing it.

http://www.atmel.com/images/doc8012.pdf
http://www.atmel.com/Images/Atmel-2596-Sensor-based-Control-of-Three-Phase-Brushless-DC-Motors_Application-Note_AVR443.pdf
http://www.atmel.com/images/doc8138.pdf

I'd recommend dropping the arduino libraries though and just programming the hardware directly.

Offline RefrigeratorTopic starter

  • Super Contributor
  • ***
  • Posts: 1682
  • Country: lt
Re: Programming a DIY Arduino BLDC controller.
« Reply #2 on: July 01, 2017, 07:04:01 pm »
I'd recommend dropping the arduino libraries though and just programming the hardware directly.

I had the arduino on hand so i thought i'd try it out, but i am looking at getting some cheaper STC micros.

Edit: forgot to mention that the code above worked out, and while it's not great the motor does run at around ~400 rpm.
« Last Edit: July 01, 2017, 07:06:22 pm by Refrigerator »
I have a blog at http://brimmingideas.blogspot.com/ . Now less empty than ever before !
An expert of making MOSFETs explode.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->