Brushed DC motor with Hall Effect Sensor

Hi. I am trying to make a linear actuator which uses a brushed dc motor and hall effect sensor for position feedback. Also for friction I have to also implement PID controller. What I'm confused about is that how will I be able to implement PID using hall effect sensor? I will be using 4 magnets (with 2 North and 2 South poles) on the screw and one latched hall sensor so every revolution gives me 2 counts.

So basically what I'm trying to do is to input the hall sensor count on the computer so that it can go to that specific count and stop the motor (Plus using PID altogether). Would really appreciate any help. I can't change this setup or motor. Any help with programming is also helpful.

Thank You

Normally a PID sets a certain motor speed, but you need a position. Your position is in "counts" or in "rotations". Suppose you need to go 10cm, then you have to go to perhaps 230 counts. So the PID can be used to go to "position" 230. The output of the PID for the motor will be zero when the 230 is reached, but that is no problem, the PID will take care of that.

You can not supply the hall sensor ticks directly to the PID, you have to supply the position in "counts". So you have to determine the zero position very careful. And you have increase and decrease the total count of the hall sensor ticks, and feed that to the PID. So the input of the PID is the position.

How do you determine which way the motor is rotating ?

I would read the inputs from your sensors and use that to maintain a step counter that holds your current position. That will be the input to the PID algorithm. The target position will also be defined in steps. The output from the PID will be a motor demand which will be a positive value to drive the motor one way and a negative value to drive the motor the other way. You will need to write some code to set the motor direction from the sign of the motor demand and set the pwm value from the magnitude.

Since you’re only detecting movement not absolute position, you will need some way to define the absolute position at startup. For example you might have an initialisation sequence which drives the actuator one way until it hits a limit switch or mechanical stop, and then zero the current position.