Skip to main content
jerry2
Associate III
April 30, 2022
Question

Measuring Motor RPM using Hall Encoder Mode of TIM2

  • April 30, 2022
  • 3 replies
  • 3911 views

I'm controlling a BLDC motor with an STM32F401 and am using TIM1 to generate the PWM waveforms to drive the motor and TIM2 in encoder mode to read the Hall encoder inputs from the motor.

This setup is driving the motor well, but I'm setting a lot of variance on the timings from TIM2. I have TIM2 set up to capture a count into CC1 every time it sees a new edge (rising or falling) on any of the three Hall inputs. I'm then using this count to compute the spin rate of the motor in RPMs.

To test this, I set up the motor running at 5000 RPM in open-loop mode (no changes to the PWM settings), and the unloaded motor is holding a very steady RPM as measured with an external laser tachometer. When I compute the rotation rate using the TIM2 CC1 count between Hall encoder edges, I get what looks like random noise between 4500 RPM and 5500 RPM, but when this is filtered, it's very close to 5000 RPM. See the plot below. The green trace is the raw RPM values computed from the TIM2 timer counts and the red trace is the averaged RPM.

What I wondering here is why measuring the time between Hall encoder edges shows so much variation? The motor itself is running very smoothly and steadily at 5000 RPM as measured by the tach. TIM2 is a 32-bit timer, so I'm not overflowing it (it would take ~50 seconds to overflow). Anyone have any ideas?

0693W00000LzZUsQAN.png 

3 replies

KnarfB
Super User
May 1, 2022

The diagram shows only very few different discrete rpm values repeatedly occurring in a random looking pattern. Why is ist?

At what speed does the TIM2 counter run? How does the raw sequence of TIM2 captured values (TIM2 CCR1) look like?

hth

KnarfB

jerry2
jerry2Author
Associate III
May 2, 2022

TIM2 is running at 84 MHz. I will collect and post a sequence of captured TIM2 CCR1 values soon.

waclawek.jan
Super User
May 1, 2022

What you see is probably asymmetry in mounting and sensitivity of the three Hall sensors. Try to calculate RPM by taking into account only every third rising edge (or don't use the XOR).

JW

Visitor II
June 27, 2024

Can you share the code for us to clearly inspect?