Sunday, September 15, 2013

A simple brushless sensored motor driver for AVR Atmega



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. Additional electronics control the inverter output amplitude and waveform (and therefore percent of DC bus usage/efficiency) and frequency (i.e. rotor speed). Because the controller must direct the rotor rotation, the controller requires some means of determining the rotor's orientation/position (relative to the stator coils). Some designs use Hall effect sensors or a rotary encoder to directly measure the rotor's position, others measure the back EMF in the undriven coils to infer the rotor position, eliminating the need for separate Hall effect sensors, and therefore are often called sensorless controllers.


The following explanation is taken from "AVR443: Sensor-based control of three phase Brushless DC motor" sheet.
Theory of operation: A three phase BLDC consists of a Stator with a number of coils. The fundamental three phase BLDC motor has three coils. Usually the three coils are referred to as U, V and W. In many motors the fundamental number of coils are replicated to have smaller rotation steps and smaller torque ripple. The rotor in a BLDC motor consists of an even number of permanent magnets. To simplify the explanation of how to operate a three-phase BLDC motor a fundamental BLDC with only three coils is considered. To make the motor rotate the coils are energized (or “activated”) in a predefined sequence, making the motor turn in one direction, say clockwise. Running the sequence in reverse order the motor run in the opposite direction. The direction of the current determines the orientation of the magnetic field generated by the coil. The magnetic field attracts and rejects the permanent magnets of the rotor. By changing the current flow in the coils and thereby the polarity of the magnetic fields at the right moment – and in the right sequence – the motor rotates. Alternation of the current flow through the coils to make the rotor turn is referred to as commutation. A three-phase BLDC motor has six states of commutation. When all six states in the commutation sequence have been performed the sequence is repeated to continue the rotation. The sequence represents a full electrical rotation. For motors with multiple poles the electrical rotation does not correspond to a mechanical rotation. A four-pole BLDC motor uses two electrical rotation cycles to per mechanical rotation. When specifying the number of Rotations Per Minute subsequently, the number of electrical rotations is referred to unless otherwise mentioned. The most elementary commutation driving method used for BLDC motors is an on-off scheme: A coil is either conducting (in one or the other direction) or not conducting. The strength of the magnetic field determines the torque and speed of the motor. For BLDC motors the commutation control is handled by electronics. The simplest way to control the commutation is to commutate according the outputs from a set of position sensors inside the motor. Usually Hall sensors are used, but even back EMF can be used to detect the stator position. A common commutation is the one represented by the image below.


For this project, I've implemented a simple brushless sensored motor driver for AVR Atmega. The code i propose it's not perfect, and can be improved, but for the needs i had it works.

Using this library, the motor can be controlled in speed and direction (clockwise and anti-clockwise).
The running step for the motor are defined as default, anyway user can change it to fit any motor. Below you can find the commutation sequence (for clockwise and anti-clockwise rotation) i've used:


User has to setup the port used to read the hall sensor, and the port to control the FET status. Also the timer interrupt and prescaler should be setup for different running frequency.

A sample main routine is provided to help you understand how the library works.

To test this project i've used a brushless motor taken from a cd-rom, it is connected to a LM339 voltage compartor to build an encloder. You can find connection circuit below.



The power drive stage for the brushless motor is build using IRF640 mosfet and IR2101 high and low side driver. The complete schematics below:


Note that even a back EMF wiring are present there, if you just want to use hall sensor you can skip those connections. Also an external crystal is connected just to test the circuit at 16Mhz, even if tests are done at 8Mhz.

Setup parameters are contained in bldcsensored.h

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 8MHz.

Code
Notes
  • read risk disclaimer
  • excuse my bad english

230 comments:

  1. Great! I wonder about motors without hall effect sensors - you can measure bemf using PC0-PC2 of mega in your schematics, but what is the purpose of PC5?

    ReplyDelete
    Replies
    1. the schematics above is for bemf readings too, wait two weeks and i will post a sensorless version ;)
      for sensored one you can skip PC0..2 + PC5 and resistors attached.

      Delete
  2. Great job! I'm studying Mechatronics, but in fact I haven't made any project yet. Do you think BLDCM is good for start-up? I've had it in mind before, but with your help I think, I could eventually do this.

    ReplyDelete
    Replies
    1. a bldc motor, and driver is not so simple to build, it will be a good project, but i do not know if could be a startup project due to it's difficulties.
      anyway if you start this, let me know about your progress ;)

      Delete
  3. how did you done via's in the PCB?

    ReplyDelete
    Replies
    1. i'm using matrix protorype board, just board with may holes.
      using solder, or components legs, i build perpendicular via.

      Delete
  4. Hi man, can you tell me the spec of brushless motor that you used?

    Thanks man

    ReplyDelete
    Replies
    1. it's an old floppy driver brushelss motor, i've not specs,
      i've also try it with other brushless motors for a friend of mine, but i've no specs even for those motors, no serial, no model, they are all recovered.

      Delete
    2. ok got it, my BLDC is about 24V (0,9A continous ampere, and 4A starting ampere). Can i use it to my BLDC?

      Thanks a lot

      Delete
    3. this should works, as you can see, the power stage is built on IRF640, which has +-20V Vgs maxm and 18A of continuos drain current. but i'm not expert in power stage, so, control heat temperature of the mosfets during your test, to prevent damages.

      Delete
    4. Sorry for late replying.
      Happy new year man.

      Ok got it, thanks.
      Can i change the IR2101 with another component?
      because so hard for me to find it in my country.

      Thanks a lot

      Delete
    5. yes, just use other high and low side driver.
      if you google around you can find other design for brushless power stage you can use.

      Delete
  5. Man, tell me about the type of Atmega 8 too.

    Thanks

    ReplyDelete
    Replies
    1. Mine was a standard ATmega8 PDIP, but every ATmega8 should work, it should work event on other atmega or AVR micro, but somethings needs to be ported to make it use with other micro.

      Delete
  6. hi,what was the compiler you used ?

    ReplyDelete
  7. Davide,
    I tried to compile this project, Using WinAVR-20100110 and eclipse kepler SR2, I got this error "bdlc.elf section .text will not fit in region text" any ideas, otherwise, awesome project!

    ReplyDelete
    Replies
    1. Hello,
      the complied hex does not fit your microcontroller memory.
      Check that libmath is linked. (add -m to avr c linker), it should fit on ATmega8, with libmath linked.

      Delete
  8. Hi Davide,

    I built the circuit, programmed the Atmega8, hooked up the motor, but no go. Where can I find the code for the sensorless version?

    ReplyDelete
    Replies
    1. this is the sensorless version: http://davidegironi.blogspot.it/2013/09/a-simple-brushless-sensorless-motor.html
      for the sensored one, at first my suggestion is to debug your hardware, the power stage, and the connection, maybe with other firmware.
      then, if the hardware works, debug your software.

      Delete
  9. hello Sir, thanks for the good offer :)
    I have a question please.
    why did you use these two function acd_getrealvref() and adc_getresistence ??
    i think you need to measure the current!!
    Thanks in advance.

    ReplyDelete
    Replies
    1. Hello, thank you for your feedback.
      I do not use the two function you write here. Those two function are part of the adc library i use. But for this project i never call none of them.

      Delete
  10. hi
    i need a file hex for atmega8 , because i can't compile from avr GCC .thanks

    ReplyDelete
    Replies
    1. Send me an email, i will replay with the ATmega8 hex attachment.

      Delete
    2. my mail anthony300688@gmail.com

      i need a file hex for sensorles and a file hex for sensored .
      thank o much

      Delete
    3. hep hex Amega8 hex mail coi3lan@gmail.com thanks

      Delete
    4. my name rangga. send me for hex atmega8 hex mail rangga.andika96@gmail.com

      Delete
    5. Hello, please find it here, link expires in 2 days https://expirebox.com/download/0f48a843765f21101853e8e4e4253664.html

      Delete
  11. seem it has bug, It have to stop when Pot 5K has value = 0, I tested it , it always run when i power for circuit although i turn Pot min

    and if signal from hall sensor wrong , it don't stop , continues driver FET wrong then burn FET .

    ReplyDelete
    Replies
    1. It is not the bug, if you look at the code, that's the way it is works, when the pot is set to zero, it continues running at the lowest speed. Also, connecto sensor in the right way, or the driver may overheat mosfet. Remember that this is a basic driver, you can edit the code, and compile it again with your changes. For more advanced driver the SimonK can be your choice.

      Delete
  12. i know firmware Simonk for multicopter. But it is sensorless and have to use a circuit create signal PPM to it work.

    your firmware is simple and easy to use . You can change code to it stop when POT is zero ? i'm not a coder, i can't. Plz

    ReplyDelete
    Replies
    1. Simonk will work even attached to sensored motor, just do not attach your sensor wires.
      Regard the code change, I'm sorry but you have to do it by yourself. Here i can help you, but i can not make you work, image if i do this for you, i have to do this for all the people who ask...

      Delete
    2. Ohh , no .I need use motor have sensor,

      Plz think again , any information that you can help me , send to my mail .
      thanks

      Delete
    3. Ok.
      Check that changes http://pastebin.com/fXRP0Fdu
      Made changes to the main.c, just changed the main loop to stop the motor when the posts is under a certain threshold.
      Than compile. For compile, check some online tutorial about avrgcc compiling, i suggest you to take a look to Eclipse and the AVR plugin for Eclipse, to me it is the simpler way to compile an uploade avr code using avrgcc.

      Delete
  13. Greetings , sir. I need .hex file for simulation in Proteus. Can you help me out?
    Thank you.

    ReplyDelete
    Replies
    1. For compile, check some online tutorial about avrgcc compiling, i suggest you to take a look to Eclipse and the AVR plugin for Eclipse, to me it is the simpler way to compile an uploade avr code using avrgcc.
      If you just want the hex, i can give you a compiled hex for Atmega8 @ 8Mhz, this case, tell me your email address.

      Delete
    2. Sir, I am new to avr . I was using arduino before. The zip file that you have provided has got 4 files and out of those, which one should I use for simulation? Then what about the other files? Do I need to copy them to some other folder or so?
      Thank You

      Delete
    3. All the files are needed for the project to run. You just have to compile those files, and link them. You can use makefile or any other avrgcc compatible ide.
      As a first project on avr, i must admit that a brushless driver is pretty complex, you'd better before setup your toolchain, and play with your toolchain, blink a led, test ADC, test timers.. and so on.. then you could pass to something more complex, but this is just my opinion.

      Delete
  14. Sir, I got the program compiled in AVR studio 5.1 to get the hex file but when I used your circuit and given .hex file to simulate in proteus. I am getting the error message as timestep too small. What can I do for the same?
    Thanks

    ReplyDelete
    Replies
    1. The timestep too small is a proteus message, i suppose you have to ask proteus forum this question.

      Delete
    2. Hi People!
      Varun can you please help me in compiling this project on AVR studio. I am using AVR studio 7.
      Thanks

      Delete
    3. Dear Davide!
      Thanks for sharing the knowledge. Can you send me the .hex file of sensor based for internal oscillator of 8 mega Hz.
      Email: 13mseemansari@seecs.edu.pk

      Delete
    4. Thanks for the email Davide!

      1) Please can you tell me for what purpose you are using uart for because when I run the hex in Proteus it warns me that "[ AVR USART ] RX frame error".

      2)Secondly I am trying to compile your code using Atmega Studio 7, So that I can shift to Atmega48 (As I have them available with me right now). Can you please suggest me what should I change in the code so that I can also compile it on Atmega48. Atmega48 has same pin configuration as Atmega8.

      3) When I was compiling for Atmega8 I got these errors.
      ---- attempt to use poisoned "SIG_UART_RECV"
      ---- attempt to use poisoned "SIG_UART_DATA"

      Corresponding to lines in uart.h:
      --- #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV
      ---#define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA

      I replaced these lines with the following ones and I got away with the errors:

      ---#define UART0_RECEIVE_INTERRUPT USART0_RX_vect
      ---#define UART1_RECEIVE_INTERRUPT USART1_RX_vect

      Did I preformed any thing wrong while changing the syntax. your suggestions are needed.

      Thanks.

      Delete
    5. One thing More I should tell you the specifications of my BLDC motor also:

      -- Out runner.
      -- 12 pole winding and 8 pole magnets. Means 4 electrical cycles = 1 mechanical cycle.
      -- 12 Volts 2.5 - 3 A Ratting (Ratted at 330 Rpm with Ratted Load)
      -- Winding is 3 phase Y connection (Star) with 3 hall effect sensors.

      The things that interests me is winding technique used in this motor which is quite different to the literature available on internet. I wish I can attach the picture of winding but Let me try by phrasing it to you:

      Assume 1,2,3,4,5,6,7,8,9,10,11 and 12 are winding poles numbering. four numbers share same winding. {1,4,7,10} 1 being the start (Lets call it "U") and 10 being the common end for Y connection, {12,3,6,9} 12 being the start (Lets call it "V") and 9 being the common end for Y connection, {11,2,5,8} 11 being the start (Lets call it "W") and 8 being the common end for Y connection.
      You must be thinking what is different in this, it is same as a three phase winding of BLDC should be. The difference is that each winding clockwise wound. Means 1,2,3........,11,12 are all clockwise wound.

      Normally if 1 winding number is clockwise then 4 should be anticlockwise and 7 CW and then 10 ACW. But that is not the case.

      My power stage burns out every time I try to run this BLDC. Please need your suggestions that how can I come across with this problem.

      Thanks

      Delete
    6. Hello,

      I'm using UART for debug purpose, you could even not use it.

      I've always used avrgcc, there will be some compiler warning or error on Atmega Studio 7 that could help you find out what problems are you having compiling it, check that errors and debug the code according to your compiler.

      About the SIG_UART_RECV error, you are using an out of date interrupt name, see you compiler documentation for the updated register name (es exaple, for avr-libc you can look here: http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html)

      Unluckly I've not much time to investigate on your motor winding. My suggestion is at first to check the BLDC driver with "standard" motor, then you could check with your motor.
      As a note: this library does not implement PWM driver, for a PWM driver i suggest you to check the SimonK firmware.

      Delete
    7. Thanks Davide for your prompt response. :D

      Delete
  15. hi God bless the mans like you that help people :-)
    I live in Iran and i could't find IR2101.can i replace it whit some thing else? please tell me lot of Thanx

    ReplyDelete
    Replies
    1. There are a few way to drive MOSFET, the one i proposed here use the IR2101. But you could also use other High and Low Side Driver that could work.Something like the FAN7382 has similar characteristics. You could also change the power zone from my schematics, if you look here you will find usefull information about the power driver for bldc motors: https://code.google.com/p/qut-brushless-controller/wiki/mosfetdrive

      Delete
  16. Hi again,i cant work with WINAVR,i just tried CODEVISION.this is my E mail:
    watersez4000@gmail.com
    please send me the HEX file for both sensored and sensorless .lot of Thanx again

    ReplyDelete
    Replies
    1. thanx a lot.hope to i be useful for you too :-)in any way :-)

      i invite you to my house in Iran and your wellcome :-)

      Delete
    2. I really appriciate this. Thank you ;)

      Delete
    3. hi davide.what if i want to control the speed with an external pulse?
      to use this kind of motor in a robot?

      Delete
    4. You can do that think, just write your own input library, get the speed control, ad set the motor speed by using the set speed method provied with this library.

      Delete
    5. but the problem is here that i don't even know what i expect from the micro.please tell me what i should do with micro? what must be the out puts from micro to the IR2101 ic :-(

      Delete
    6. The IR2101 is just a high low side driver, what controls the motor, is what the microcontroller sends to it. If you would like a PWM input, you can use any free microcontroller port, and program it as PWM input port, but you have to write your own code. If you take a look to avrfreaks forum, ther you can find some posts about how to threat PWM input.

      Delete
  17. i can write my own program in CODEVISION with C, if i know the performance.what are the pulses to give to IR2101 .both HIN and LIN .lot of thanx

    ReplyDelete
    Replies
    1. The pulse to IR2101 are the 6 phase pulses (UVW), that you can see above in this post. Also, a better driver should use PWM to drive the motor. I've another driver library which works with PWM and soon i will post it.
      Keep in mind that a brushless driver is not a simple think, or at least, is not the think i will suggest someone to start with using microcontrollers.
      There are a few white papers, and tutorial that teach how a bldc motor works. At first read that ones.
      Anyway, for a much performance driver, my choice in the Atmel world, would be use the SimonK assembler driver, mounted on an ESC board. And other microcontroller to govern the ESC board and so the motor.

      Delete
    2. hi.so much thanx .i've done this before .here is the video.take a look at it: http://youtu.be/gppPOxElImY

      but as i said in video,the motor does't spin properly ,and also i want to use MOSFET.i made a simple pulses that are only different in 120 degrees and nothing similar to your pulses :-(((

      Delete
    3. the pulses i gave to motor coils are exactly the pulses in this page that you named them "H1,H2,H3"
      but i think these are for the hall efect sensors :-(

      Delete
    4. ha .......i got it.the idea is that to make a SINE wave with DC current as much as it's possible :-) right?

      Delete
    5. Nice work.
      Power stage: as you tell, it is better to use MOSFET for your power stage, you can even use a simple NPN to drive the mosfet if you can get IR2101 or other high low side driver.
      Pulse send: MOSFET has to be set on/off at correct time, so you have to read the motor position, and then power the mosfet respect that position. If you simply use a timer and do not read the motor position, that will not make the motor spin well.
      For a SINE wave, you need: a) turn the MOSFET on and off at right time respect the motor position, so get the motor position, and emit the correct phase number; b) do that in PWM, i.e. do not turn on one MOSFET and leave it on until the next phase, but use PWM even for on/off driving of the FETs.
      Said that, the driver i propose here are simple driver, that do not use PWM, so you will not get a good sine wave from my driver. The other one i've worked in, but i've not released yes, does PWM.
      Also note that i'm not an expert on bldc motor, if you are looking for a better driver, you should look at an RC community, because driver for the ESC board are developed for performance on RC, like the SimonK one, or wii-esc, or the Takao Shimizu code for arduino.

      Delete
  18. Hello,
    I am using Atmega32 to control the speed of a motor which i coded in atmel studio and simulated in proteus. I now need to drive a 3 phase motor for which I guess your driver circuit will be useful. I have used 2 buttons to increase and decrease the speed. I am somewhat confused with the coding. I need to sense the hall sensors and then drive the motor at a speed which can be altered. I have used variables in the on and off state currently to vary the speed which is working ok. I hope you could help me with some ideas to generate the PWM1 PWM2 and PWM3 so that i could drive the 3 phase motor. my load is actually a 250 W motor but i am planning to currently test on a CDrom motor. I also need to measure the rpm of the motor. I am looking forward for your help.

    ReplyDelete
    Replies
    1. Hello, for a 250W motor i would opt for a PWM motor driver, the driver i propose here are simple driver, that do not use PWM. Anyway it could work if well configured. The code indeed is a pretty simple loop. Also the power stage schematic proposed here is not able to drive a 250W motor, you have to design a more accurate power stage for that motor power. To make the code works on ATmega32 you should change some registers, TIMER etc.. but it will not be so difficult.

      Delete
    2. How can i develop a more accurate power stage motor? Can you guide me into it?
      I am thinking to first test in a CD_ROM motor. I am taking your project as a reference though. Since i have to drive a 250 watt motor and put some more fancy features like LCD and all, i chose the ATmega 32 microcontroller. Please guide me to make a more accurate power driver.

      Delete
    3. I tried to simulate the circuit, but the motor ran very slow. How can i make it run faster?

      Delete
    4. Hello, you have to change the timing parameters

      Delete
  19. I am finding it difficult to actually determine the active mosfet h-bridge and l-bridge configurations based on the hall sensors input..
    I can see you have 10-01-00 (UVW) fet outputs for 100CW
    but i find 10-00-01 for 100CW. Is it that we could use the configurations for either of the U,V or W having the same patterns or does it has something different to do here? Thanks

    ReplyDelete
    Replies
    1. The sequence i've proposed works for my test motor. It depends on your motor type and how it is wired. Generic commutation can be found in Microchip AN857 app note, or Atmel AVR194 app note, or other app notes.

      Delete
    2. does the bldcsensored getspeed function reads the speed of the motor?
      If so, how?
      If not, how can we do that?

      Delete
    3. That function read the speed from 0 to 100 unit of the motor, not the rpm. If you need to read rpm, you have to add an hall sensor, and setup a timer to read, like it is done in this winding machine: http://davidegironi.blogspot.it/2014/05/a-pickup-winding-machine-built-on.html

      Delete
  20. Thanks for share this post, i really like this

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Greetings , sir.
      Thanks for share this post, i really like this.....
      I need .hex file. Can you help me out?
      my email shadowion@yahoo.com
      Thank you.

      Delete
    2. Hello, thank you.
      You've got mail ;)

      Delete
  22. sir i need hex file and can i use it for 35V 25A bldc and please help me i'm confused with this code can i got arduino code ? thanks in advance i'm from india
    please help me sir

    ReplyDelete
    Replies
    1. Hello, this cose is not for arduino. It for plain avrgcc. You can compile it using avrgcc, or an IDE like Eclipse IDE plus the AVR plugin. If you want i can send you the hex compiled for Atmega8 @ 8MHz.
      Keep in mind that this driver does not implement PWM, for an adavanced driver take a look to SimonK firmware.

      Delete
  23. this driver does not implement PWM??? means?
    my motor is 10000 rpm and 24 volt does it work for it ?
    send me hex if it does work for it i'm gonna use this bldc in cnc cutter and i'm stuck at this bldc things please help me out to make my cnc run thanks in advance

    ReplyDelete
    Replies
    1. Hello, about the PWM, read this document: http://www.st.com/st-web-ui/static/active/jp/resource/technical/document/application_note/CD00041736.pdf.
      Yes it works, but a PWM is preferred. You can try the SimonK firmware, or the wii-esc project. For the hex, i can send you the ATmega8 @ 8Mhz version, the one i build for my project. You have to tell me your email address.

      Delete
  24. and this motor has also 3 hall sensor feedback ,so i need hex for sensored , if i can achive 8000-10000 rpm without sensor then i'm ready to use sensorless
    hex thanks for your precious support !

    ReplyDelete
  25. my email adress is : cktandel@yahoo.in

    ReplyDelete
  26. thank you very much for your help !!!!!!!!!!!!!!!!!!!!!!!
    thanks a lot !!!!!!

    ReplyDelete
  27. i've got a mail thaks you really help me !

    ReplyDelete
  28. in your schematic you have putted 16 mhz crystal and i have 12 mhz containing board so does this hex work ?

    ReplyDelete
  29. i have uploaded hex but mosfet and other circuitary is remaining to build..
    so it uploaded sucessfully means it will works or not ?

    ReplyDelete
  30. so your schematic circuit remains same but i have to change 16mhz crystal 8 mhz crystal ?

    ReplyDelete
    Replies
    1. Please, do not flood with comments.
      The schematics, is a generic schematics. One can also use it with a 16Mhz crystal, but the hex i've send you is for 8Mhz, so, setup your ATmega to run at 8Mhz (even using the internal RC).
      To check if it works, you can you an oscilloscope. And test over the output pins of the mega.

      Delete
  31. sorry for that, i got it . i really appriciate your help thaks for your support !

    ReplyDelete
    Replies
    1. No problem, It's just to let the comments on this post not to grow, to let the comments be fast to read and usefull for other people.

      Delete
  32. hello im very new to all this but would love to build this as a project do you have a parts list. many thanks dan

    ReplyDelete
    Replies
    1. Hello, I'm sorry but i've no part list, you can find all the parts I've used in the schematics file.

      Delete
    2. thanks can i ask what the difference is between vcc2 and normall vcc i see that it has a capacitor between it how come?

      Delete
    3. VCC2 is the main power, it is used to run the motor and the micro, cd/rom or floppy disk motor like the one i've used can be run at 12V. VCC is the voltage needed to run the micro, it must be 5V, and it eventually can be regulated from the main VCC2 with a linear voltage regulator or a step down DC/DC regulator, it depends on the main VCC2 and on what's your design guidelines.

      Delete
  33. I see it just confused me because in your schematic the middle mosfet was listed as VCC not VCC2 is this a typo?

    ReplyDelete
    Replies
    1. O yes, sorry. Typo error. Now fixed. Thanks for feedback.

      Delete
    2. No worries, the capacitors you used are the highly rated do they need to be a special type? and also the part coming out of pins 9 and 10 whats its putpose. Sorry for all the questions but your design has really helped me understand bldc controllers.

      Delete
    3. I do not use any special type cap. As a rule of thumb, i choose at least almost twice the voltage that load the cap. Attached to PB6 (XTAL1) / PB7 (XTAL2) of the ATmega there is a crystal. It is usefull if you would like to run the micro at 16Mhz. My tests runs at 8Mhz using the internal oscillator, just to check if even 8Mhz is enough to spin the motor.

      Delete
  34. I need hex of sensorless , my email:cktandel@yahoo.in thanks in advance

    ReplyDelete
  35. Hi this may sound dumb but where and what do JP1 and JP3 do?

    ReplyDelete
    Replies
    1. Hello, there is nothing dumb in it ;)
      JP1 is left for debug purpose, in case one want's to attach it to an UART terminal.
      JP3 si the hall sensor connector, it has to be connected to the hall sensor mounted on the motor.

      Delete
  36. Thanks so much for the reply you really are awesome. I'm planning to use this for 60amp brushless RC motor I'm gonna need to step up the mosfets and driver any recommendations?

    ReplyDelete
    Replies
    1. Never try this circuit on a 60A motor. So I can not give you any advice. Just some basic ones, just try before with less power motor. Also keep an eye on mosfet temperature. Notice that it is preferred to use a PWM driver, expecially for high power application, the software I've write is fairly basic and does not implement PWM, so you could also take a look to wii-esc project, or SimonK firmware.

      Delete
  37. HELLO !!

    FRIEND I LIKED VERY YOUR PROJECT, AND JA RODE PCB ALL, SO THAT AT THE TIME SO HAVE AVAILABLE A ARDUINO UNO ATMEGA328, AND TESTING DO INTEND, HOW WOULD SEND THE FILE PROGRAM VC ARDUINO, I TRIED WITH FILES .c .h NOT KNOW CONVERTER. THANK YOU. MY IMAIL: aurismarf@hotmail.com or projetozul@gmail.com.
    'M TRYING TO RIDE A ESC.
    THANK YOU

    ReplyDelete
    Replies
    1. Hello. This project can be compiled on ATmega328, but it has to be modified in orderd to work on it. You had to change the TIMER interrupt. Then you can compile this using avrgcc. If you want i can send you the hex for ATmega8.
      If you are searching for a good arduino sketch, please look at the Takao Shimizu one, files on rcgroups.com forum. He writes good BLDC driver.
      In addition to this, please do not write capitalized.

      Delete
  38. Thanks for share this post, i really like this.....
    I need .hex file. Can you help me out?
    my email coi3lan@gmail.com
    Thank you.

    ReplyDelete
  39. need hex file @ nk9727@gmail.com
    and please share component details

    ReplyDelete
    Replies
    1. Hello, you have got mail with the hex attached. You can find all the source code in the link above in this post.

      Delete
  40. if you are not using pwm then ho do you controll the speed of motor without pwm ?!

    ReplyDelete
    Replies
    1. PWM for BLDC driver, it is intended on 1 commutation sequence. I do not use PWM on the single commutation sequence. I Just run sequences.

      Delete
  41. and another qustion is irf640 can handle 3600 W power by it's datasheet so i think it can handle 250W easyly please correct me if i am wrong ..with this calculation..

    ReplyDelete
    Replies
    1. Yes, but you have to think about power dissipation.
      Speaking rough, PDr=Rds * I^2. Anyway, take a look here: http://www.mcmanis.com/chuck/Robotics/projects/esc2/FET-power.html

      Delete
  42. Hey, thank you for such a great post! I am unable to compile it at the moment, please can you kindly mail me the .hex files for both Sensored and Sensor Less versions on Email ID: aakash.akoo99@gmail.com

    Thankyou. :)

    ReplyDelete
  43. Hi Sir

    First of all, thanks for all this information. I'm examining DIY Schematics to widen my information and this project helps a lot.

    If you don't mind, I couldn't quite grasp the BEMF part of your schematics : especially PD6 pin. I understood from your code that you run the ADC in differential mode to compare the BEMF to the coil fitting to the commutation cycle in progress, but I didn'T quite get the R14-R16 resistors there. It's as if you are connecting the motor windings to each other, through big resistors, kinda like shorting the leads. Why? Why not use a diode or something there, instead of resistors?

    I'm sorry if I'm making a stupid mistake there, I just couldn't picture the logic there.

    Thank you

    ReplyDelete
    Replies
    1. Hello, although this driver is for sensored motor, it has a schematics that would works even for sensorless ones, For the sensored version i do not use the BEMF part of this circuit. Said this, the one I'm using is a pretty standard bemf input circuit. R14-R15-R16 should be big enought to prevent cross influence between the ADC input pins, and they are in the range of the ATmega ADC impedance. Those thress resistors create a sort of virtual center tap point. No problem at all, me too i'm not an electronic eng.

      Delete
  44. hello friend, thanks for sharing, I am trying to build a esc, however'm using different components, I'm trying to get a pic, I am using an inverter bridge with bjts, I'm using TIP41 and TIP42, I would like to know your opinion on the use of bjts for the construction, my goal is to drive a cd engine room as well. I would like your help, Thank you!

    ReplyDelete
    Replies
    1. Hello, thank you for your feedback. At first i must admit that a brushless driver is not so simple to build. If you are planning to build it from scratch, i mean both hardware and software, i suggest you to check your software with a know to work hardware first (like using the power driver from an existing ESC), or viceversa check your hardware with a know to work software, like the SimonK firmware. Usually NPN transistor isn't the best choice for a Bridge driver, that's the reason why i choose Mosfets. Hope this helps!

      Delete
  45. Thank you, I had problems with hall sensors, you know where to find more information about the hall sensors? I think I burned them in your schematic you get the circuit you entered the picture and connects the outputs of the comparators in atmega entries? when I turn on the cd room of engine sensors, do not know what the gnd and what is the VCC

    ReplyDelete
    Replies
    1. Hello, the comparator are not used on that circuit. GND and VCC depends on hardware, you have to measure it. The first thing you should do is to test the hall sensor, you may use led like i've done, it may helps during the developement stage.

      Delete
    2. Thank you, I managed to solve the problem with the sensors! Another question, do you know how much electrical current of a cd rom engine?

      Delete
    3. Happy to hear this. About the current, obviously it depends on how the driver it is written, the motor, the power supply electronic, and speed reached. As example, if I remember correctly, that motor in the example runs at 12V / ~4A. I've run all examples using a old PC ATX power supply capable of 12V/7A and 5V for the board output.

      Delete
  46. Hi !

    I am changing 5k Ohm variable resistor to change the speed (PC3). But I notice no change in the frequency or duty cycle of output waveform.

    ReplyDelete
    Replies
    1. Hello, the duty cycle does not depend upon the value of this resitor. That resistor is mapped to a 1 to 100 value, that sets the speed.

      Delete
    2. In the video above: when you rotate the knob of a potentiometer, the speed of motor varies. So while you change the knob, the mapped values 1-100 also changes. This change should be observed in the six output pins of microcontroller isn't? When I rotate the knob I observe no change in these six outputs.

      Delete
    3. You can observe the output changes if a motor is running and connected.

      Delete
  47. And about these lines:

    #elif defined (__AVR_ATmega8__)
    #define ADC_BANDGAPVOLTAGE 1300L

    How did you set the value of ADC_BANDGAPVOLTAGE to be 1300L. I want to know because I am changing the code for Atmega48 so I have to define these lines for Atmega48 also like this:

    #elif defined (__AVR_ATmega48__)
    #define ADC_BANDGAPVOLTAGE xxxxL

    I have searched the datasheet, but not helpful. Can you please tell me how use used the datasheet to calculate the value 1300L.

    ReplyDelete
    Replies
    1. Hello, it's in the datasheet from Atmel ATmega8, "Internal Voltage Reference", page 42 of doc Rev.2486AA–AVR–02/2013
      For ATmega48, it's 1100L, look at the Table 29-3, page 307 or doc Rev. 2545T–AVR–05/11.

      Delete
  48. Hi Davide,
    Nice work, very informative, quick question about the 10k voltage divider. Above 10 volts battery wouldn't this exceed 5 volts at times and the voltage limit for the pins? Just being cautious.

    ReplyDelete
    Replies
    1. Hello, and thank you for you feedback. You are right, but there's some voltage drop due to motor current absorption, so up to 12v it will works. Keep in mind that I'm not an electronic engineer so I may have done mistakes in the schematics proposed here.

      Delete
  49. Hi.
    Thank you for your work.
    Can you send me please hex file for Hall version.
    extwriter@gmail.com

    ReplyDelete
  50. This comment has been removed by the author.

    ReplyDelete
  51. Hi,
    i am trying to control and regenerative braking a 100W BLDC motor.
    I am using Arduino and IR2110 as my mosfet driver.
    had a few doubts and would love your help.

    1. The IR2110 circuit I am referring doesnt has a capacitor between VCC and Gnd, is it necessary?
    2. The VDD is connected to 5v, can I supply it from arduino?
    3. VCC of IR2110 is connected to 12v where as motor voltage is 24V, can i connect the same 24v (26v in actual) to the mosfet driver and the inverter circuit?
    4. you have shown 1 capacitor between each driver's VCC and ground, and all 3 VCCs and grounds are connected, so cant I put just 1 capacitor between the main VCC (VCC2 in your diagram) and gnd and eliminate the rest three?
    5. Do I need to isolate the arduino and the mosfet drivers using capacitor between their grounds? because connecting vcc (26v) to mosfet drivers and 5v to arduino turns off the arduino, and some say its because i havent used any isolation between the circuits.

    sorry to bother you, but need serious help.

    ReplyDelete
    Replies
    1. Hello. 1) Not necessary but better to put it; 2) Yes, you should, the IR2110 is a low current consuming device, but It's better if you get 5V somewhere else and use it for you micro and IR2110; 3) You make me notice that there is my mistake in the cicuit, IR2110 needs to be connected to 5v (0..25V), i will fix it; 4) Yes you can do this; 5) Ground shold be the same, i don't get this.

      Delete
    2. Hello, I've checked the schematic. The IR2101 needs 10 to 20V supply voltage. Also I've fixed the capacitors on IC driver and the comparator values. I hope i will have time to develop the PWM version of this driver.

      Delete
  52. Hi Davide.
    Can you send me hex. files for sensored and sensorless
    edolaidi@gmail.com

    ReplyDelete
  53. why i can't use your schematic circuit on the proteus?

    ReplyDelete
    Replies
    1. Hello, I'm not a proteus use sorry. You have to ask it at the proteus discussion board.

      Delete
  54. I want to make bldc 3 phase motor controller with hall sensor. Input voltage 18 - 36v.featured like cw ccw direction,speed vary ,brake ..send me procedure aslso send hex file on email id --Satish.Sadashiv@yahoo.in
    Above you given ckt perfectly working so decided go a head.. Thx for sharing knowledge

    ReplyDelete
  55. Great work!!
    I am working on three phase motor controller with input voltage 12-15v for cw, ccw, variable speed, dynamic braking . I am using H-bridge inverter. Plz I need produre and hex file too. my email: kingmko4real@yahoo.com

    ReplyDelete
  56. Hello Sir. I am trying to make project " Calculating speed of BLDC 3 phase motor with Hall Sensor". Can you send me procedure and hex.file too? rahmisk13@gmail.com

    ReplyDelete
    Replies
    1. Hello. You can download the source for this project just above on this post.

      Delete
  57. Hello Sir, its really such a great post!
    but i have a problem, my friends use your source code to make a bldc motor but i didn't understand. Can you help me to find out the relation of variable setting with the speed control resulted?
    and one thing, I have a source code which has modified from your source code, but still don't know how to find the relation, do you mind to help me find this out? If you are available, i will send the source code and proteus simulation through email.
    do you want to email through anissayolandaputri28@gmail.com if you want to see the source code because i don't know your email.
    Thankyou sir, i really need your help.

    ReplyDelete
    Replies
    1. Hello, speed (from 0 to 100) it is mapped (look at the map function or Arduino as reference) to the bldcsensored_speed variable, that is used in the TIMER1_OVF_vect to emit the step. I'm sorry but I'm fully engaged in another project, so I probably can not review you code, still send me that, I will try to take a look on it (I've sent you an email you can reply on). What i can tell you, is that - if possible - you should make small steps from any working code, that way you will always know what are you changeing and how your changes affect the code behaviour. BLDC are a pretty difficult things to move, it may take a while to succed, but do not ever break down.

      Delete
    2. I've tried to change some values of the variable to observe what changes can occur, but i don't know exactly what variable should be used for the speed of bldc motor in that program. Well sir, i've sent you email, if you don't mind, i really hope that you check it and reply my message, Thank you so much, Sir.

      Delete
    3. Hello, I will take a look as soon as possible.

      Delete
  58. Sir, on your source code, where I could find program of measuring speed of Hall Sensor? is it on Adc, bldcsensored or main ?

    ReplyDelete
    Replies
    1. Hello, here in my code there is not the speed rpm measure of the motor. But you can add it with a few steps, I think it will be good coding adventure.

      Delete
    2. Hello, just leave your email here, I will send you a message.

      Delete
    3. rahmisk13@gmail.com. thankyou sir :)

      Delete
  59. sir, on your source code, what is input if module uart is used. because when i running is not working. ivanieisa@gmail.com

    ReplyDelete
    Replies
    1. Hello, not input is used, for uart debug mode you have to use the uart output of your micro, on ATmega8 it's PD1. Note: if the uart library does not compile, use the newer one from P.Fleury website.

      Delete
    2. hmm, so in your program there is not function for input? if i want to make a code for manage motor bldc to stop or start from vb.net. where is exactly i can add the code?
      sir,can i have your email?

      Delete
    3. sir, if i want to stop motor bldc from vb.net. what is the code that i must add?

      Delete
    4. In order to drive the motor from the serial port, you have to write your own protocol. You can write it in a main routine, inside while(1), you should wait for some byte to come from the uart input then read and interpreter the byte you get according to the serial protocol you write. As a suggestion. Start with a simple protocol do not attach it to a motor driver, simply attach it to a led, or echo back the command you read, just to check if all it's running. Once all it's tested, then you can attach my or another motor driver.

      Delete
  60. sir, i have tested it for on/off led an it is working.and also i have can to start bldc mototr from vb.net but i don't know in your code which one that used to stop bldc motor

    ReplyDelete
    Replies
    1. Hello, to stop the motor call bldcsensored_setstop()

      Delete
  61. Hello Davide,

    Great Job!! Looks great. Few questions I have.
    A. Does the MOSFET driver gets hot in any way during drive?
    B. In some cases the MOSFET ground gets replace with -DC. when is that required?
    C. Can I drive a 24 volt 10amp BLDC motor with this drive?
    Looking forward to hear from you.
    Srijit

    ReplyDelete
    Replies
    1. Hello and thank you.
      A. it depends on the motors and current used, in my sample above not
      B. MOSFET ground is the GND of the voltage, I do not understand what you mean
      C. I've never tried a 10A motor, I've tried it with 24V motor up to 5A, no proeblem at all.
      Side note: this driver does not implement a "true" PWM output schema to drive the motor, i mean MOSFET are always ON or OFF during any commutation. I'm implementing a driver with PWM schema, but unluckly I've no time to end that work and post it here, i hope it will be posted in a few months.

      Delete
  62. Hi Davide,

    I noticed on your schematic that the VCC-drv and VCC-motor are separated. Will this circuit work if I have a single 12V power supply for both the drivers and the motor, or does the driver voltage need to be higher than the motor voltage?

    ReplyDelete
    Replies
    1. Hello, yes it will, IR2102 works from 10V to 20V.

      Delete
  63. Dear friend can you help me please? I wrote on youtube too. So the problem is my computer is so old and slow. Because of it cant normaly compile file to hex. I want to use Atmega 8a and hall sensors for my brushless motor (24volt 70watt) Can you please help? bestoffff18@gmail.com this is mail e-mail address. Thank you.

    ReplyDelete
  64. I use Arduino mega as programator. I need to make motor work in two days. Because i broked it. Please help me. I have so little time left. Hope for your help

    ReplyDelete
    Replies
    1. Hello, to me, two days will be not enought to build this driver. Anyway, you have to download the code and compile it. This is the first step.

      Delete
  65. Hi again! Can you send me hex file and all procedures please? Bestoffff18@gmail.com i tried to compile but atmel studio 7 dont see the project.

    ReplyDelete
    Replies
    1. Hello, you can download the file with hex and src on this link (expires in 2 days) https://expirebox.com/download/3e8423ab559c961d8553b3763950a246.html
      About the compile procedures, the simpler thing to me will be to use Eclipse + the AVR pluing. Just make a new AVR project, add a source folder, copy and paste all the project file there, and compile. You can find many tutorial online about the Eclipse + AVR.

      Delete
  66. Thank you! I have made controller. But now problem is motor is trying to start but it goes to both sides and give some weird noise. Don't know what because of what (((((

    ReplyDelete
    Replies
    1. Hello, you have to tune the driver for your motor, an oscilloscope may help doing this. Also you can debug using a simple UART port.

      Delete
  67. Hello Davide!

    Nice job! Thank you very much!
    Can you please add a braking feature? I need to stop my motor with a very steep ramp, and hold in position. Is it possible?

    ReplyDelete
    Replies
    1. Hello, yes it is possible but not implemented, unlucky I've no time right now to develop this, but you can build a ramp down stop simply by setting speed from actual to zero. It could be a nice feature, if you build this please sahre it.

      Delete
  68. Thank you very much for sharing your knowledge on BLDC with us. I have question. Can you write the codes of BLDC in Arduino ide?

    ReplyDelete
    Replies
    1. Hello and thank you. This code is for plain avrgcc and there are no plan for a Arduino IDE porting, but you can port it simply because it's avrgcc based. If you implement this porting and would like to share please port there the git or pastebin link for that.

      Delete
  69. Hello pls give me hex file . My mail is nishanthajw@gmail.com thank you.

    ReplyDelete
    Replies
    1. Hello, you can download it from here: https://expirebox.com/download/ec2b17da95f4b9701b84ac6880000d7b.html (link expires in 2 days).

      Delete
  70. Hello
    Can i please have the .hex file
    my email: mahmoud_elsagheer@hotmail.com

    ReplyDelete
  71. Pin PD0 not work . Alway = zero . I think problem you use it for UaRt.
    And exactly you use internal 8mhz or external 16mhz ??? how is Fuse bit ?

    ReplyDelete
  72. This comment has been removed by a blog administrator.

    ReplyDelete