Position Control of a Brushless Motor with Arduino and SOLO [+ Arduino Code]
In this article we are going to control the position of a Brushless DC or Permanent Magnet Synchronous Motor using SOLO with Arduino and more specifically using SOLO’s Arduino Library , in another word, we are turning SOLO into a precise servo drive to interface and control a brushless Motor using Incremental Quadrature Encoders, in this article the communication between SOLO and Arduino is done through UART by sending data packets.
This type of control can be interesting for precise systems like CNC machines, 3D printers, Robots ,etc, where the accurate position of the rotor of the motor is important, with SOLO thanks to Field Oriented Control, you can simultaneously make sure all the parameters like Torque, Speed and Position are observed and controlled, so during your trajectory tracking in case of having load disturbances, you can be confident that they are not going to affect the quality of position controlling.
In simple language, to achieve Position Controlling with SOLO you need to deal with 3 control loops that are participating in this architecture and each of them are having their own part, in simple language:
1 The Torque Loop: This is the closest controller to the motor and the fastest one, it will only control the Torque of the Motor regardless of the Speed, The Speed depends on how much Load you have on the shaft of the Motor, so the difference between the Motor’s Torque and Load’s Torque will define the final Speed of the rotor of the motor.
2 The Speed Loop: The Speed loop comes behind the Torque Loop, it is slower than the Torque controller (at least around 10X) and it basically tries to fix the Speed on a desired value, this controller will keep the speed fixed regardless of the load variation and it will adjust the torque accordingly. You might ask why the Speed controller comes after the Torque controller, this is a big topic, but to simplify it drastically the in principle an Electrical Motor is a Torque Generation machine, even the Speed is controlled finally by controlling the Torque, so the speed controller when it tries to command, it commands to Torque Controller to increase or decrease the Torque on the Motor to stabilize the motor on a specific Speed that it desires!
3 The Position Loop: this is the last loop coming behind the speed loop, it basically tries to set the exact position of the Motor on a specific value, so to make this loop functional, you need to make sure the Torque and Speed loop are firstly tuned and ready, then you can do the Position Controlling.
If you are interested to know more about this technique and what is happening behind the scene you can have a look on the video and article below:
The Wiring
The only wiring you need to apply will be the connection of the Motor to SOLO and by just connecting UART_TX, UART_RX and GND lines between SOLO and ARDUINO.
SOLO UNO wiring to ARDUINO UNO

SOLO MINI wiring to ARDUINO UNO

SOLO BETA wiring to ARDUINO UNO
The UART_TX and UART_RX Lines of SOLO BETA are 3.3V, so if you are going to use it with ARDUINO boards with 5V level UART lines (like Arduino UNO), you need to use circuitry similar to image above, otherwise, if your ARDUINO has 3.3V UART lines (TX and RX) you can connect them directly to SOLO.

SOLO’s Setup
In This example make sure you put SOLO into Closed-Loop control mode by pressing the Piano Switch PIN NO# 5 DOWN similar to image below

The Motor
The Motor used in this example is “teknic m-2310P-LN-04K” and you can download its datasheet from here. Practically this motor is a brushless motor with sinusoidal shape BEMF and they are known also as Brushless AC or PMSM motors. This Motor has 8 Poles with an internal Encoder with 1000 physical lines pre-quad, meaning that in 1 turn rotation SOLO will read 4000 pulses generated by this motor’s encoder in quadrature manner ( 4 times more)

The Encoder and Motor Calibration
If this is the first time you are connecting this motor to your SOLO, you need to make sure at least for 1 time, you have correctly calibrated the Encoder with correct connection of the Motor’s windings to ABC outputs of SOLO, to do that you need to read the article below and do the steps there at least 1 time, and as long as your Motor or the Encoder is the same or the Encoder is not mechanically displaced with respect to the Motor ( in case of using portable encoders). All the calibrated parameters will reside in long term memory of SOLO and they will be remembered after power recycling.
Important Note for ARDUINO UNO users
In ARDUINO UNO , there is only 1 line of UART which is shared between the UART to USB convertor of ARDUINO board itself which is also used for flashing the ARDUINO UNO board as well, so whenever you want to program your ARDUINO UNO board through the ARDUINO IDE, YOU need to disconnect the UART_TX of SOLO from being connected to RX of ARDUINO UNO, by not doing this, you might get some errors in ARDUINO IDE during uploading the code.
To know more, read this post on ARDUINO website.
The Code
/*
* Title: Position Control of a Brushless Motor with Arduino and SOLO
* Author: SOLOMOTORCONTROLLERS
* Date: 2022
* Code version: 3.0
* Availability: https://github.com/Solo-FL/SOLO-motor-controllers-ARDUINO-library
* Please make sure you are applying the right wiring between SOLO and your ARDUINO
* The Code below has been tested on Arduino UNO
* The Motor used for Testings: teknic m-2310P-LN-04K
* Read more about this code here:https://www.solomotorcontrollers.com/position-control-brushless-arduino-and-solo/
*/
#include "SOLOMotorControllersUart.h"
//For this Test, make sure you have calibrated your Encoder before
//to know more please read: https://www.solomotorcontrollers.com/how-to-connect-calibrate-incremental-encoder-with-solo/
//instanciate a SOLO object:
SOLOMotorControllers *SOLO_Obj1;
//the device address of SOLO:
unsigned char SOLO_address1 = 0;
//Desired Switching or PWM Frequency at Output
long pwmFrequency = 70;
//Motor's Number of Poles
long numberOfPoles = 8;
//Motor's Number of Encoder Lines (PPR pre-quad)
long numberOfEncoderLines = 1000;
//Speed controller Kp
float speedControllerKp = 0.15;
//Speed controller Ki
float speedControllerKi = 0.03;
//Position controller Kp
float positionControllerKp = 0.12;
//Position controller Ki
float positionControllerKi = 0.02;
// Current Limit of the Motor
float currentLimit = 15.0;
// Battery or Bus Voltage
float busVoltage = 0;
// Desired Speed Limit[RPM]
long desiredSpeedLimit = 3000;
// Desired Position Reference
long desiredPositionReference = 0;
// Motor speed feedback
long actualMotorSpeed = 0;
// Motor position feedback
long actualMotorPosition = 0;
void setup() {
//In this example, make sure you put SOLO into Closed-Loop by
// pressing the Piano Switch NO# 5 DOWN. in SOLO UNO
//High Speed High Performance Baudrate (Recommended)
//Use this baudrate to have the best and real performance
//of SOLO under all conditions.
//Serial.begin(937500);
//Low Speed Low Performance Baudrate
//Use this baudrate only for devices that don't support
//937500 or 921600 baudrates.
Serial.begin(115200);
//Initialize the SOLO object
SOLO_Obj1 = new SOLOMotorControllersUart(SOLO_address1);
delay(1000);
Serial.println("\n Trying to Connect To SOLO");
delay(1000);
//wait here till communication is established
while(SOLO_Obj1->CommunicationIsWorking() == false)
{
delay(500);
}
Serial.println("\n Communication Established succuessfully!");
// Initial Configurations
SOLO_Obj1->SetOutputPwmFrequencyKhz(pwmFrequency);
SOLO_Obj1->SetCurrentLimit(currentLimit);
SOLO_Obj1->SetIncrementalEncoderLines(numberOfEncoderLines);
//select Digital Mode
SOLO_Obj1->SetCommandMode(SOLOMotorControllers::CommandMode::digital);
SOLO_Obj1->SetMotorType(SOLOMotorControllers::MotorType::bldcPmsm);
//run the motor identification
//run ID. always after selecting the Motor Type!
SOLO_Obj1->MotorParametersIdentification(SOLOMotorControllers::Action::start);
Serial.println("\n Identifying the Motor");
//wait at least for 2sec till ID. is done
delay(2000);
//Operate while using Quadrature Encoder
SOLO_Obj1->SetFeedbackControlMode(SOLOMotorControllers::FeedbackControlMode::encoders);
//Control The Position
SOLO_Obj1->SetControlMode(SOLOMotorControllers::ControlMode::positionMode);
//Speed Controller Tunings
SOLO_Obj1->SetSpeedControllerKp(speedControllerKp);
SOLO_Obj1->SetSpeedControllerKi(speedControllerKi);
//Position Controller Tunings
SOLO_Obj1->SetPositionControllerKp(positionControllerKp);
SOLO_Obj1->SetPositionControllerKi(positionControllerKi);
}
void loop() {
//set a desired Speed Limit for trajectory in RPM
desiredSpeedLimit = 5000;
SOLO_Obj1->SetSpeedLimit(desiredSpeedLimit);
// set a positive desired Position Reference
desiredPositionReference =+500000;
SOLO_Obj1->SetPositionReference(desiredPositionReference);
// wait till motor reaches to the reference
delay(3000);
actualMotorPosition = SOLO_Obj1->GetPositionCountsFeedback();
Serial.println("\n Number of Pulses passed: ");
Serial.println(actualMotorPosition);
//set a desired Speed Limit for trajectory in RPM
desiredSpeedLimit = 1500;
SOLO_Obj1->SetSpeedLimit(desiredSpeedLimit);
// set a negative desired Position Reference
desiredPositionReference =-32559;
SOLO_Obj1->SetPositionReference(desiredPositionReference);
// wait till motor reaches to the reference
delay(6000);
actualMotorPosition = SOLO_Obj1->GetPositionCountsFeedback();
Serial.println("\n Number of Pulses passed: ");
Serial.println(actualMotorPosition);
}
The Results
In This code as you can see, after setting up the essential parameters like the Motor’s number or Poles, Encoder lines and etc, we are periodically asking SOLO to spin the motor into two different positions of +50000 pulses with 5000RPM and stay there for a short time, then turning back the Motor into -32559 pulses with 1500RPM of speed.
It worth mentioning that when SOLO is in Position Mode, it counts the pulses in quadrature manner, so the feedbacks and the references are all in quadrature form, for instance in this example when we ask SOLO to traverse the shaft of the motor into +50000 pulses, if we consider the initial starting point is at zero, we are spinning the motor for (+50000 / ( 10000 * 4)) = 12.5 turns or 4500.0 degrees, so like this you can calculate the exact end point and then provide sufficient and desired reference to SOLO to reach your goal.
As an instance, while Arduino was commanding SOLO, we turned on Monitoring mode in Motion Terminal to monitor the Position controlling effect, and as you can see in Figure 1, the Motor went all the way up to near +50000 pulses with requested speed of 5000RPM, however on Figure 2 the next goal with different speed of 1500RPM and final position of -32559 is achieved.


As you can see while the position is approaching to the goal, SOLO automatically starts to reduce the speed so the position can settle softly on the final goal, however you might notice some errors in the final achieved goals, in that case you might want to tune further the Position controller gains and see how far you can improve your accuracy, clearly to have higher accuracy you can opt to select encoders with higher resolutions that will eventually increase your final position controlling quality, if you are interested you can also read about how SOLO can sustain it’s precision quality in noisy environments by reading this article.
You can read more about our SOLO Motor Controller ARDUINO Library here.
Products in this article


Related articles

SOLO Arduino Library v5.5.0: Now Supporting Arduino H7
SOLO Arduino library v5.5.0 adds support for the Arduino H7 platform, improving compatibility so developers can integrate SOLO controllers with H7 boards.

SOLO Arduino Library – Hello World
Get started with the SOLO Arduino library: set up the Arduino IDE, wire SOLO over UART or CANopen, and run your first example code to command the controller.

CANopen Brushless Motor Position Profile Control with Arduino and SOLO
Command cyclic position profiles on a brushless motor from Arduino over CANopen with SOLO: CANopen basics, wiring and system setup, full code and results.
