Showing results for 
Search instead for 
Did you mean: 

Wrong period values during motor startup.

Associate III

I am making a BLDC motor controller using STM32G030C8t6. This microcontroller has a hall sensor sensing capability known as ‘Hall Sensor Mode / XOR Mode’. This generates an interrupt when any one of the three channels toggle. We are taking the timer period between this interrupts.

This period value is used for the calculation of SVPWM (Space Vector Pulse Width Modulation). Using this value, we are calculating the angle within sector. While debugging, we found that the motor vibrates slightly during the motor startup, So we are getting improper period values.

Untitled (2).png

The blue line is the period value, and green is how it should have looked according to motor speed. As in this image, due to vibrations during the initial stage, we get multiple interrupts, this messing up the period values.

This is the part of code where we are measuring the period value.

// Function to measure the period of a hall sensor signal
void getMotorPostion(){

    // Check if it's the first capture
    if (0U == Motor.FirstCap) {
        Motor.FirstCap++; // Increment the first capture flag
        (void)LL_TIM_IC_GetCaptureCH1(TIM3); // Get the first capture value
    } else {
        // Get the current capture value
        uint16_t CurrentVal = LL_TIM_IC_GetCaptureCH1(TIM3);

        // Calculate and update the moving average speed
        // Clip the current value to ensure it is within the specified range
        if (CurrentVal < MIN_HALL_PERIOD) {
            CurrentVal = MIN_HALL_PERIOD;
        } else if (CurrentVal > MAX_HALL_PERIOD) {
            CurrentVal = MAX_HALL_PERIOD;

        // Update the Motor structure with the calculated period
        Motor.Period = CurrentVal;


The following function shows the implementation of the SVPWM calculation.

// Function to execute SVPWM calculation at a frequency of 20 kHz
void Fastloop() {

    // Increment the speed PI counter

    // Check if the counter has reached the maximum value
    if (motorVar.speedPI_counter >= motorParam.speedPI_maxCounter) {
        motorVar.speedPI_counter = 0; // Reset the counter
        speedPI(); // Execute speed PI control

    // Execute current PI control

    // Calculate and update the moving average of the motor period
    // Get the average period from the moving average filter
    uint16_t AvgPeriod = movingAvgFilterPeriod.avg;

    // Check if the average period is not zero to avoid division by zero
    if (AvgPeriod != 0) {
        // Calculate the phase increment based on the average period
        Motor.phaseInc = ((long unsigned int)PHASE_INC_CALC / (unsigned int)(AvgPeriod));

    // Update the motor phase by subtracting the phase increment
    Motor.Phase = Motor.Phase - Motor.phaseInc;

    // Execute Space Vector Modulation (SVM) with the current PI output and updated motor phase
    SVMX(motorVar.currentPI_output, Motor.Phase);
cedric H
ST Employee

Hello @Gladson ,

I did not find any questions in your post. Are you asking the community to check your code ?

Please understand that we do our best to provide you the best support for our products. In the Motor control area, our offer is both hardware and software. On software side our offer is the MCSDK that you can download here.

On hardware side, the STM32G030 documentation is fully available on

We unfortunately do not have the bandwidth to dive into user code. If you think your issue comes from a MCU feature, do not hesitate to ask a specific question about it.

In your specific case, be aware that the electronic between the hall sensors and the MCU is important and could be part of the issue, and last point, do not forget the angle used in your SVPWM is an electrical angle, and what you measure with your hall sensors is a mechanical angle and both must be aligned (We call it Hall electrical phase shift).

This is explained in the documentation of our MCSDK.



Associate III


Sorry for the bad composition of the earlier post. I am making a custom motor control program.

The problem I am facing is that due to the motor vibration at start, hall interrupts are generated in quick successions. So, the period value is very low. We are calculating the electrical angle based on this period value, but since it is wrong, the whole SVPWM calculation is getting messed up.

So, when period value is not correct, how should I calculate electrical angle?


I faced similar issues very early on and it was due to noise in the hall sensor signal lines. Probe the MCU Pin nodes, especially during runtime if you see fluctuation in the signal crossing 1/2Vcc it could be triggering "HALL" Timer Interrupts. Use a good filter network. May be even use a schmitt trigger.

I also faced similar startup issues when I had a motor connected to load with high amount of mechanical backlash.
I had to rewrite the rotor position feedback logic, which was intense.....

Good Luck.

Associate III

I have added RC filter on the hall signal lines, and have tested them on DSO, there is no noise present on the signals which could cause multiple interrupts in my case.