https://github.com/simplefoc/Arduino-FOC
Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library
https://github.com/simplefoc/Arduino-FOC
arduino arduino-foc arduino-library arduino-simplefoc bldc-motor bldc-motor-controller bldc-motors encoder foc foc-algorithm low-cost motor stepper-library stepper-motor stepper-motor-control vector-control
Last synced: about 2 months ago
JSON representation
Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library
- Host: GitHub
- URL: https://github.com/simplefoc/Arduino-FOC
- Owner: simplefoc
- License: mit
- Created: 2019-03-04T05:49:09.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-08-11T13:39:54.000Z (2 months ago)
- Last Synced: 2025-08-13T10:05:20.719Z (2 months ago)
- Topics: arduino, arduino-foc, arduino-library, arduino-simplefoc, bldc-motor, bldc-motor-controller, bldc-motors, encoder, foc, foc-algorithm, low-cost, motor, stepper-library, stepper-motor, stepper-motor-control, vector-control
- Language: C++
- Homepage: https://docs.simplefoc.com
- Size: 10.2 MB
- Stars: 2,431
- Watchers: 81
- Forks: 620
- Open Issues: 70
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Citation: CITATION.cff
Awesome Lists containing this project
- StarryDivineSky - simplefoc/Arduino-FOC - FOC是一个基于Arduino的磁场定向控制(FOC)算法库,专为无刷直流电机(BLDC)和步进电机设计。它旨在简化电机控制,让开发者能够轻松实现高性能的电机驱动。该库提供了完整的FOC实现,包括 Clarke 和 Park 变换、PID 控制器和空间矢量脉宽调制(SVPWM)。其核心优势在于易用性和灵活性,允许用户自定义控制参数和硬件配置。项目支持多种Arduino开发板,并提供了丰富的示例代码和文档,方便用户快速上手。通过精确控制电机电流和电压,Arduino-FOC能够实现平稳、高效的电机运行,适用于机器人、自动化和电动车辆等领域。它是一个开源项目,鼓励社区参与和贡献,共同完善电机控制技术。该项目特别适合那些希望在Arduino平台上实现高性能电机控制,但又不想深入研究复杂控制理论的开发者。 (硬件_其他 / 资源传输下载)
README
# SimpleFOClibrary - **Simple** Field Oriented Control (FOC) **library**
### A Cross-Platform FOC implementation for BLDC and Stepper motors
based on the Arduino IDE and PlatformIO[](https://github.com/simplefoc/Arduino-FOC/actions/workflows/arduino.yml)
[](https://github.com/simplefoc/Arduino-FOC/actions/workflows/stm32.yml)
[](https://github.com/simplefoc/Arduino-FOC/actions/workflows/esp32.yml)
[](https://github.com/simplefoc/Arduino-FOC/actions/workflows/rpi.yml)
[](https://github.com/simplefoc/Arduino-FOC/actions/workflows/samd.yml)
[](https://github.com/simplefoc/Arduino-FOC/actions/workflows/teensy.yml)
[](https://github.com/simplefoc/Arduino-FOC/actions/workflows/arduino_mbed.yml)


[](https://www.ardu-badge.com/badge/Simple%20FOC.svg)
[](https://registry.platformio.org/libraries/askuric/Simple%20FOC)
[](https://opensource.org/licenses/MIT)
[](https://joss.theoj.org/papers/4382445f249e064e9f0a7f6c1bb06b1d)We live in very exciting times 😃! BLDC motors are entering the hobby community more and more and many great projects have already emerged leveraging their far superior dynamics and power capabilities. BLDC motors have numerous advantages over regular DC motors but they have one big disadvantage, the complexity of control. Even though it has become relatively easy to design and manufacture PCBs and create our own hardware solutions for driving BLDC motors the proper low-cost solutions are yet to come. One of the reasons for this is the apparent complexity of writing the BLDC driving algorithms, Field oriented control (FOC) being an example of one of the most efficient ones.
The solutions that can be found on-line are almost exclusively very specific for certain hardware configuration and the microcontroller architecture used.
Additionally, most of the efforts at this moment are still channeled towards the high-power applications of the BLDC motors and proper low-cost and low-power FOC supporting boards are very hard to find today and even may not exist.
Therefore this is an attempt to:
- 🎯 Demystify FOC algorithm and make a robust but simple Arduino library: [Arduino *SimpleFOClibrary*](https://docs.simplefoc.com/arduino_simplefoc_library_showcase)
- Support as many motor + sensor + driver + mcu combinations out there
- 🎯 Develop modular and easy to use FOC supporting BLDC driver boards
- For official driver boards see [SimpleFOCBoards](https://docs.simplefoc.com/boards)
- Many many more boards developed by the community members, see [SimpleFOCCommunity](https://community.simplefoc.com/)> NEXT RELEASE 📢 : SimpleFOClibrary v2.3.5
>
> - ESP32 bugfix
> - after the low-level API changes in the Arduino-ESP32 core [PR447](https://github.com/simplefoc/Arduino-FOC/pull/447)
> - Pin is not configured [PR458](https://github.com/simplefoc/Arduino-FOC/pull/458)
> - C6 MCPWM bugfix [PR440](https://github.com/simplefoc/Arduino-FOC/pull/440)
> - New fuctionality
> - HybridStepperMotor added to the main library [PR457](https://github.com/simplefoc/Arduino-FOC/pull/457) - [see in docs](https://docs.simplefoc.com/steppermotor)
> - Motor characterisation (phase resistance and inductance) [PR436](https://github.com/simplefoc/Arduino-FOC/pull/436) - [see in docs](https://docs.simplefoc.com/bldcmotor#how-can-i-measure-the-phase-resistance-and-inductance)
> - SAMD21 support for low-side current sensing [PR479](https://github.com/simplefoc/Arduino-FOC/pull/479)
> - RP2350 support [PR435](https://github.com/simplefoc/Arduino-FOC/pull/435) [PR468](https://github.com/simplefoc/Arduino-FOC/pull/468)
> - STM32
> - New driver code [PR442](https://github.com/simplefoc/Arduino-FOC/pull/442)
> - Low-side current sensing support for H7 family [PR460](https://github.com/simplefoc/Arduino-FOC/pull/460)
> - Docs
> - Hybrid stepper motor example [see in docs](https://docs.simplefoc.com/stepper_control_shield)
> - Sensorless FOC example [see in docs](https://docs.simplefoc.com/sensorless_foc_nucleo_example)
> - A short guide to synchronous loop - [see in docs](https://docs.simplefoc.com/real_time_loop)
> - See the complete list of bugfixes and new features of v2.3.5 [fixes and PRs](https://github.com/simplefoc/Arduino-FOC/milestone/12)## Arduino *SimpleFOClibrary* v2.3.5
This video demonstrates the *Simple**FOC**library* basic usage, electronic connections and shows its capabilities.
### Features
- **Easy install**:
- Arduino IDE: Arduino Library Manager integration
- PlatformIO
- **Open-Source**: Full code and documentation available on github
- **Goal**:
- Support as many [sensor](https://docs.simplefoc.com/position_sensors) + [motor](https://docs.simplefoc.com/motors) + [driver](https://docs.simplefoc.com/drivers) + [current sense](https://docs.simplefoc.com/current_sense) combination as possible.
- Provide the up-to-date and in-depth documentation with API references and the examples
- **Easy to setup and configure**:
- Easy hardware configuration
- Each hardware component is a C++ object (easy to understand)
- Easy [tuning the control loops](https://docs.simplefoc.com/motion_control)
- [*Simple**FOC**Studio*](https://docs.simplefoc.com/studio) configuration GUI tool
- Built-in communication and monitoring
- **Cross-platform**:
- Seamless code transfer from one microcontroller family to another
- Supports multiple [MCU architectures](https://docs.simplefoc.com/microcontrollers):
- Arduino: UNO R4, UNO, MEGA, DUE, Leonardo, Nano, Nano33 ....
- STM32
- ESP32
- Teensy
- many more ...
![]()
![]()
## Documentation
Full API code documentation as well as example projects and step by step guides can be found on our [docs website](https://docs.simplefoc.com/).
## Getting Started
Depending on if you want to use this library as the plug and play Arduino library or you want to get insight in the algorithm and make changes there are two ways to install this code.- Full library installation [Docs](https://docs.simplefoc.com/library_download)
- PlatformIO [Docs](https://docs.simplefoc.com/library_platformio)### Arduino *SimpleFOClibrary* installation to Arduino IDE
#### Arduino Library Manager
The simplest way to get hold of the library is directly by using Arduino IDE and its integrated Library Manager.
- Open Arduino IDE and start Arduino Library Manager by clicking: `Tools > Manage Libraries...`.
- Search for `Simple FOC` library and install the latest version.
- Reopen Arduino IDE and you should have the library examples in `File > Examples > Simple FOC`.#### Using Github website
- Go to the [github repository](https://github.com/simplefoc/Arduino-FOC)
- Click first on `Clone or Download > Download ZIP`.
- Unzip it and place it in `Arduino Libraries` folder. Windows: `Documents > Arduino > libraries`.
- Reopen Arduino IDE and you should have the library examples in `File > Examples > Simple FOC`.#### Using terminal
- Open terminal and run
```sh
cd #Arduino libraries folder
git clone https://github.com/simplefoc/Arduino-FOC.git
```
- Reopen Arduino IDE and you should have the library examples in `File > Examples > Simple FOC`.## Community and contributing
For all the questions regarding the potential implementation, applications, supported hardware and similar please visit our [community forum](https://community.simplefoc.com) or our [discord server](https://discord.gg/kWBwuzY32n).
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!
### Github Issues & Pull requests
Please do not hesitate to leave an issue if you have problems/advices/suggestions regarding the code!
Pull requests are welcome, but let's first discuss them in [community forum](https://community.simplefoc.com)!
If you'd like to contribute to this project but you are not very familiar with github, don't worry, let us know either by posting at the community forum , by posting a github issue or at our discord server.
If you are familiar, we accept pull requests to the dev branch!
## Arduino code example
This is a simple Arduino code example implementing the velocity control program of a BLDC motor with encoder.NOTE: This program uses all the default control parameters.
```cpp
#include// BLDCMotor( pole_pairs )
BLDCMotor motor = BLDCMotor(11);
// BLDCDriver( pin_pwmA, pin_pwmB, pin_pwmC, enable (optional) )
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11, 8);
// Encoder(pin_A, pin_B, CPR)
Encoder encoder = Encoder(2, 3, 2048);
// channel A and B callbacks
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}void setup() {
// initialize encoder hardware
encoder.init();
// hardware interrupt enable
encoder.enableInterrupts(doA, doB);
// link the motor to the sensor
motor.linkSensor(&encoder);
// power supply voltage [V]
driver.voltage_power_supply = 12;
// initialise driver hardware
driver.init();
// link driver
motor.linkDriver(&driver);// set control loop type to be used
motor.controller = MotionControlType::velocity;
// initialize motor
motor.init();
// align encoder and start FOC
motor.initFOC();
}void loop() {
// FOC algorithm function
motor.loopFOC();// velocity control loop function
// setting the target velocity or 2rad/s
motor.move(2);
}
```
You can find more details in the [SimpleFOC documentation](https://docs.simplefoc.com/).## Example projects
Here are some of the *Simple**FOC**library* and *Simple**FOC**Shield* application examples.## Citing the *SimpleFOC*
We are very happy that *Simple**FOC**library* has been used as a component of several research project and has made its way to several scientific papers. We are hoping that this trend is going to continue as the project matures and becomes more robust!
A short resume paper about *Simple**FOC*** has been published in the Journal of Open Source Software:
SimpleFOC: A Field Oriented Control (FOC) Library for Controlling Brushless Direct Current (BLDC) and Stepper Motors.
A. Skuric, HS. Bank, R. Unger, O. Williams, D. González-Reyes
Journal of Open Source Software, 7(74), 4232, https://doi.org/10.21105/joss.04232If you are interested in citing *Simple**FOC**library* or some other component of *Simple**FOC**project* in your research, we suggest you to cite our paper:
```bib
@article{simplefoc2022,
doi = {10.21105/joss.04232},
url = {https://doi.org/10.21105/joss.04232},
year = {2022},
publisher = {The Open Journal},
volume = {7},
number = {74},
pages = {4232},
author = {Antun Skuric and Hasan Sinan Bank and Richard Unger and Owen Williams and David González-Reyes},
title = {SimpleFOC: A Field Oriented Control (FOC) Library for Controlling Brushless Direct Current (BLDC) and Stepper Motors},
journal = {Journal of Open Source Software}
}```