cancel
Showing results for 
Search instead for 
Did you mean: 

Measuring Motor RPM using Hall Encoder Mode of TIM2

jerry2
Senior

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 

4 REPLIES 4
KnarfB
Principal III

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

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

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

mcelik19
Associate

Can you share the code for us to clearly inspect?