i'm trying to control a DC motor with PID. I have already made a code where i control the motor with an ultrasonic sensor (for distance) but now i need to run the motor at a given RPM (the Output). I did a lot of resarch and found that i will have to use the PID Library. But with my existing code i'm not sure if it's going to work. I know that i will have to modify PWM values by the Input on the analogWrite(). Can you enlight me on this PID? Here is my code:

int enableBridgePin = 10; 
int forwardPin = 11;
int backWardPin = 9;
int tickPin = 2;
const int trig = 5;
const int echo = 6;

int dt;
int distance;

void setup() {
  pinMode(enableBridgePin, OUTPUT);
  pinMode(forwardPin, OUTPUT);
  pinMode(backWardPin, OUTPUT); 
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  digitalWrite(forwardPin, LOW);
  digitalWrite(backWardPin, LOW);

  digitalWrite(enableBridgePin, HIGH);

void loop() {

  digitalWrite(forwardPin, HIGH);
  digitalWrite(backWardPin, LOW);

  if (distancefonction(dt,distance)<15 && distancefonction(dt,distance)>7){

int distancefonction(int dt, int distance) //function to show distance

void slowdown(){
  for(int i=255;i>=0;--i){
  • 1
    PID is a feedback loop control. So you first need feedback from the motor, e.g. some way to measure the actual speed/RPM of the motor. Only then you can use PID.
    – chrisl
    Dec 8, 2019 at 14:52
  • Does your PWM analogWrite(enableBridgePin,i); work as desired ? ( A minimal but complete and compilable code is appreciated :) Dec 8, 2019 at 15:40
  • @chrisl the motor returns the tickPin
    – kevin0304
    Dec 8, 2019 at 16:03
  • 2
    What does the motor with trickPin? You haven't explained this, nor do I see anything in your code. Are you already reading the current RPM back from the motor, or is this be actual problem?
    – chrisl
    Dec 8, 2019 at 16:42
  • 1
    @chrisl I can't read RPM on the motor. The tickPin is actually a timer pin connected to channel A (motor with encoder), i think everytime there is an impulsion the timer increments (i'm not sure abt that)
    – kevin0304
    Dec 8, 2019 at 17:52

1 Answer 1


PID- or any similar control needs

  • set point
  • actual value
  • controlling variable (your PWM value)

The difference between set point and actual variable and its change over time are combined to calculate the controlling variable in order to minimize that difference.

Control theory in general is too complex to answer the "how to" here in general.

Besides measuring the actual value you also need knowledge about the behavior of your system: e.g. If you change the PWM value from 50 to 100, how much and how quick will the motor speed change? Even the response form over time will affect the optimal parameters for your control loop. It's called (feedback) loop because any change of the PWM value will change that difference and lead to a different new value of the controlling PWM value again (Allowing for instable behavior in case of bad controlling parameters).

Getting the actual value is your first task. How your distancefonction affects the desired value (set point) might also need more clarification...

  • thanks for the explanation. Actually 'distancefonction' just displays the distance between the object and the sensor. what i don't understand is how i'll get the actual value? the setpoint is 100rpm
    – kevin0304
    Dec 9, 2019 at 8:22
  • One of the simpler ways is a optical wheel, or even just a small 'flag', attached the motor shaft, that interrupts a light beam every revolution (or part of one). Time the pulses and convert to RPM.
    – JRobert
    Sep 5, 2020 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.