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

    2. Hello, that link was temporary. You can find the board published here: https://davidegironi.blogspot.com/2020/02/bldc-motor-driver-atmega-dev-board.html

  2. Sir I've been trying to implement your eclipse project, but ive not been able to generate the hex file. the eclipse doesn't identify it as a C/C++ project.

    1. Dear Hassan, you have to build a new project, include the src code, then you can compile it. You can also use VSCode as an alternative. If you google for Eclipse AVR plugin tutorial you will find how to setup Eclipse to build and compile an avrgcc projeect.


  3. Hi Engineer, it is possible to enter the password of the file

    1. Hello, there is no password to the provided source files.

  4. hello sir can you share the hex code of your project
    because i dont know programing well
    but i completely build the hardware
    can you help me?

    1. Hello, find it here: https://file.re/2021/11/19/avrlibbldc02-board2sensorless/
      The file will be automatically deleted by November 20, 2021, at 07:13 PM (CET)

  5. sir i wrote one of your hex file (avr_lib_bldc02-board2sensorless.hex) from release folder to my at mega 8A-8PU
    but the motor didn't run
    is there anyway to solve my problem
    how can i contact you

    1. Hello, please do not multi-post the same question like you have done 4 times for this.
      You can look at the I/O PIN, using a multi-channel oscilloscope. Consider building up your compiling enviroment, my hex is for ATmega running at 16Mhz, so you also have to set Fuse of the micro. Keep in mind that setting fuse in an uncorrect way may brick your micro.

  6. Hello sir.
    Thanks for your sharing. I have some question about supply voltage for bldc moter because you used cd-rom moter that it run at 3.3V or maximum 5v but your vcc for gate driver is >12V.

    1. Yes, honeslty i do not investigate the working voltage for a CDROM motor, cause it's just for a testing environment. What i can say is that it works at 12V without too much overheat.

  7. Dear sir, im interesting to try this awesome project but theres no hex file code on article, may u kind send me please, thanks in advance for ur kindness hope u stay fine keep inspiring Cheerss..

    1. ok this what i did, ive dowload the avr_lib_bldc02-board1sensorless_02.zip - extract it - open extratced folder n found main.c - than open browser - drag+drop main.c to browser n main.c readed as text - than open this web https://www.online-toolz.com/tools/text-hex-convertor.php - corverted to hex - copy her result to notepad+ - than save as "intel hexadecimal" option than it gave me a *hex extension file.. Q/a is this ok upload it via Avrdudes.. ?

    2. Hello, I suggest you to compile you code. Anyway you can find the hex here (available for 6 days): https://filebin.net/b7rzefj6ok91gcz4

    3. "ok this what i did, ive dowload the avr_lib_bldc02-board1sensorless_02.zip - extract it - open extratced folder n "... not the right way. You have to Compile it. There are a lot of tutorial online on how to compile avr-gcc, you can check it. I think the simple IDE is Eclipse IDE with AVR plug (you can search online for tutorials). A good place to ask is https://www.avrfreaks.net/ forum. Hope this helps.

    4. Ok Thanks a lot Sir.. for ur the time n kindness..

  8. This comment has been removed by the author.