Programming a DIY Arduino BLDC controller. - Page 1
Forum section: electronics / general

author: Refrigerator
author_rank: Super Contributor
timestamp: July 01, 2017, 12:06:48 pm
content: 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)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: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" );}}

author: jwm_
author_rank: Frequent Contributor
timestamp: July 01, 2017, 02:51:53 pm
content: Atmel has a few nice app notes on doing it.http://www.atmel.com/images/doc8012.pdfhttp://www.atmel.com/Images/Atmel-2596-Sensor-based-Control-of-Three-Phase-Brushless-DC-Motors_Application-Note_AVR443.pdfhttp://www.atmel.com/images/doc8138.pdfI'd recommend dropping the arduino libraries though and just programming the hardware directly.

author: Refrigerator
author_rank: Super Contributor
timestamp: July 01, 2017, 07:04:01 pm
content: Quote from: jwm_ on July 01, 2017, 02:51:53 pmI 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.