Monday, September 30, 2013

A simple brushless sensorless 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.



For an introduction to BLDC motors, please look at my sensored motor driver post, here: http://davidegironi.blogspot.it/2013/09/a-simple-brushless-sensored-motor.html


For this project, I've implemented a simple brushless sensoreless 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.

The motor can be controlled in speed and direction (clockwise and anti-clockwise).

This project use open loop startup and bemf zero crossing detection method with ADC.
Speed change can be done only when motor is not running, ADC is used during spinning phase in zc detection so it can not be used during the motor spinning, but digital speed changing can be implemented.
ZC threshold current should be defined by user depending on the motor type.

User has to setup the port used to read the the bemf current. Also the timer interrupt and prescaler should be setup for different running frequency.
The running step for the motor are defined as default, anyway user can change it to fit any motor.


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

The same test board of sensored library post is used, reported here the schamatics, for further info look at the sensored library post.

Setup parameters are contained in bldcsensorless.h

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

Code
Notes
  • read risk disclaimer
  • excuse my bad english

88 comments:

  1. I was waiting for it. Great. But I don't understand smt:

    [quote]:
    "User has to setup the port used to read the hall sensor, <...>"

    Isn't this sensorless?

    ReplyDelete
    Replies
    1. thank you for you feedback.
      yes, it is for sensorless motors, i made i mistake writing description here, now i've corrected this post :)

      Delete
  2. i've not measured torque force, for shure the sensored has more force on startup stage, cause input are driven by hall sensor.

    ReplyDelete
  3. No problem, just google for "Eclipse AVR tutorial" you will find some website that teach you how to build and hex file for ATmega with Eclipse. I suggest you, at first, to try building and loading a simple project, like a blinking led, then try with this one.

    ReplyDelete
  4. Thanks for your effort.
    I saw the schematic and the source code for sensorless.
    When I check the source code and the schematic, I found some difference.
    For example
    To change the direction, you use the PC5 port. But the PC5 connect to on the PD0 schematic. So I'm confusing.
    I want to know what the truth is. And I want to get the schematic of the latest version. Can you send it to me?
    Have a nice day!
    Regards HYYoo

    ReplyDelete
    Replies
    1. Thank you for feedback, I've never noticed i've posted the wrong schematics version.
      The one you can find now in this post has correct wiring.

      Delete
    2. Thank you Davide.
      Your source code is simple and excellent. So I can understand the concept for the BLDC. Also I can know how to implement it.
      As your code, for the change a speed and a direction, first stop a motor then start it. Is right? I think that the speed is changed while spin. So I just add some part as follow. is it possible?
      if(direction != directionold) { //direction changed
      ......
      }else{
      potspeed = adc_read(POTSPEED_CHANNEL); //read filtered pot speed
      if(potspeed != potspeedold) {
      potspeedold = potspeed;
      uint16_t potspeedn = (long)(potspeed - 0) * (long)(100 - 0) / (long)(1024 - 0) + 0;
      bldcsensorless_setspeed(potspeedn);
      }
      }

      Thank you!

      Regards HYYoo.

      Delete
    3. thank you.
      The sensorless version of my code check the backemf by ADC continously (BLDCSENSORLESS_ADCREADSEL), ADC is always "busy". that's the reason why i prefer to read speed changes while not spinning.
      You could free the RX pin an control the speed by uart.
      You also could use other tecnique to read the backemf. Some time ago I was implementing a version that use cumulative current check, and make less use of ADC, but I've never continue on that project. I suggest you to give a try to the Takao code from rcgroups.com, or the wii-esc project code.

      Delete
  5. hi
    i m implementing ur code but i need some clarification about these points
    BLDCSENSORLESS_ZCERRORS : what does it refer to
    how in code i define zero cross threshold
    bldcsensorless_setspeed(100) :what is the unit for speed
    thanks for ur help


    ReplyDelete
    Replies
    1. Hello,
      BLDCSENSORLESS_ZCERRORS it is used to check how many zc incorrect position (errors), occours, if too many occours, then a startup loop is launched.
      Unit for speed, is 0 to 100.

      Delete
  6. thanks for ur reply
    i m trying it now with cd rom motor of nidek
    the start up commutations start but the repeat without entering running phase
    what is the problem
    thanks again for feed back

    ReplyDelete
    Replies
    1. Check your hardware with another firmware, like wii-esc, so you will be shure that hardware is ok, and then you can debug the firmware.

      Delete
  7. hi again
    how do i define zc threshold ? i mean if it is estimated tobe 4 volts >>how do i write 4 volts in ur code"i see value of 150 and i dont know what it refers to "volts - millivolts....???
    thanks

    ReplyDelete
    Replies
    1. My threshold is a raw value, not a voltage converted value.

      Delete
  8. //check zc depending on actual polarity
    if ((!bldcsensorless_zcpolarity && currentbemf>BLDCSENSORLESS_ZCTHRESHOLD) || (bldcsensorless_zcpolarity && currentbemf>>>>?????
    this part is vague to me

    ReplyDelete
    Replies
    1. That's where the zero crossings (zc) is checked.
      Keep in mind that i use adc to check this, a better approach should use the analog comparator. As soon as i will have time to implement that, i will release an analog comparator version of this project.

      Delete
  9. hi
    which wii-esc is suitable for ur hardware scheme?

    ReplyDelete
    Replies
    1. Check the wii-esc circuit, i suppose you just have to change ports setup.

      Delete
  10. hi
    here what i did till now
    1 i built ur hardware circuit
    2 i flashed the mcu with ur code"didnt change it coz i dont know what variables to change regarding zcthreshold and the port used to read bemf current "

    ReplyDelete
    Replies
    1. Again, check the harware whit another firmware, then you can debug the software.
      Mine is a farily basic driver, It does not implement duty cycle, and do not use analog comparator. So if you need performance, i suggest you to switch to another one. Takao Shimizu on rcgroups.com is building a simple but very good driver, check the "BLDC controller" discussion on the rcgroups.com. To read more about bldcmotors, rcgroups.com is the place you have to be.
      Also a good starting point will be the AVR444 Atmel Application Note.

      Delete
  11. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. hi
      I have hdd motor ı want to drive without any feedback when delay about 30ms it turn but when delay is too low it produces "squealing sound" but does not turn can you suggest me any solution
      thanks for your help

      Delete
    2. It could be because you are trying to move it too fast. If you are using this driver keep in mind that this is not a PWM driver. Also it use ADC reading to read back EMF, there are faster approach that use ADC comparator, and faster ADC conversion means faster spin.

      Delete
  12. To adjust the speed, I understand you can bypass the switches (in order to avoid the losses), and instead do it by adjusting the VBus going to the motor. The switches would only be used for commutation.


    But what if you want to control speed via the PWM duty cycle.. does it matter which switch you pulse (i.e. high side, low side, both (complimentary))?

    Also, there's unipolar vs bipolar (1-4 quadrants) that I'm not sure how what this means in practical terms. From what I've read, when doing unipolar, the rotation direction is determined by the commutation sequence, but in bipolar, the direction is based on if the duty cycle is less than or greater than 50%?! But how (or why) does this actually work?

    Also, can you recommend where to buy a driver board? I'm not too handy with a soldering iron, but would love to hook up a bldc motor (no sensors) to my atmega1280 kit. I'd like to try 6-step trapezoidal commutation and maybe someday sinusoidal/foc with a beefier processor!

    ReplyDelete
    Replies
    1. If you mean controlling the speed control of the driver by PWM, you need to implement a PWM reader using the interrupt input PIN as example. There are a few post on avrfreaks to discuss about this.
      For the "how to drive", and "how it works", The unipolar / bipolar commutation sequence it is a PWM techinique of controlling the motor. This (my driver), is not PWM.
      The cheapest compete boards that comes to me, are ESC 30A drivers commonly used for RC. You can buy any SimonK compatible ESC board, then open it, make just some solder to expose the ISP programming wiring, and use the internal ATmega8 micro. Or you can buy somthing like the hip4086 and use just some mosfet to build an external driver to use with your ATmega1280.
      Hope this helps.

      Delete
    2. Awesome, thanks! I was thinking you were using pwm because the bldcsensorless.c source had a comment about fast pwm mode... but looks like that's just to setup the timer. So are you controlling the speed by adjusting the time (ICR1) between when a zero crossing occurs and when the next commutation is applied?

      Delete
    3. Yep. The speed control it's "ICR1 = bldcsensorless_speed;". I'm working on a PWM driver, but In this period I'm full of work and I do not have time to finish that driver.

      Delete
  13. Hi Davide . I made it but what is the UART (PD1) ? where i should connect it to ?
    What is the use of it ?
    Thank you

    ReplyDelete
    Replies
    1. Hello,
      the uart PD1 is just for debug purpose on serial port. If you need to debug something throght serial connection.

      Delete
  14. so why it does'n work ? :-(
    I made both in real and also in Proteus .
    In real it rotates the motor for a while with different speeds and again and again.
    and it has nothing to do with the speed variable resistor and direction set .

    ReplyDelete
    Replies
    1. Check your timer setup. Check the zc bemf threshold. Scope around to see what's not working. It will probabily do the startup cycle, then maybe the zc threshold is not read or it is not propery setted. It changes by motors and even by power driver. You have to debug it.

      Delete
  15. thank you for this.
    But I can't understand why PD6 don't have LPF?
    I had known neutral point need LPF. Its input is HF signal.
    can you tell that?

    ReplyDelete
    Replies
    1. Hello. The schematics you see above it's a standard implementation. The code in this library does not use the comparator on PD6. A better approach is to use that comparator. Indeed I've another driver in production which use that comparator. I will share that project soon.

      Delete
  16. Hello, I have one more question.
    It can't start with itself. If I forced to start, it has run.
    I think starting power is lack, but I can't modify.
    How can I fix the source to do?

    //run commutations
    for(;;) {
    bldcsensorless_runstep(bldcsensorless_commutationstep);
    if(bldcsensorless_commutationstep == 0) {
    if (i < 2)
    i += 1;
    if ((i == 2)&&(j<BLDCSENSORLESS_STARTUPCOMMUTATIONS)) {
    j += 1;i=0;
    }
    if ((i==2)&&(j==BLDCSENSORLESS_STARTUPCOMMUTATIONS)){
    break;
    }
    }
    //do startup delays
    uint16_t d = 0;
    for(d=0; d<startupdelays[j]; d++) {
    _delay_us(5);
    }

    I modified i and delay_us(5), but that wasn't change.

    Linked address is video of my bldc driver.
    https://youtu.be/F-iwdNtXJYg

    I'm sorry for this. T.T

    ReplyDelete
    Replies
    1. I modified zc threshold and zc error but it can't run too.
      How can I start a motor...

      Delete
    2. Hello, at first i would try with another motor, one that's needs less current then the the one you are using, maybe a floppy or cd one. Just to see if is all working.
      Then, one thing you could change is BLDCSENSORLESS_STARTUPDELAYS and BLDCSENSORLESS_STARTUPCOMMUTATIONS
      But remember, this a simple brushless driver, i think you should go for a PWM one, which works slighty better on high speed.

      Delete
  17. can u give me the code for spwm technique for low speed control of sensorless bldc

    ReplyDelete
    Replies
    1. Hello, you can download the code for this project by the link on the description. However, this code does not implement SPWM.

      Delete
  18. could you tell me the code.? thanks

    ReplyDelete
    Replies
    1. Hello, you can find it in the blog post link.

      Delete
    2. is this all code able to generate with CVAVR software.?

      Delete
    3. This code is tested and copiled over a avrgcc compier. I've never test it on CodeVision AVR

      Delete
    4. it's ok, i'll try to generate with CVAVR because I usually use it.
      Davide, could you send me hex file of sensored and sensorless to my email ruslirizani@gmail.com, please.
      thank you Davide

      Delete
  19. Ciao Davide, spero che non dia fastidio che scrivo in italiano ma lo é per me molto piú semplice. Ho un BLDC motore coi parametri tecnici 100V/150Hz 280W 2250r/min ed ho provavo far girare questo motore per mezzo dello tuo driver. Sono riuscito a farlo girare coi 15V sugli MOSFET senza qualsiasi modifiche dello programma, anche se il motore si è avviato molto difficilmente e qualche volta ci voleva pure 30 secondi finche non si è avviato e quando si girava, lo faceva molto lentamente e percio mi sono deciso di provarlo coi 50V sugli MOSFET ma dopo aver provavo questo, mi scoppiarono qualche componenti dello driver e non so perché. Potresti darmi qualche consiglio come dovrei modificare lo programma (BLDCSENSORLESS_STARTUPDELAYS , BLDCSENSORLESS_STARTUPCOMMUTATIONS,  zc threshold) o componenti dello schematics? In effetti, é secondo te possibile gestire il mio motore per mezzo dello tuo driver coi 50 o 100V sugli MOSFET?

    Grazie

    Scusa per sbagli in italiano

    ReplyDelete
    Replies
    1. Ciao, questo driver non implementa alcuno schema di PWM. Questo significa che durante un ciclo di commutazione i mosfet sono sempre ON o OFF (al contrario per esempio di uno schema come H ON L PWM per bldc). In realtà anche quella usata da questo driver è una modalità di PWM, ma non funziona bene soprattutto a bassi giri. Il mio consiglio è dare una occhiata a driver più performanti come SimonK o anche wii-esc. Sto terminando un driver che implementa PWM però non riesco mai a trovare il tempo di finirlo, quindi per ora è ancora in prova. Puoi tentare modificando STARTUPDELAYS, STARTUPCOMMUTATIONS, STARTUPDELAYMULT (per problemi nella fase di inizio corsa), TIMER1_ICR1 TIMER1_PRESCALER (timer principale del ciclo di gestione) SPEEDMIN ,SPEEDMAX (indici per velocità min e max del motore) ZCERRORS soglia di ZC (questo è un parametro che può variare da motore a motore).
      Tieni sempre d'occhio il consumo di corrente, ed il calore dissipato dai mosfet. Spero di esserti stato di aiuto. Un saluto. Nota: il tuo italiano molto buono non ti preoccupare.

      Delete
  20. Molte grazie per i tuoi consigli e anche per complimento :-)

    ReplyDelete
  21. Hello Sir I am trying to make this project using STGIPN3H60 IC but it uses different technique to measure current and voltage , So what will be the changes in code and schematic.
    Please reply ..
    my email id masgar4488@gmail.com

    ReplyDelete
    Replies
    1. Hello, you can try to add the BEMF part of the schematics you found in this page (R17, R18, R19, R11, R12, R13) out of the STGIPN3H60 W,V,W output. It should work.

      Delete
  22. Hello my friend
    Thanks for your help and sharing such a nice job :)
    I downloaded the files and trying to compile with atmel studio 7. But unfortunately I can not do it. Can you give me a hand How to do it ?
    And also hex file.
    gkygnn@hotmail.com

    I appreciate you... waiting for your reply

    ReplyDelete
    Replies
    1. Hello, you've got mail.
      About the Atmel Studio, Check that you have linked all the needed libraries. Take a look at the compiler output and try to debug it starting by the compiler output errors. But don't copy and paste here the full compiler stack error please, I'm think long comments became useless cause people tend to ignore long readings.

      Delete
  23. The error I get is ..adc/adc.h: No such a file or directory, and when I click on it, it shows that code ( #include BLDCSENSORLESS_ADCINCLUDE ) in bldcsensorless.c

    and, there are another errors, which I believe due to this error.

    Is there something I am missing ?

    Thanks for your time Again. By the way thanks for the quick mail my friend.

    ReplyDelete
    Replies
    1. Hello, you are missing to include the adc library in the correct folder. you have to check the folder structure and the inclusion of folders.

      Delete
  24. Hello Mr. David Gironi
    I am trying to make project about control system of angular velocity reaction wheel BLDC motor with close loop method. i am using ATmega8 and AVR studio 4 as programming app.

    Do you have source code of my problem?
    Thankyou :')

    ReplyDelete
    Replies
    1. Hello, unluckly not, or I should say. You can take inspiration to project like wii-esc or Takao code from rcgroups.com. My driver here is a closed loop one. The main disadvantages of this driver is that PWM is not fully implemented, and I did not use the ADC comparator for bemf detection. I have a new driver I'm tring to develop from months, but due to other project that one is still on my benchtop. I hope I will finish that driver soon.

      Delete
  25. Hello Davide,
    I fell in love with your controller, I want to build it! Can you send me the HEX files / There is sensor driver and sensorless driver HEX files Thank you in advance for your time and consideration.
    My email: mcu51c51@gmail.com

    Best regards,
    Attila T.

    ReplyDelete
    Replies
    1. Hello and thank you. You've got mail. As a side note, I'm developing and enhanced version of this controller, but It's not yet completed, I hope to post it soon.

      Delete
  26. This comment has been removed by a blog administrator.

    ReplyDelete

  27. Jaber AL SHABAHApril 16, 2017 at 9:08 AM
    Dear
    Davide Gironi, I need the Hex file of the following project of you "avr_lib_bldcsensorless_01c.zip". Plz provide me the Hex file with the folder.
    Regards
    Shak Jaber Al Shabah

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

      Delete
  28. Dear,
    Davide Gironi,i forgot to download your hex file within two days and so i couldn't download it. Anyway, i can't generate hex file in AVR studio from your source code. several errors occur. If i put a double slash(//) before #include BLDCSENSORLESS_ADCINCLUDE, then the hex file is generated. Is it okay ? If not, i need your further instructions and a generated hex file of the source code of it("avr_lib_bldcsensorless_01c.zip")

    ReplyDelete
    Replies
    1. Hello, you have to include the adc library, mine or other one in order to make this project work. I can give you the hex, but it will be useless to me, cause you will need to compile it out and modify. I suppose the problems you had are file inclusions and path issues. You can fix it in AVR Studio, or try Eclipse + AVR plugin, which is simpler to use to me.

      Delete
  29. Hello my friend
    Can you send me the HEX files
    hf.pish@gmail.com

    ReplyDelete
    Replies
    1. Hello, you can find it here, it will expires in 2 days: https://expirebox.com/download/ea90a6ac7aacc50d079d5b25763ca143.html

      Delete
  30. Could you please, send me the HEX file for sensorless driver
    jar3m4@gmail.com

    ReplyDelete
    Replies
    1. Hello, you can find it here, it will expires in 2 days: https://expirebox.com/download/ea90a6ac7aacc50d079d5b25763ca143.html

      Delete
  31. Hello,

    Can you adaptive your codes for arduino ide.

    ReplyDelete
    Replies
    1. Hello, yes it is possible to port this to the Arduino framework for any ATmega328 based Arduino. If you do this please share your port.

      Delete
  32. Hello Sir, Please send me the schematic and source files (with hex) for sensorless BLDC motor. My email id : abhisek.it@gmail.com

    ReplyDelete
    Replies
    1. Hello Abhisek. You've got mail.

      Delete
    2. Thank you sir. But my 12V BLDC motor is not running...and one of my IRF640 is getting tremendous heated. I am using IR2110 instead of IR2101. My schematic for sensorless BLDC is as same as BLDC sensored.
      Please suggest if there any changes required in software or hardware circuit.
      Thanks once again for your kind help.

      Delete
    3. Can I use p55nf06 instead of IRF640 ??

      Delete
    4. Hello, STP55NF06 should work, as long as you use an N channel mosfet in the max threshold, it works. IR2110 also should work good. If one is getting hot double check your wiring, also cheeck the EMF threshold value.

      Delete
  33. Hello Sir, Now I want to run 24V BLDC ceiling FAN motor with the same circuit. But presently it is not working. Kindly advice how to run it in the same circuit.

    ReplyDelete
    Replies
    1. Hello, it may depend on your motor, this circuit is able to drive 24V bldc motor, I've test it on a senserless one, but you may play around with the EMF threshold value. In a few month I will post an enhanced version of this code. If you want as a preview I can send you it.

      Delete
  34. Hello Davide,

    can you send me the enhanced version?

    ReplyDelete
    Replies
    1. Hello sascha, here can you find the new version: https://sht.click/nNO expires in 7 days.

      Delete
    2. Hello,

      Unfortunately, I can not download the file there

      Delete
    3. Hello, try this https://gofile.io/?c=6DgDMn

      Delete
  35. Hello Mr. Davide, hope you are doing well. I was able to drive the 24V BLDC motor with your circuit. Beside that, I also want to have the enhanced version. Kindly send me the link for updated source code with circuit details.

    ReplyDelete
    Replies
    1. Hello Abhisek,
      You can find it here: https://davidegironi.blogspot.com/2019/12/an-atmega-brushless-sensorless-motor.html

      Delete
    2. This comment has been removed by a blog administrator.

      Delete
  36. Dear brother!
    Thank Bro !
    I can search all over the world to find what I really need, can you give me its hex code?
    Thank you brother so much!
    Email: huongtandong2009781@gmail.com

    ReplyDelete
  37. Thank you very much !
    I have received your letter and I am a Vietnamese

    ReplyDelete