DC Motor Position Control using Potentiometer as a Feedback
Show older comments
Hello,
I am trying to control the position of a dc motor using a potentiometer as a feedback (on simulink). The idea is to input an angle from 0 to 180 degrees and then motor will spin clockwise or anti-clockwise (depending on it's physical position) until it gets to the desired angle. My goal is to implement a P controller on this process (getting to the desired angle). The problem is that I am confused on how to express the problem on it. At first I have a similar setup as the one in Picture 1. After that, in Picture 2, I have expressed the dc motor's speed with a constant and I have used Digital Pin 9 for PWM. So 0 means that dc motor will not spin and 255 means that dc motor will spin at its highest speed. Potentiometer is connected at pin 5 and its value is shown on the simulink's display. Both dc motor's speed and potentiometer's value are working and I would like some suggestions at Picture 2. My idea is to set a desired angle as a constant, do you think that it will work with a constant block? Also, I am confused on how am I going to connect dc motor's speed and potentiometers value with my P controller, can you suggest me a way? One last thing is, how can I get the current angle and then fix the error in order to get to the desired angle, any ideas?
Thank you so much for your time, appreciate your help !
Excuse me for any mistakes !


Picture 2: Thoughts of the model in Simulink
33 Comments
Mathieu NOE
on 5 Jan 2022
hello Christos
I have the following recommendations :
before attempting to put the controller in place , you have to be sure that you are able to spin your motor in both directions (which is not yet the case as far as can see here) and in a proportionnal manner (through the PWM signal)
so the control of you motor requires you to be able to transform a given positive or negative control signal to the right PWM and CW /CCW rotation signal (I will not dig into how to do it at the L293D driver level, there are enough ressources about that topic)
that was for the motor
for the potentiometer sensor , your signal is a voltage signal that must be first converted to a angular representation; assuming the pot has a linear output (vs actual position) this equation must be simple (affine)
from there you have to make a conversion block that will convert back the voltage reading to position
then you can easily buid a classical feedback loop (can be P controller or more advanced controllers)
- position error = your input - measured position (after voltage to angle conversion)
- P controller output = K * position error (NB : this command signal can be positive or negative , hence my first recommendation about knowing how to control your motor speed in amplitude and direction)
Christos Tsallis
on 5 Jan 2022
Mathieu NOE
on 5 Jan 2022
hello again
for the sensor it's fine now;
for the motor control : from what you say I guess IN1R1 and IN2R1 will define the motor rotation direction depending if they are "1 0" or "0 1" (I have not really looked at the driver specs)
see below : you can now control the speed and direction without any manual intervention :
I used a sinus wave input as an example (with max amplitude =1 here) , this will be in the future the output of your controller
- the speed control is defined by the abs block output (multiplied here by 255 to represent the PWM)
- the direction is given by the sign block that drives two switch blocks (these are the two IN1R1 and IN2R1 signals)


the simulink file is attached if it works for you
Christos Tsallis
on 5 Jan 2022
Edited: Christos Tsallis
on 5 Jan 2022
Mathieu NOE
on 5 Jan 2022
there is one thing that still needs to be done before we decide what controller is appropriate to do this task
we need to know what is the model or transfer function between you command signal (PWM) and your position reading
I would suggest first to make a step response (PWM switch from zero to a non zero value at given time value) and record both command and angular signals together
now I am curious how the angular sensor step response looks like
can you do this experiment and share the result ?
Christos Tsallis
on 5 Jan 2022
Edited: Christos Tsallis
on 5 Jan 2022
Mathieu NOE
on 6 Jan 2022
hello again
it's a bit confusing ... you say you are rotating the pot by hand ?? i thought the pot was only linked to the motor shaft ?
the step response measurement means the step signal must be applied to the motor and we measure this signal and the pot reading simulteanousy
why turning the pot manually ? it was supposed to be done by the motor under a step signal command ...
Christos Tsallis
on 6 Jan 2022
Edited: Christos Tsallis
on 6 Jan 2022
Mathieu NOE
on 6 Jan 2022
as you post only a fraction of the model (picture), I cannot see if the test is done open loop or closed loop (with P controller)
can you clarify ?
Christos Tsallis
on 6 Jan 2022
Edited: Christos Tsallis
on 6 Jan 2022
Mathieu NOE
on 6 Jan 2022
sorry - again a few basic questions
1/ are you able to control your motor in speed and direction now ? I mean positive and negative u1 are giving expected rotationnal speed amplitude and direction ?
2/ the u2 input in the IF block is not used I assume ? or what was the intention (I don't understand why it's connected directly to your angular input
3 / have you tried to reduce / increase Kp and see the effect on transient response (how fast the otor reacts) and the steady error ?
4/ your steady error will be reduced if you can use a larger K gain but within the limits of stability (some chatter may arise if you are too strong)
also the PWM resolution may limit your position accuracy ; can you increase the PWM resolution ? or a geared motor would be more precise so you would see less wandering around the position target value
same for the accuracy of the position sensor voltage reading : accuracy of your ADC converter can also limit here how accurate your loop will be
Christos Tsallis
on 6 Jan 2022
Edited: Christos Tsallis
on 7 Jan 2022
Mathieu NOE
on 7 Jan 2022
hello
I am not sure the driver is able to provide lot of improvement on the PWM resolution
there is some info here : if you could reduce the PWM carrier frequency this translate into better resolution
another point to watch is the freeplay in your geared motor (freeplay is your ennemy, will create inaccurate control & oscillations), try to reduce that or introduce a way to make sure the teeth are always in positive contact (preload spring or use a weight (gravity) to introduce some loading torque on the output shaft.
if you cannot improve your current motor system , maybe pick a geared motor with high ratio gear (worm gear) and minimal freeplay (or add some loading on the output)
in the control logic you can add a "dead band" block on the position error signal, so when you have reached steady state, the small oscillations are zeroed by the dead band block; this solves the oscillation problem but you are still with some inaccuracy of the control
Christos Tsallis
on 7 Jan 2022
Edited: Christos Tsallis
on 7 Jan 2022
Mathieu NOE
on 8 Jan 2022
hello
normally , the dead band "amplitude" should be selected to be slightly larger to the oscillation amplitude you have before you implement it
what was you "best resukt" obtained so far ?
have you tried to load the shaft to reduce the free play ?
what is your gear ratio ?
Christos Tsallis
on 8 Jan 2022
Mathieu NOE
on 8 Jan 2022
well , even if not good , what is the result right now ?
Christos Tsallis
on 9 Jan 2022
Edited: Christos Tsallis
on 9 Jan 2022
Mathieu NOE
on 9 Jan 2022
hello
I just figure out now that your previous implementation with the IF block was not making any proportionnal control of your motor as your are always driving the motor with a fixed PWM value ( = 65), just changing the direction;
now this new version is a bit different as now you are using the range (-65 to 65) together with the sign of the PID signal driving the direction pins
nota that all you did isnside the red rectangle is correct but is basically what a simple abs block can do in a much simpler way

also, now that the PWM is proportionnal to error why do you limit the PWM to 65 ? you are limiting yourself to lower speeds ?
if you want to reduce steady state error you can try using the integral gain of the PID
the derivative term is for me not needed in this case because your "load" is not a resonant system (like a mass - spring system)
again it would have been interesting the measure the open loop step response to do a bit of PID tweaking (matlab script) - but sure you can also make trials and error method on the bench directly
Christos Tsallis
on 9 Jan 2022
Edited: Christos Tsallis
on 9 Jan 2022
Mathieu NOE
on 10 Jan 2022
hello
this model is correct for me
the gain block (x 255) can be used but this is equivalent to put this gain inside the PID gains - so there is not "benefit" or "drawback" using this block ; at the end, your performance is given by the open loop gain (i.e the product of gain of all blocks, so the controller's gain is either fully inside the PID block or you can split it in two but again no benefit of doing so)
the PID output saturation depends only if you really need to saturate the command to the motor to keep RPM or power consumption below a given level; on the drawbacks I would say it limits your time to reach the target angle (it acts as a slew rate limiter); can be needed but I leave it to you to decide if it's necessary or not; it will not have any influence on the accuracy or noise as this is more a question of PWM resolution / free play and gear ratio
Christos Tsallis
on 10 Jan 2022
Edited: Christos Tsallis
on 10 Jan 2022
Mathieu NOE
on 11 Jan 2022
Edited: Mathieu NOE
on 11 Jan 2022
hello again
I would like to make a test to measure the actual transfer function between command and measured position
this is an open loop test , so the PID should be removed
can you try record 10 to 30 seconds of data if you put a low amplitude random noise at the motor command signal
in the data should be the command signal and the position signal
from there we can build a model of your process and do a bit of controller optimisation
this is more "scientific" rather than doing by trial and error (maybe sometimes you are lucky, sometimes no)
Mathieu NOE
on 11 Jan 2022
Christos Tsallis
on 11 Jan 2022
Edited: Christos Tsallis
on 11 Jan 2022
Mathieu NOE
on 11 Jan 2022
hello again
you can build either your model from the equations , but you can also do it experimentally (this was my suggestion)
the open loop test means that before you even put in place any controller , you must first have a good model of your process - either by theoretical or experimental approach
this is how you should make your model for a open loop test
the random number block (red) is used to "shake" your system with a kind of broadband (white ) noise
we record simulteanously both this command signal along with the response of your system (the position signal)
from there we will build a model of your system and that will be the start point of the PID optimisation

Mathieu NOE
on 11 Jan 2022
forgot to mention : maybe add a gain block after the random generator so you can easily tune the amplitude of the broadband signal so that the system is shaken with a reasonnable amplitude , not too low, not too high.
make sure that what you record is the broadband signal after the gain block
Christos Tsallis
on 11 Jan 2022
Edited: Christos Tsallis
on 11 Jan 2022
Mathieu NOE
on 11 Jan 2022
hello
yes it can ba bit tricky to center the motor at the potentiometer mid position before you do the test
you can probably do that by giving some positive / negative hort duration maunal commands
to your record
I am quite a bit surprised by the look of the position sensor signal. I was expecting something like low pass filtered version of the input signal , so it should be more centered around a mean value. I wonder alsowhy we have these "flat" responses from time to time (as if the shaft would not move at all ? ) and also abrupt vertical changes
is the potentiometer a bit "sticky" so that the motor has difficulties to turn in a precise and stick slip free manner ?
or have we made any mistake in the control logic (amplitude and direction ?)

Christos Tsallis
on 11 Jan 2022
Edited: Christos Tsallis
on 11 Jan 2022
Mathieu NOE
on 11 Jan 2022
Edited: Mathieu NOE
on 11 Jan 2022
Ok , so it means we have a situation where it's not easy to do "open loop" testing
I would try another option, so let's put the "best" PID controler back and now the random signal is applied again as a position command input (replaces the step block)
the data we need to measure for the model is
- the PID output (which is now a closed loop command signal to the motor)
- the position sensor (as before)
the benefit of having a "almost" working PID is that it will keep the shaft centered around a mean value and there should be no risks of hitting the potentiometer ends - if the input amplitude is not too large
Christos Tsallis
on 11 Jan 2022
Edited: Christos Tsallis
on 11 Jan 2022
Mathieu NOE
on 12 Jan 2022
yes , basically replace the step block by the random generator and record the PID output along with the position sensor signal
Answers (2)
Christos Tsallis
on 12 Jan 2022
0 votes
5 Comments
Mathieu NOE
on 12 Jan 2022
hello
I have the feeling your geared motor is not able to turn the pot at low speed / low torque (tends to stick in place)
that's why we get these "flats" in the response when the PWM is in the low range
I haven't check either that the arduino board can supply enough current to make your motor turn in all cases
that could explained why we have so much difficulties to get a clean response and why the PID cannot really give you the expected results in terms of accuracy and steady state response (without oscillations)
maybe the pot is too hard to turn for your geared motor , I am not sure how much torque is needed to make the pot turn. that could be the major issue here
as I am a bit involved in the hobby and I am using rc servos , I think it's worth looking at how rc cars / planes are made , basically the same ways as your system but with a higher gear ratio , less free play (maybe) and better motors (maybe )
why not buy a cheap rc servos (take analog rc servo with plastic gears ) , you can easily unsolder the tiny electronic circuit and solder the motor and pot to your arduino board

Christos Tsallis
on 12 Jan 2022
Edited: Christos Tsallis
on 12 Jan 2022
Mathieu NOE
on 13 Jan 2022
hello
yes , I think this servo can do the job. I'm curious to see the results with this one.
all the best
Christos Tsallis
on 27 Jan 2022
Mathieu NOE
on 28 Jan 2022
hello
ok - sounds good
I see the gear ratio is only 1/52
all the best for the future
Noor Amira Ilyanie
on 4 Aug 2022
0 votes
what is 0 to 255 unit? is it voltage or rpm? or bit?
Categories
Find more on Motor Drives in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

















