Tuesday, December 3, 2019

An ATmega brushless sensorless motor driver v02

Brushless electric motor (BLDC motors) are synchronous motors that are powered by a DC electric source via an integrated inverter/switching power supply, which produces an AC electric signal to drive the motor.
For an introduction to BLDC motors, you can take look at my sensored motor driver post, here: http://davidegironi.blogspot.it/2013/09/a-simple-brushless-sensored-motor.html or you can browse the internet, you will find there is a lot of litterature about BLDC motors.

This library implements a brushless sensorless motor dirver for AVR ATmega.
To write this library I take inspiration from the library above:

This library take inspiration from:
The code i propose is written, tested and compiled using avrgcc over an ATmega8 running at 16Mhz.
I've also tested a version of this code on ATmega8 running at 8Mhz.
The aim of is project is to build a fairly simple bldc driver one can embed and easly modify in order to meet their own needs.

There are of course driver that performs better than this one, so if you are looking for best performance and you are using an ATmega micro, i suggest you to take a look at the SimonK firmware (https://github.com/sim-/tgy) or the BLHeli firmware (https://github.com/bitdump/BLHeli).

My previos driver you can find on links above does not fully implements PWM, also the brushless sensorless version to detect Zero Crossing reads the ADC voltage insted of using the internal ADC comparer.

Like the previos driver, this one implements a speed and direction (clockwise and anti-clockwise) controller.

Speed and direction can be set using the proper function, the speed have to be set using a 0 to 100 interval. 0 will stop the motor, direction has defined macro available for CW and CCW set.

In the sample main file the speed is driven by an RC input library, binded to the external interrupt input PIN.
For the test project the RC signal generator is a servo tester.
The RC reader library can be found here: http://davidegironi.blogspot.com/2018/03/an-avr-atmega-rc-control-signal-reader.html
Settings for this library can be changed on the rcin2.h file.

The bldcsetup.h file containst the main settings. As example there you can find the RC input library enabler.

This driver implements 4 PWM schemes:
  • H PWM L ON: PWM is performed on the ON channel
  • H ON L PWW: PWM is performed on the OFF channel
  • H PWM ON: PWM is performed on UWV channels every 60degrees, starting from U ON-ON
  • H ON PWM: PWM is performed on UWV channels every 60degrees, starting from V OFF-NULL
The PWM scheme is the way the power gates bridge is controlled in order to drive the motor.
One can chose the PWM scheme in the bldcsetup.h file.

The main.c file is a sample running project that use this library to run a motor.

bldcsetupmicro.h file contains specific microcontroller definition, this file should allow the implementation of this library over others than the ATmega8 microcontroller. It contains ADC registry settings and TIMER settings.

This driver can run on many board pinout, input/output PIN can be changed in the bldcsetupboard.h file.
The adc mux schema represent the order of PIN chosen as Zero Crossing detection depending on the running step.

The commutation sequence is defined in the bldc.h file.
By default it is set as follow (first group is the U bridge switch, second is the V, third is the W):
  • Step 1: 10-01-00
  • Step 2: 10-00-01
  • Step 3: 00-10-01
  • Step 4: 01-10-00
  • Step 5: 01-00-10
  • Step 6: 00-01-10
The picture below represent the commutations:

The interface with the user consists in a bounch of function that performs:
  • initialization of the library
  • speed selection or retrive
  • direction selection or retrive
  • speed retrive in RPM
  • debug function to retrive the status of the driver

Two TIMER are implemented:
  • TIMER PWM: works as the PWM emitter, it emit the commutation to the power bridge according to the selected step
  • TIMER MAIN: select the step to emit, depending on speed and direction selection. It works in three modes:
    • startup mode: perform the startup sequence of step
    • emission mode: select the commution step to run and switch to non emission mode
    • non emission mode: check the BEMF for Zero Crossing detection, once the ZC is detected, the mission mode is triggered
Two error detection strategies are implemented, both the strategies can be enabled or disabled at compile time. ZC detection error and Max Ticks between steps detection. Both the strategies trigger a motor startup when the max number of errors is reached. The ZC detection errors makes a +1 increment whenever a ZC crossing should be detected but it was not. The Max Ticks between steps makes a +1 increment whenever a step happens.

The RPM speed estimation is performed assuming the TIMER A runs at a fixed frequency.

Further improvement can be made on the whole driver, expecially the startup stage.


  • read risk disclaimer
  • excuse my bad english


  1. Thanks Davide for your kind help. I have found the schematic from this blog. But the pic is not clearly visible. Please send me the clear view of schematic if any. Eagerly waiting for your early response.

    1. Dear Abhisek, please do not multi/post. Find it here: https://file.io/1PxLhURs