On the internet I found this "Arduino PID Example Lab" as an introduction to PID-contole. I set up the hardware and it is working like it is described in the document. enter image description here Since it is possible to connect Simulink with the Arduino as it is shown here, it would be nice to simulate the PID-control in Simulink. The needed Support Package for the Arduino is published here. After simulating the contole it could be connected to the Arduino to change the parameter while running. I managed to interface the arduino (read analog / digital values) and sent digital (PWM) signals. The main problem is about setting up the PID:

\$u(t)=MV(t)=K_pe(t)+K_i\int_o^t \!e(\tau)d\tau*K_d\frac{d}{dt}e(t)\$

src: Arduino PID Example Lab (1.1)

And to make it compatible with the analog input and the PWM output to the board. So far this is what i got: simulink model

Status-update: status


1 Answer 1


The philosophy behind a PID controller (PID stands for Proportional Integral Derivative) is that you use the difference between the output of your system and and the desired output to generate a corrective input.

e(t) would be equal to the difference between the setpoint and the measured signal, that is e(t) = setpoint - measured_signal(t). Then you just apply the formula, inside your digital PID block. The derivative is optional, if you have a way of obtaining it, use it in your PID. If not, it's not a big deal. PI implementation You should get something similar to the image above, you will need the "Gain" block, the "Integrator" block and the sum. The error is the input, the output is the control input for the Arduino.

Kp and Ki are most easily usually obtained empirically, through manual tuning. Just begin with a small Kp and increase it until the response seems acceptable. Ki is used to kill any offset you have on the output, but slows down the response of the system. Again, you tune it manually, starting with a small Ki. If you want, you can try the Ziegler Nichols method (https://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method)

  • \$\begingroup\$ Thank you for the fast answer. Sorry but I dont get how to apply the formula to the PID block. Can you show it in detail? \$\endgroup\$
    – kimliv
    Commented Jan 9, 2016 at 19:16
  • \$\begingroup\$ But what is that you don't know how to do ? I've given you the simulink implementation of the equation you wrote in your post. Or is the problem that you don't know what a PID is ? \$\endgroup\$ Commented Jan 9, 2016 at 19:43
  • \$\begingroup\$ Sorry for not giving enough details. So far i got it halfway working but the behavior is by far not like the one from the code. The system is slow and swinging when I cover the lightsensor from the surrounding light (so the LED has to birthteh up to reach the set point). The sample-time is set on 0.01 and I put an lower limit of 0 and an upper limit of 255 to the cotroller elements. (an updates scrrenshot is in the question) \$\endgroup\$
    – kimliv
    Commented Jan 9, 2016 at 21:36
  • \$\begingroup\$ I THINK you might need to increase Kp, but I'm just guessing from the look of your output. PID are simple and efficient controllers but they can be a pain to calibrate. Why don't you start with a simpler case, by trying to regulate to a fixed input (not a sine) ? Once you get it working, you can go on to a sinewave again. \$\endgroup\$ Commented Jan 9, 2016 at 21:41
  • \$\begingroup\$ The set-point is a fixed value from a potentiometer connected to analog input pin-1 with a value of around 175. \$\endgroup\$
    – kimliv
    Commented Jan 9, 2016 at 22:03

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.