Library for DC Motor with positional feedback and acceleration


I am using a DC motor which is connected to a spindle combined with a string potentiometer for positional feedback. I want to be able to move the motor to a predefined position and use acceleration and deceleration to get there. The speed of the motor will be controlled with a PWM signal.
Programming this all be myself would be possible, but I am sure there is already a library to do that, which will be a much easier and probably a more reliable solution. But so far I was only able to find libraries for stepper motors or servo motors. Or could I use a library for servo motors? If so, which one would be best suited for my task?

Thanks in advance.

Welcome to the forum.

That is a specific use of the DC motor. I have not heard of such a library.
There are continuous servo motors that can set the speed, but that would make it unnecessary complex. You might have to write the code yourself.

Can the motor go forward and backward ? Do yo have a direction signal + pwm signal ?
Linear acceleration and deacceleration ? I think that is good enough.
So there is a maximum speed and the steepness of the ramp.
With a ramp, you need timing.

Do you have an idea how to do the timing ? millis ?
How often do you want to change the PWM value ?
It is easy for a led, since the human eye is slow, so 10Hz...50Hz update is good. I used 25Hz here (the Uno has a PWM of about 500Hz).
It is also easy for a servo motor, their position is often changed every 10ms or 20ms. I used 10ms here.

A analog input has noise. You can take the average of a number of analogRead() and perhaps a little low-pass filter in software. If there are vibrations because of the motor, then a low-pass filter should solve those problems.
I suggest to use a value of 0...1 or 0...100% as a floating point number for the position. Then the sketch is easy to read.

[ADDED] A PID control for the motor might be possible, but that also needs tuning and I think that overshoot is really bad.

Thanks for the quick response!

Yes, the motor can go forward and backward. I plan to use the Pololu G2 Motor Driver, they have a PWM and DIR pin.
Linear acceleration and deceleration should be fine, I don't need any complex acceleration curves.

I plan to use millis for the timing, but I am not sure yet how often I want to change the PWM value. The idea so far is to start with a PWM signal of 0 and then increase it in every cycle by 1 until I reach the maximum value (255 or maybe a bit less).
But then I still have to figure out when I should start to decelerate and also make sure that I can actually reach the maximum velocity, or otherwise stop accelerating, so I have enough time to decelerate again.

You use a PID controller library to provide a feedback loop - the potentiometer reading provides the current position, compare this to a set point as the inputs to the PID, and the PID output is converter into a drive signal for the motor driver in question.

PID loops are standard components of motion control systems.

Thanks for the suggestion. However I do not fully understand how to do that in practice. All the examples I found online used an encoder for measuring the rotations of the motor, and I am not sure how to apply this to my setup with the potentiometer for absolute positional measurements. Do you maybe have a short example?

I would like to see a picture or drawing of how you are using a "string potentiometer". I have never seen such a device.

No one on this forum does either, because you did not describe it. For useful help, you need to provide useful details.

Here is a demo code that will position a gear motor connected to a (string) pot to a position controlled by another pot. It is a simple proportional only control. The string pot is the feedback element and the control pot the control element. No acceleration though. I leave that to you.

#define CW 1
#define CCW 0

const byte controlPotPin = A0;
const byte mtrPotPin = A1;
const byte mtrPwmPin = 5;
const byte mtrDirPin = 4;

void setup()
   pinMode(mtrPwmPin, OUTPUT);
   pinMode(mtrDirPin, OUTPUT);
   digitalWrite(mtrDirPin, CW);
   //analogWrite(mtrPwmPin, 140);
   digitalWrite(mtrDirPin, CW);

void loop()
   int mtrPot = analogRead(mtrPotPin);
   int controlPot = analogRead(controlPotPin);
   int error = mtrPot - controlPot;
   if(error < 0)
      digitalWrite(mtrDirPin, CW); 
   else if(error > 0)
      digitalWrite(mtrDirPin, CCW); 
      digitalWrite(mtrPwmPin, 0);
   analogWrite(mtrPwmPin, abs(error));

void printPots()
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
      timer = millis();
      Serial.print("motor pot ");
      Serial.print("   control pot  ");

Tested with this circuit. The string pot is represented by a 5K pot directly connected to the output shaft of the gear motor.

That's just P control, PID will enable much better performance.

Thanks for the responses so far! I thought the explanation in the first post was understandable but now I see how this can be interpreted in different ways.

The schematic looks like this:

For better understanding how a string potentiometer works:

The end of the string potentiometer is connected to a "gantry" that is moving back and forth. The potentiometer then outputs different analog values according to the position of the gantry. I want to send a value/position to the microcontroller and then move the gantry to this position by comparing the value I sent with the analog value from the potentiometer. While also accelerating and decelerating. I think I now have an idea of how it could work with PID control.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.