Link

Supported microcontrollers

Arduino SimpleFOClibrary supports Arduino, STM32 and ESP32 devices off-the-shelf, using Arduino IDE, and with small modifications many more…
This library has been ported to Maple boards, Arduino DUEs and even to strom32 BGC v1.31, so I hope to be able to support more boards in future. 😃

Arduino support

BoardNameSpecificationsLinkPrice
Arduino UNOATMega328
- 5V logic
- 6 PWMs
- 2 interrupts
- 16Mhz
Ebay
Arudino Store
5€
25€
Arduino MegaATMega2560
- 5V logic
- 15 PWMs
- 6 interrupts
- 16Mhz
Ebay
Arduino Store
10€
35€
Arduino NANOATMega328
-5V logic
- 6 PWMs
- 2 interrupts
- 16Mhz
Ebay3€

Arduino SimpleFOClibrary has stared as an Arduino UNO library and it has been optimized to be used with these types of devices. But it is not restricted to the Arduino UNOs, you will be able to run it on Arduino MEGA and basically any board with ATMega328 chip or ATMega2560. Such as Arduino Nano, Arduino pro-mini and similar.

BoardNameSpecificationsLinkPrice
HMBGC V2.2ATMega328
- 5V logic
- 6 PWMs
- 0 interrupts
- No SPI
- 16Mhz
- 2x BLDC driver
Ebay20€
BGC 3.0ATMega328
- 5V logic
- 6 PWMs
- 0 interrupts
- 16Mhz
- 2x BLDC driver
Aliexpress10€

One of the interesting example boards for beginners are the gimbal controller boards based on the ATMega328 chips that usually have integrated BLDC driver hardware for running two motors. Their performance is limited but they are great initial setup.

STM32 boards support

BoardNameSpecificationsLinkPrice
Nucleo-64 boards(ex. Nucleo F446RE)
- 3.3V logic
- 20 PWMs
- all pins interrupts
- 180Mhz
Mouser15€
Bluepill(ex. STM32F103C8)
- 3.3V logic
- 15 PWMs
- all pins interrupts
- 72Mhz
Ebay5€

The STM32 boards are supported using the STM32Duino package, it is completely open-source and can be installed directly through the Arduino Board Manager. Please check the STM32Duino wiki to see a detailed guide how to install the package and all its functionalities.

ESP32 boards support

BoardNameSpecificationsLinkPrice
ESP32(ex. NodeMCU)
- 3,3V / 5V logic
- 16 PWMs
- all pins interrupts
- 240MHz
- Wifi + Bluetooth
Ebay10€

ESP32 boards are supported using arduino-esp32 package, it is open source software provided by the espressif. You can download the support package through the Arduino Board Manager by searching for esp32 or follow the instruction of their webpage package installation.

There is just one more thing you need to do in order to s setup the ESP32 boards with the SimpleFOClibrary. Since ESP32 support in Arduino IDE is quiet recent, there are some known bugs. You can find the github issue here. One of them is closely related to the MCPWM class which we need to use to have smooth motor control with the ESP32. In order to resolve this bug you just need to replace one header file in the arduino-esp32 package, file mcpwm.h.

This file is usually placed in (Windows):

C:\Users\(you user name)\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\tools\sdk\include\driver\driver

Navigate to this directory and replace the file mcpwm.h with the file that you can download beneath:

Now your ESP32 code should be compiling and you are ready to go!

Choosing the microcontroller

Even though all the MCUs from the table above (and many more) are supported in the library and all of the will work with most of the BLDC motors+BLDC driver+sensor combinations, their performance will not be the same. So here is a quick guide how to choose which MCU to use.

MCUloopFOC() + move() - encoderloopFOC() + move() - magnetic sensor SPIloopFOC() + move() - magnetic sensor I2C
HMBGC V2.2800us (ipr = 0), ~10ms (ipr > 10000)(doesn’t support SPI)1100us
Arduino UNO800us (ipr = 0), ~10ms (ipr > 20000)1200us1100us
Stm32 Bluepill200us (ipr = 0), ~1ms (ipr > 50000)300usto be tested
Stm32 Nucleo-64100us (ipr = 0), ~500us (ipr > 50000)200usto be tested
ESP32to be testedto be testedto be tested

*ipr = interrupt callbacks per second.

In the table above you can a comparison of the execution times of the FOC loop for different MCUs. When you are deciding which MCU to use with your project please make sure that your loop execution time loopFOC() + move(), in the worst case, will not be greater 3-4ms. And for optimal performance your loop time should be under 2ms. Make sure to account for multiple motors.

Gimbal controllers

Gimbal controllers are the most simple and surely the cheapest solution for running FOC algorithm with your gimbal motor. They are perfect for smooth position/velocity controlling two BLDC motors with sensors if you don’t have high constraints on dynamics. Their main disadvantage is that they use all the external interrupt pins for PWM signals and therefore you cannot access them from outside. That would mean that even if you only need one motor (3PWMs) you will still not be able to use pin 2 and 3 for encoder A and B signals. This means, if you are planing to use encoders with these boards you will need to use software interrupts. The good news is that this will work, the bad news is that the performance of counting encoder signals will be impaired. So I would suggest you to use Magnetic sensors with communication interface (SPI, I2C…) with these boards if possible.

Don’t let this discourage you from using the gimbal controllers with FOC, just be aware of possible side-effects when deciding which motor and sensor to use.

Make sure your gimbal controller has communication interface pins you need, available before buying it.

Arduino MCUs

Arduino devices, such as UNO,MEGA,NANO and similar, are probably the most commonly used microcontrollers there is, and therefore probably with this library as well. The simplicity of using these boards si incomparable. If you are planning to run this library with the Arduino device I would certainly suggest you to think about using Magnetic sensors instead of encoders. Encoders are highly inefficient sensors (at least their implementation for Arduino UNO & MEGA) and due to constant counting the interrupt signals of the encoder produces a large difference of execution time depending on velocities you are driving your motor.

Encoder CPR: Rule of thumb for Arduino UNO/MEGA

For Arduino UNO, the maximum number of pulses/second should not exceed 20,000. After this value it start to have execution issues. Please take this in consideration when choosing the encoder and especially if using more than one motor.

Example

If your CPR value is 10000, you will be able to spin your motor with max velocity 120rpm - 2 rotations/second

Additionally Arduino UNO has only two encoder interrupt pins and if you are running two motors with encoders on your Arduino UNO you will be forced to use software interrupt callback which introduce additional augmentation in execution times. Arduino MEGA has 6 interrupts and you should not have this problem.

This library will enable you to use Arduino UNO/MEGA as your FOC brain and you can still do a lot of cool stuff with it even with two motors. Just be aware of the role of thumb if you are using the encoders.

Stm32 devices

Stm32 devices are probably the best choice for the FOC implementation MCU. They are very powerful and have many external interrupt pins. They don’t loose too much performance due to counting and have much lower loop times making the FOC algorithm much smoother. Stm32 Bluepill can run up to 4 BLDC motors without a problem and Nucleo-64 can run 6+.

The biggest problem of using the STM32 boards in the community is the complexity of programming these devices. But since they have been integrated into the Arduino IDE even this is not a problem any more. Arduino SimpleFOClibrary code for stm32 devices is exactly the same as for Arduino UNO except the different pin numbers. Therefore I strongly urge you to consider using these devices in your projects because the results are awesome. 😃

Arduino SimpleFOCShield is completely compatible with Nucleo-64 devices. And from the shield version 1.3.1 you will be able to stack 2 of the shields with the Nucleo boards as well.

ESP32 devices

ESP32 devices are very interesting choice for running this library. They have superb communication interfaces and they move the user interaction with the motor to the next level. ESP32 devices are, in theory, capable of running 4 BLDC motors at the same time with this library. ANd their performance will be much better than the one of the Arduino devices. Especially since they don’t have problems with external interrupt limitations.

There are two main problems when using ESP32 boards with for real time motor control.

  • ESP32 boards were not originally designed for precise real-time control tasks, they have exceptional communication capabilities, therefore sometimes you can have some strange problems due to this fact. But in 99% of situations this board will perform exceptionally well, it is just if you were planning bring it to its limits when the strange things can happen.
  • The other problem of this board is the pinout limitations. Make sure, if you are new with the ESP32, that you watch this YouTube video. On startup some of the GPIOs have to be in specific states in order to the ESP32 to boot normally. But this is not a big problem once you get used to it!

This board has a lot of benefits and it seems like we will see much more of it in the domain of real-time motor control in future.

SimpleFOC Community

If you have ported the library to another device or you are searching for help to port it to some specific device don’t hesitate to post in community forum

It is always helpful to hear the stories/problems/suggestions of people implementing the code and you might find a lot of answered questions there already!