Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

DC Motor Position control with Arduino (PID)/Encoder


In my project, I’m using Arduino UNO and Adafruit motor shield (pololu.com/product/1103/specs). I was trying to do a position control of my DC motor without using encoder which is already attached to the motor, but controlling it via Arduino code as it appears in the attached code. however, I couldn’t achieve the required motion of the DC motor which is (Raising from 0 to 90 degrees, holding , and lowering back from 90 to 0 degrees, for 5 seconds for each stage).

The question, should I use the encoder with PID control to achieve this exact motion? or can I do that with just the arduino code?
In case I need to include the encoder, What do I need to integrate PID control into my project?
Finally, how can I hook up the encoder/PID to my DC motor? Do I need to include an other hardware other than my Arduino and motor shield?

My goal is to drive the DC motor at low speed without loosing torque and in the specific motion as mentioned above.


[code]#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_PWMServoDriver.h”

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which ‘port’ M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

void setup() {

AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz

void loop() {






I am not entirely sure what motor you are talking about. You linked to a gearmotor that does not have an encoder, but also mention that you are using a motor “without using encoder which is already attached”.

I am also not sure whether your goal is position or speed control. However, you would be able to get more accurate control of either if you use encoder feedback. There are many useful articles about PID control on the Internet. (You can start with this Wikipedia article). You might consider reviewing our Wheel Encoder library, which is described inside our AVR C/C++ Library. This forum post by Ben explains how to tune PID constants.



This is really strange!! It seams to be that you didn’t see my entire previous post? This is the motor I’m talking about (29:1 Metal Gearmotor 37Dx52L mm with 64 CPR Encoder) and it does have encoder with it as you can see from this link (pololu.com/product/1443).

I’m looking for precise position and speed control for my DC motor. At the current situation, I’m able to run my motor forward, hold, and backward, but I still need to control the speed which needs to be slow and the position which needs to be forward for 0 to 90 degree and backward from 90 to 0 degree.

Thanks for the support


Since you already have a brushed DC motor, using encoders would be a good way to get more precise control over speed and position. However, you might also consider using stepper motors, which might be easier to use for slow and precise movement.



I bought this DC brushed motor (pololu.com/product/1443/specs) from Pololu company, and I need to control the position and the speed for my project, but I don’t have its tuning parameters Kp, Ki & Kd for the control! I need to have the operational parameters of this motor to be able to control it.
So, would you please send me these parameters?


It sounds like you might not understand how PID tuning works. Those parameters are not a property unique to a gearmotor. Instead, they are parameters used to define a feedback system. (The gearmotor can be a part of the system, and its characteristics can definitely affect those parameters.) You can learn more PID tuning in this Wikipedia article.



what did you do for your project?
because i have same one
if you dont mind help for code