enter image description here

Above illustration is the basic setup for the question. The scenario will be the following:

A potentiometer(providing 0 to 5V) is an analog input to A0. This potentiometer voltage is converted to PWM(pin9) which controls a fan M1. Which means the human controls directly the M1 fan speed.

This directly speed-controlled M1 fan creates air pressure on a sensor Sen1.

On the other hand I want M2 fan to reach to a speed(via pin10 PWM) such that Sen2 should output the same voltage as Sen1. Which means eventually voltage at A1 analog input should be equal to A2. So one can achieve same air pressures at Sen1 and Sen2.

I'm not experienced in PID or other type of controls. I would be glad if someone can help me to relate parameters and code this scenario.


3 Answers 3


Well, (very) simply you could slowly increase/decrease the PWM value driving M2 until the voltage at A1 equals that of A2. If you make the time between adjustments long enough you should not get any (significant) oscillations.

However, more likely you want to get to the correct M2 PWM value quickly and for that you need a Proportional, integral, Differential (PID) control algorithm. Explaining this in detail is outside the scope of a S.E. answer, but in summary:

You have a value that signifies the current state of the system - which will be the A2 reading. You also have a value that states where you want to get to - which will be the A1 reading. You also have a control value - which will be the PWM value of M2

The Proportial part looks at how far away the A2 reading is from the A1 reading. The Integral part looks at how quickly we (the A2 reading) are moving towards (or away from) the desired value (the A1 reading). The Differential part looks at how long we have been away from the desired value.

Each of these (PID) parts (with tuning values that you set) contributes towards determining a new value for PWM at M2 and this iterates around with new values getting the current state closer and closer to the desired value.

There are lots of Arduino PID examples around. I'd suggest you read up on the algorithm itself and have a look at the Arduino.cc samples here.


PID loops are a good way to go for something like this, but, you don't need to implement all three parts. I have done industrial control using just the P and I. Start by implementing just the proportional part.

For this, you calculate the error between the input signal (Va0) and the feedback signal (Va1). This is just a subtraction Err = Va0 - Va1.

You now multiply the Err by a constant and add that to Va0 to adjust the PWM.

A bit like this pseudo code:

  k = 0.5;
  Err = Va0 - Va1;
  pwm = Va0 + (Err * k);

Then set the pwm to the new calculated value.

This gives a correction proportional to the error.

Experiment with k. Start at 1. See how it goes.

Depending on the sensors you may have to apply some gain and/or offset to the Va1 reading in order to make it so that when the motor is at the correct speed, the calculated Va1 reading reads the same as the Va0 input. For example, say the output range of the sensor is 0V to 2.5V, you will need to multiply by 2 before calculating the Err.

The P part of PID can give acceptable results. The next stage would be to implement an I part. Do the P and see whether the I is needed.


I want M2 fan to reach to a speed(via pin10 PWM) such that Sen2 should output the same voltage as Sen1.


  adc_sens1 = readadc(SENS1_PIN); //read adc on sense 1 pin
  adc_sens2 = readadc(SENS2_PIN); //read adc on sense 2 pin
  if (adc_sens2>adc_sens1 + SENS_DELTA) pwm_set(M2_PIN, --dc_ms); //if sens2 value too high, slow down m2's duty cycle -> positive correlatin assumed
  if (adc_sens2<adc_sens1 - SENS_DELTA) pwm_set(M2_PIN, ++dc_ms); //otherwise, increment duty cycle

No error check, no boundary check, ..... but the basic idea is the same.

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.