Sensorless BLDC motor control with Arduino – DIY ESC

DIY ESC with Arduino UNO

This topic shows how to build a sensorless brushless DC (BLDC) motor controller or simply an ESC (Electronic Speed Controller) with an Arduino UNO board.

There are two types of brushless DC motors: sensored and sensorless. Sensored BLDC motor has built-in 3 hall effect sensors, these sensors detect the rotor position of the BLDC motor. Controlling a sensored BLDC motor is easy since we know the rotor position like what was done in the project below:
Sensored brushless DC motor control with Arduino

The commutation of the sensored BLDC motor is done according to the hall effect sensors state.

Sensorless BLDC motor doesn’t have any sensor to detect its rotor position, its commutation is based on the BEMF (Back Electromotive Force) produced in the stator windings.

The main advantage of sensorless BLDC motor control is lower system cost and the main disadvantage is the motor must be moving at minimum rate to produce sufficient BEMF to be sensed.

How it works:
When the BLDC motor rotates, each winding (3 windings) generates BEMF opposes the main voltage. The 3 generated BEMF signals are 120° out of phase which is the same as the hall effect sensor signals. The figure below shows the relationship between the hall effect signals and the BEMF signals:

bldc hall sensor and bemf signals

As shown in the figure above, the BEMF signals are not synchronized with the hall effect sensor signals (phase shift of 30°). In every energizing sequence, two windings are energized (one connected to positive and the other to negative) and the third winding is left open (floating). The floating winding is used to detect the zero crossing, thus, the combination of all 3 zero cross over point are used to generate the energizing sequence. Totally we’ve 6 events:
Phase A zero crossing: from high to low and from low to high
Phase B zero crossing: from high to low and from low to high
Phase C zero crossing: from high to low and from low to high

How to detect the zero crossing event:
The easiest way to detect the zero crossing events is by using comparators. The comparator has 3 main terminals: 2 inputs (positive and negative) and an output. Comparator output is logic high if the positive voltage is greater than the negative voltage, and logic low if the positive voltage is lower than the negative voltage.
Basically 3 comparators are needed for this project, connections are done as shown in the figure below (example for phase B). Each phase requires a similar circuit.

bldc bemf signal detection

The virtual natural point is the same for all the 3 comparators, it is generated using 3 resistors. When the BEMF generated in the floating (open) winding crosses the zero point towards positive side, the comparator output makes a transition from low-to-high. When the BEMF generated in the floating winding crosses the zero point towards negative side, the comparator output makes a transition from high-to-low. By having three such comparator circuits, one on each of the phases gives three digital signals corresponding to the BEMF signal in the windings. The combination of these three signals is used to derive the commutation sequence.

Hardware Required:

  • Arduino UNO board
  • Brushless DC (BLDC) motor
  • 6 x 06N03LA N-type mosfet (or equivalent)  – datasheet
  • 3 x IR2104S (IR2104) gate driver IC  – datasheet
  • 6 x 33k ohm resistor
  • 3 x 10k ohm resistor
  • 6 x 100 ohm resistor
  • 3 x IN4148 diode
  • 3 x 10uF capacitor
  • 3 x 2.2uF capacitor
  • 2 x pushbutton
  • 12V source
  • Breadboard
  • Jumper wires

Sensorless BLDC motor control with Arduino circuit:
Project circuit schematic is shown below.

Arduino sensorless BLDC motor controller DIY ESC circuit

 

Note that all grounded terminals are connected together.

In the circuit there are 2 pushbuttons, one is used to increase BLDC motor speed and the 2nd one is used to decrease it.

The first three 33k (connected to motor phases) and the three 10k resistors are used as voltage dividers, because we can not supply the microcontroller with 12V, the other three 33k resistors generate the virtual natural point. The virtual natural point is connected to Arduino pin 6.

The Arduino UNO board is based on the ATmega328P microcontroller which has one analog comparator. The positive input of this comparator is on Arduino uno pin 6 (AIN0) and the negative input can be pin 7 (AIN1), A0 (ADC0), A1 (ADC1), A2 (ADC2), A3 (ADC3), A4 (ADC4) or A5 (ADC5). So I connected the virtual natural point to the positive pin of the analog comparator (pin 6), phase A BEMF to pin 7 (AIN1), phase B BEMF to pin A2 and phase C BEMF to pin A3. Each time the comparator compares the virtual point with the BEMF of one phase (this is done in the software). This minimizes the hardware needed and simplifies the circuit.

The IR2104S chips are used to control high side and low side mosfets of each phase. The switching between the high side and the low side is done according to the control lines IN and SD. The figure below shows input and output timing diagram:

ir2104s timing

The SD lines of the three IR2104S are connected to pins 11, 10 and 9 respectively for phase A, phase B and phase C. The Arduino UNO can generate PWM signals on that pins where only high side mosfets are PWMed.

Sensorless BLDC motor control with Arduino code:
The code below does not use any BLDC motor library.

As mentioned above, Arduino pins 9, 10 and 11 can generate PWM signals where pin 9 and pin 10 are related with Timer1 module (OC1A and OC1B) and pin 11 is related with Timer2 module (OC2A). Both Timer modules are configured to generate a PWM signal with a frequency of about 31KHz and a resolution of 8 bits. The duty cycles of the PWM signals are updated when a pushbutton is pressed (speed up or speed down) by writing to their registers (OCR1A, OCR1B and OCR2A).

The analog comparator compares the positive input AIN0 (Arduino pin 6) with the negative input which can be AIN1 (pin 7), ADC2 (pin A2) or ADC3 (pin A3). When the positive pin voltage is higher than the negative pin voltage, the output of the analog comparator ACO is set, and when the positive pin voltage is lower than the negative pin voltage, ACO is cleared.
In this project I used the analog comparator interrupt and I used its interrupt on rising (transition from low to high) and interrupt on falling (transition from high to low), this makes the zero crossing events interrupt the microcontroller.

To fully understand the code, please read the ATmega328 datasheet!

Sensorless BLDC motor control with Arduino video:
The video below shows how project is working.

References:
Microchip AN970 document
https://www.microchip.com/

42 comments

      1. In the video there is only 5 capacitors, but the circuit shows 6 capacitors and the arduino uno default timer2 frequency is ~61 khz, but you explained and coded default prescale, can u please provide the full report with the virtual ground resistor design. Please help me i am on my major project..

        1. In my circuit it should be 7 capacitors (the 6 capacitors shown in the schematic diagram and one between (+) and (-) of the 12V source). The biggest capacitor in the video is for 12V source. It could be done just as shown in the schematic diagram.
          The frequency of Timer2 PWM signal is about 31KHz because when it is ON its register TCCR2A = 0x81 which means WGM20 bit = 1 (PWM phase correct mode) and therefore: freq = 16000000/(N*510) = 31372Hz where N is the prescaler = 1 (TCCR2A = 1), you can check it with a real oscilloscope or even by Proteus simulation. All data are provided in the Atmega328P datasheet.

  1. Thank you, and what you used as power source Lipo battery or power supply. And the design of virual ground and back emf

    1. I used 12V power supply for both the Arduino and the 3-phase bridge, a battery (Li-ion, Li-po ..) can be used for powering both of them.

      1. I also used 12v power supply, but when i see the voltage across power supply dropping to 5v or lower when turning on motor and motor vibrates and try to turn but not rotating. Also tested with lipo battery but motor condition is same and i am using STP105N3LL mosfet. Could you please verify the program and also the circuit diagram,

    1. It’s A2212 1000KV, it’s well used in quadcopters, even PC cd-rom (dvd-rom) sensorless BLDC motor (spindle motor) works fine.

    1. It’s the analog comparator interrupt service routine (ISR), the analog comparator is used to detect zero crossing events and at any event the analog comparator interrupts the microcontroller in order to update the commutation state.

      1. Thanks for the quick reply! I understand that this is an interrupt service routine and that is updating commutation state. I just do not understand why “i” is decremented by 1 in “for” loop when ACSR.ACO is “1” or “0” and why loop is counting to 10.

        I suppose that these “if” statements make decisions regarding the rising or falling period of BEMF, because ACSR.ACO is in these cases going from 1 to 0 (rising period of bemf) and from 0 to 1 (falling period of bemf), but can not figure out why “i” is decremented by 1 and why is counting to 10.

  2. Hi! I’m having issues understanding the commutation code:
    1 – ex:
    void BH_AL(){
    PORTB = 0x08; // 0000 1000 >> PB3 >> PIN 11 ON
    PORTD &= ~0x28; // 0010 1000 CLEAR >> PD5, PD3 >> PIN 5 OFF, PIN 3 OFF
    PORTD |= 0x10; // 0001 0000 SET >> PD4 ON >> PIN 4 ON
    TCCR2A = 0; // Turn pin 10 (OC1B) PWM ON (pin 9 & pin 11 OFF)
    TCCR1A = 0x21; // 0010 0001 >> CLEAR OC1B ON COMPARE, PWM PHASE CORRECT 8 BIT >> PB2 >> PIN 10

    Why did we set pin PB3 when we clear it ?

    2-
    I am trying to convert this code to run on my hardware, the gate drivers have separate input for High and Low side FETs, I think I shall be able to do that with inverted output PWMs, any comments here ?

    1. See the post below, it’s a little bit different, I used IR2101 instead of IR2104 gate driver. The IR2101 has separated gate control inputs for high side and low side mosfets.

    1. You can try it by your self but doing that may cause some problems to the analog comparator because it’s used for zero crossing detection and the analog comparator share the same multiplexer with the analog-to-digital converter (ADC).

  3. It can work your design with the following engine I have .. N5065 1820W 320KV Outrunner Brushless Motor For Electric Skate Board DIY Kit New

  4. Hello. Nice work thank you! Do you think, is it possible to check that engine is started after start procedure? If engine will not start correctly or its blocked. I tried to check bemf but when engine is fixed, the comparator goes crazy…

    1. As you say, when stationary, there’s no BEMF signal at all, and since there’s no (defined) hysteresis on the comparator it’ll just be triggering on noise. The traditional means of detecting operation is a stall counter – increment a variable every millisecond or so using a timer interrupt, and resetting every time a correctly sequential commutation is detected i.e. rotation rather than oscillation. If there’s no correct sequence in say half a second then disable – or start a restart routine.

  5. Hello, very good job.
    It’s works perfect, i try it in my project and need to make some changes( revers, startup by button, current restart(when its high, and higher speed by make the pwm is higher) and therer some problems…..
    For example- try to make the revers by button- wrote some lines of code to stup the motor and use bldc move in another way- but the motor after perfect work stops after i press the button-and start work crazy……
    Can you help me with code( can and want to simmelar pay for it)
    can you connect with me by e-mail(for example)
    Best regards, Igor

  6. Good job! But if i use a power supply higher than 12v, what resistor should i use for the virtual point and voltage divider? If i dont make any change, can i damage my microcontroller?

    1. Of course you may damage your Arduino microcontroller. Make sure that the input voltage doesn’t exceed 5V (for the ATmega328P)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.