# Using hall sensors to calculate position

I have a geared DC motor with hall speed sensors. I want to count signals from the sensor to get position of the motor. Hall sensor has resolution of 12 CPR. Gear ratio is 1:810, which means that I don't really need very precise measurement to get close to the desired position.

But in reality it doesn't work. I run the motor at the same direction trying to get exactly 360 degrees. Sometimes I am close to the position I set, sometimes I am too far (like 300 degrees instead of 360).

The code is simple. I just attached the hall sensor to an interrupt pin and count raises of the signal.

Has anybody tried that? Does anybody knows some obvious problem which causes me to fail such miserably?

Here is the motor I am using:

• it is possible that some of the interrupts are not getting serviced. please post your code. (minimal code that demonstrates the observed problem) – jsotola Jan 14 '18 at 21:12
• The hall sensor may need external circuitry to produce a good signal. If you put a schematic of the pcb in your question (or hi-res photos of front and back of the pcb if there is no schematic in the documentation from the manufacturer) and post it on electronics.stackexchange you should get a good answer. – hauptmech Jan 14 '18 at 21:20
• connect just the sensor to your controller. load simple code to control an LED with the sensor, then slowly spin the sensor magnet. – jsotola Jan 16 '18 at 2:54
• Welcome to Robotics, Kons. You said, "The code is simple. I just attached the hall sensor to an interrupt pin and count raises of the signal." What code? Where is the code? Are you trying to drive the motor for X number of counts, or are you running the motor at full speed and counting the number of pulses? Have you checked the pulse train with an oscilloscope or similar? Are you seeing variable results under the same loading, or are your angles varying under load or at different speeds? What troubleshooting have you done? – Chuck Jan 16 '18 at 20:42

## 1 Answer

Are you running a PID loop to control the motor? Without a PID loop, the motor is going to be highly dependent on load and will likely stop differently every time. Also, since the sensor is relative position only, you will need a way to home the motor to be able to know absolute position.

If you need accurate absolute position, you could use a hall absolute position sensor.

• Looks like my initial problem was had 2 root causes: 1) I should have used both interrupts of the motor hall sensors (one produces some extra signals). 2) Without PID loop it was too inaccurate. I decreased the motor speed and now it looks way much better. Thank you for your answers! – borisovke Jan 17 '18 at 19:03
• Glad to hear you are making progress. – markshancock Jan 17 '18 at 20:43
• Note: A PID loop is pretty easy to do. One of the biggest questions would be whether you want the PID to control position or error. That is really dependent on your design. It sort of sounds like to me that you are looking for positional control. – markshancock Jan 17 '18 at 20:43
• @Kons, one produces some extra signals ... not "extra signals" .... you need to fully understand what you are dealing with if you want to have success – jsotola Jan 19 '18 at 3:14
• @Kons Are trying to decode a quadrature encoder yourself by capturing the edges using interrupts? If so, there are LOTS of things that can go wrong! One classic problem is edge jitter where the encoder produces a very fast chain of edges because the encoder stops right on one of the encoder transitions and ends up quickly going back-and-forth across it. Missing or mishandling the edges thus cause the position to appear to move even though it didn't. – markshancock Jan 19 '18 at 4:45