Learn how to interface a **BLDC Motor with STM32 microcontroller** step by step. In this tutorial, we cover motor driver setup, PWM generation, Hall sensor feedback, and speed control logic. Perfect for robotics, drones, and automation projects.

🔽 DOWNLOAD CODE & FILES: [https://controllerstech.com/how-to-interface-bldc-motor-with-stm32/]

📺 RELATED VIDEOS
- https://youtu.be/OwlfFp8fPN0
- https://youtu.be/WMS0t9WGqVw
- https://youtu.be/Mh-itQEaeyM

STM32 Playlist : https://www.youtube.com/playlist?list=PLfIJKC1ud8gga7xeUUJ-bRUbeChfTOOBd

#STM32 #BLDCMotor #MotorControl #STM32Projects #EmbeddedSystems
________________________________________________________________________________________

Facebook Page : https://www.facebook.com/controllerstech

Telegram Group : https://t.me/controllerstechdiscuss

Instagram : https://www.instagram.com/controllerstech/

For more info, visit https://controllerstech.com
Transcript
00:00Hello and welcome to controllers tech. Today in this video we will see how to
00:14interface a BLDC motor with SDM32. This video will only cover the interfacing of those motors
00:22which are connected with the electronic speed controller or ESC. If you don't have the ESC
00:29and directly want to control the motor, this video is not for you. The LDC motors can be
00:36controlled via the very similar process as we used to control the servo motor. I am going to
00:42use a potentiometer connected via the ADC to control the speed of the motor. We will see that
00:49but first let's see the connection. Here is my setup. I have the 30 ampere ESC which is powered
00:58by a 12 volts adapter. The ESC is connected to the 1400 kV motor. The two side pins can be connected
01:08to either side of the motor as it controls the direction of rotation. The center pin from the
01:14ESC should be connected to the center pin of the motor. Here the yellow wire is the center wire and
01:21the other two you can connect however you want. Three pin header cable from the ESC is responsible for
01:28controlling the speed of the motor. This is just like the three pin wire we see in a servo motor,
01:34with red being the VCC, brown being the ground, and yellow is the signal pin. The VCC and ground have
01:42the potential difference of 5 volts, so you can use this to power the controller. I have connected them
01:48to the 5 volt pin on my F103 controller. The signal pin is connected to the pin PA8, which I will set as
01:57the PWM output pin later. I have connected one potentiometer to the pin PA0, and this will be used
02:05to control the speed of the motor. This is it for the connection, let's understand how this is going to
02:12work. As I mentioned, the BLDC can be controlled in the same way as the servo motor. I have already
02:20written a tutorial about the servo motor, so we will follow the same. As I mentioned here, we need a PWM
02:28signal of time period 20 milliseconds, or a 50 hertz frequency. Then the speed can be controlled by
02:35varying the pulse width between 1 millisecond, to 2 milliseconds. That is all we need to do.
02:43Let's see how we can do this with the F103 controller. Start the ID, and create a new project.
02:51I am using the SDM32F103C8 controller. Give some name to the project, and click finish.
03:00First of all I am selecting an external crystal to provide the clock. The board has 8 MHz crystal on
03:10it, and I want to run the system at maximum 72 MHz. I am also using an ADC for the potentiometer,
03:18and I want to keep the ADC clock to minimum. This is because I don't want the ADC to trigger
03:24the interrupts at a very high rate. I have already explained the ADC conversion time in another video,
03:31you can check that out. Anyway, this is the minimum I can set, so let's go with it.
03:38Note that the APB2 timer clock is at 9 MHz now. I am going to use the timer 1 for the PWM,
03:47which is connected to the APB2 bus. So the APB2 timer clock is going to be the base clock for our timer.
03:55Select the debug as serial wire, and time base as sys tick. Let's configure the ADC first.
04:03I am using channel 0, and remember that the potentiometer is connected to pin PA0.
04:10I want the conversion to happen continuously, so enable it. As I mentioned I don't want the interrupt
04:16to trigger at a very high rate, so I am selecting the maximum sampling time.
04:22We will let the DMA handle these conversions. Make sure to enable the circular mode.
04:29F103 has a 12 bit ADC, so half word is good enough. The DMA interrupt is enabled,
04:37so we are good to go. Now let's configure the timer 1. I am using channel 1 as the PWM generation.
04:45Note here the pin PA8 is selected as the PWM pin, and this is where I have connected the signal pin
04:52of the ESC. Timer 1 is connected to the APB2 clock, which is at 9 MHz right now. Let's understand the
05:02calculations before we configure the timer. We know the timer's output frequency can be calculated using
05:09this formula. Now if I want the output frequency of 50 Hz, I can use the prescaler of 180, and the
05:17auto reload of 1000. Using auto reload of 1000 is better instead of 100, because our pulse width is
05:25small, and so does the duty cycle. With the reload value of 1000, we can accumulate more values for
05:33the duty cycle, as compared to the value of 100. You will understand this in a while. So we know the
05:41minimum pulse width is going to be 1 ms, which is 5% of the 20 ms. But our auto reload is at 1000, so a 5%
05:51duty means we have the value 50. So 1 ms is equivalent to the value 50 in our case. Similarly 2 ms will be
06:05equivalent to the value 100. Now since we have the auto reload of 1000, we could use the values from 50 to
06:13100, but if we had 100 here, we could only use the values from 5 to 10. This is why I kept the auto
06:21reload at 1000, instead of 100. Alright let's set the values. The prescaler is 180, and auto reload is 1000.
06:34We will leave the rest of the setup to default. This pulse value will be changed, during the runtime itself,
06:42based on the input we get from the potentiometer. This is it for the setup, click save to generate
06:49the project. Let's write the code now. First we will start the timer in PWM mode. I am using timer 1 channel 1.
06:59Now start the ADC in the DMA mode. We will define the ADC data variable in a while, and there is only one conversion.
07:15Define the variable to store the ADC value.
07:18We need to convert the raw ADC value to our range of PWM values, so we will store the converted values in this variable.
07:29Now when the DMA will finish the conversion, the conversion completed callback will be called.
07:40Let's copy this definition. I am using the map function from the Arduino source code,
07:46to map the ADC values to our range. The values will be stored in the converted variable.
07:55We want to map the ADC data variable value, the minimum value of this variable can be 0,
08:01and the maximum can be 4095, since it's a 12-bit ADC value. The minimum output value can be 50,
08:10and the maximum can be 100. This is as per the duty cycle we calculated earlier.
08:18So basically, when the potentiometer reads 0, this function will output 50,
08:23and when the potentiometer is at maximum 4095, this function will output 100.
08:31Now we need to send these values to our timer, so that the duty cycle can be varied.
08:37I am using timer 1 channel 1, so the CCR1, that is capture compare register 1.
08:45You can use another capture compare register, if you are using another channel of your timer.
08:51If you are familiar with BLDC motors, you might know that we also need to calibrate the ESC.
08:58This code will do that for you. Basically we set the maximum duty,
09:03that is the pulse of 2ms and wait for some time. The ESC will sound the beep indicating it has been
09:11calibrated for the high pulse. Now send the lowest duty, that is the pulse of 1ms, and again wait for
09:19some time. The ESC will again sound the beep indicating it has been calibrated for the low pulse.
09:28I have defined this at the top of the file, so that you can enable this only if you want to calibrate.
09:33The start DMA function takes a 32-bit variable, so let me typecast this.
09:48Alright everything is set now, let's debug the code.
10:04I will use the motor later, first we will see the results in the debugger and the logic analyzer.
10:10We will monitor the ADC converted variable. Here the potentiometer is at 0, so the value is 50.
10:20And if I move the potentiometer to maximum, the value has been increased to 100.
10:25Let's, see the pulse timing on in the logic analyzer.
10:33I have connected the pin PA8 to the channel 0 of the analyzer.
10:43Here you can see the pulse remains high for 1ms out of 20ms period.
10:50And now I am rotating the potentiometer to the maximum position.
10:55Now we have the pulse with the width of 2ms. Here the value of the variable is 100.
11:02So the pulse timing is correct, let's switch on the motor.
11:06I will disconnect the controller, so that it will directly get the power from the ESC itself.
11:22You can hear the set of beeps produced by the ESC, it means the calibration is completed.
11:28Let's rotate the motor. You can judge the speed by listening to the sound it is making.
11:36be
11:44for
11:48you
11:51at
11:55a
11:58a
12:02a
12:05So the motor is rotating fine, and we were able to control it using the potentiometer.
12:14If you don't want to do the calibration, just set this definition to zero, and this
12:19particular code will be excluded.
12:22This is it for the video.
12:24I hope you understood how we can control the BLDC motor using the SDM32.
12:30You can download the code from the link in the description.
12:34Leave comments in case of any doubt.
12:37Keep watching, and have a nice day ahead.

Recommended