A PID example question

Hi there,

I'm working with PID controller but I'm confused with those variable on PID in this atthached example...

Do set point, input and output ranges from 0 to 1023?

If I use this program to drive an led, do I need to set the set point to 1023 to get the brightest output?

Thanks

I'll be back in a while, when I find a picture of the answer. If you want help before then, post your code properly.

I'm sorry I just want to know which part I didn't make it clear.... Is the picture of the code too small?

The code is the PID example code in PID library named "PID_Basic". The explanation of this code is only those two sentences.
It reads the analog input at the pin A(0) and gives the output(PWM) at pin 3. I've tried to connect LED and resistor as this newly attached picture and vary the set point. The LED blinks at the same duty cycle even at different set points so I'm confused about what really does the code do?

Is the picture of the code too small?

I can't copy your picture and paste it into the IDE, to reproduce the problem, whatever it is. I can't scroll the picture up and down to see all of the code. In other words, pictures of code just do not cut it.

The pictures disappear when I click reply, too, while code remains displayed.

The explanation of this code is only those two sentences so I'm confused about what really does the code do?

This suggests that you don't know what PID is or why it should be used, which leads me to question why you are even using PID.

/********************************************************

  • PID Basic Example
  • Reading analog input 0 to control analog PWM output 3
    ********************************************************/

#include <PID_v1.h>

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);

void setup()
{
//initialize the variables we're linked to
Input = analogRead(1);
Setpoint = 4;

//turn the PID on
myPID.SetMode(AUTOMATIC);
}

void loop()
{
Input = analogRead(1);
myPID.Compute();
analogWrite(3,Output);
}

here is the code.... I've given you more information by correcting the previous reply (Please see the circuit that i've attached also)

Do set point, input and output ranges from 0 to 1023?

The Input variable does. The Setpoint value, since it defines the PWM value desired, should range from 0 to 255. The Output value will then range from -infininty to infinity. The magnitude determines whether the actual value to use is higher than the setpoint or lower than the setpoint.

For instance, suppose the setpoint was 72 degrees, and the idea is to keep the room at a comfortable temperature. Now, suppose that we measure the actual temperature, and it is 14 degrees. Obviously, we need to add a lot of heat, so output will be a large positive value. Suppose, instead, that the actual temperature is 84 degrees. We need to fire up the A/C, so the output value will be negative, with a much smaller magnitude than in the 14 degree case.

There is nothing in your picture that indicates that anything is connected to analog pin 1. Connecting the resistor and LED to digital pin 3 and analog pin 0 does not make sense.

What is the input in that setup? What are you trying to maintain (what does setpoint mean)?

I'm sorry, the picture was wrong. It is the analog pin 1 that connects to the LED not the analog pin 0.

http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/
This is the site of who develops this PID library. It says error = Setpoint - Input. If the setpoint and the input don't have the same range, they will never be cancel out completely and the error will always be present.

It is the analog pin 1 that connects to the LED not the analog pin 0.

OK. So what is your input? The code example expects that analog pin 1 will provide some input. Obviously, you'll have a difficult time reading anything from an LED.

What is the target (value) for your system? What value are you trying to control?

What are you using the output for?

If you are trying to control the temperature of a room, you need a way to measure the current temperature and to add and remove energy from the room, to raise or lower the temperature.

If you are trying to maintain height above ground in a quadcopter, you need a way to know the current height above ground, and you need a way to climb or drop.

What is it you are trying to do?

This is the site of who develops this PID library. It says error = Setpoint - Input. If the setpoint and the input don't have the same range, they will never be cancel out completely and the error will always be present.

Range is not an issue. In that code, error, Setpoint and Input are all floating point variables.

With PID, it is not important whether the Setpoint and Input cancel exactly. They almost never will and there will almost always be an error term. If the code is properly tuned, the output will drive Input to approach Setpoint as closely as the system can manage, and that is the main purpose.

So what you have is better explained by my schematic below, where as PaulS says, there needs to be some sensor or other providing a voltage between 0 and 5 on a A1. I've just shown that as a pot, and as Paul says it certainly can't be the LED as in your earlier schematic.

JimboZA:
So what you have is better explained by my schematic below, where as PaulS says, there needs to be some sensor or other providing a voltage between 0 and 5 on a A1. I've just shown that as a pot, and as Paul says it certainly can't be the LED as in your earlier schematic.

Didn't the code let us set the desired set point from the program? If you're right, the feedback path of this control loop is a human which observes the brightness of the led and tune the potentiometer. What the PID gain do is determine how fast and how much error the response will be.

PaulS
What am I gonna do from this example is I want to control the PWM duty cycle at the pin3. Because I have no oscilloscope so I will observe the percentage of the duty cycle via the brightness of the led. I will give the setpoint value which is my desired brightness of the LED (in fact, the PWM duty cycle percentage). I didn't change the whole example code but I just want to see how the example works.

At first, I wanted the LED to be fully "ON" which means 100% duty cycle. I was hesitate to write "Setpoint = 100" in the program because from a link from the PID library developer, the setpoint might be the same range as the input (from the equation error=setpoint-input) so I didn't know how to give the value to the setpoint. (As the example says Input = analogRead(1) which implied that the input might range from 0 to 1023 and I might have to give "Setpoint=1023" instead of 100)

I've tried setpoint=1023 first. For the feedback path, I just simply jump the wire from pin3 to the analog pin 1. I want the led to be slowly brighter until it's at the maximum so I increase the derivative gain and test the circuit but what I've got is just a blink. I've try to tune the integral gain because the steady state error might be present (the steady state of output might be at 800 or sth.) but I still have the same outcome (Proportional gain won't help also).

jremington

From your suggestion, I'll try:

  1. Setpoint = 0 to 255
  2. The output just leave it be because the PID function calculates it automatically
  3. Try to vary the PID gain to see the led brightness (or flashing if the frequency is low)

Am I right?

  1. Try to vary the PID gain to see the led brightness (or flashing if the frequency is low)

The PID gain term (Kp) has nothing to do with LED brightness. It has to do with how rapidly the output changes to correct the error term and if set too high, the system response will oscillate about the setpoint. Google "PID tuning" for lots of advice.

Your program will probably need to "clamp" the output to the range 0-255 (if used for LED brightness), as the PID algorithm itself doesn't know the limitations of the external devices. Clamping will affect the overall behavior of the system, but there is little you can do about that. For example:

if (Output > 255) Output = 255;

On the other hand (Big Picture view), if changing the LED brightness does not do anything to the system, there is no point in using the PID algorithm. Go back to PaulS' question: what are you actually trying to accomplish with PID?

You should also be printing the Input, Setpoint, and Output values so YOU can see how the output changes as the input changes, as you manipulate the tuning parameters.

I assume that this is a learning exercise and that there's more to come later, but right now this:

I want to control the PWM duty cycle at the pin3

as others have said, doesn't need the PID at all. Replacing your loop function with this satisfies your requirement as written:

void loop()
{
  Input = analogRead(1);
  analogWrite(3,Input/4);
}

Is there some reason that you don't want the PWM to react so quickly?

In fact, I just want to understand the code clearly. sorry for making confusion.

Because at the top of the example says:

  • PID Basic Example
  • Reading analog input 0 to control analog PWM output 3

At first glance, I didn't really know about the process of the code and its application

After I've looked at the code, I thought (If I am wrong, please confirm that to me) that the analog input was a feedback part which fed the PWM output at pin 3 back to the microcontroller. I've done this test because I want to know how the code works and the only option to observe the duty cycle without an oscilloscope is led brightness.

PaulS:
You should also be printing the Input, Setpoint, and Output values so YOU can see how the output changes as the input changes, as you manipulate the tuning parameters.

Of course, I'll learn how to record the response of this thing and show you later. What I've done is just changing the PID term by by term and see how it affects. I just want to understand how to use the code properly (how to give a value to the setpoint parameter properly) not to tune this led lamp into a critical damp which is the goal of PID.

wildbill:
I assume that this is a learning exercise and that there's more to come later, but right now this:

I want to control the PWM duty cycle at the pin3

as others have said, doesn't need the PID at all. Replacing your loop function with this satisfies your requirement as written:

void loop()

{
  Input = analogRead(1);
  analogWrite(3,Input/4);
}




Is there some reason that you don't want the PWM to react so quickly?

Yes because I want to see the effect of the PID gain (In fact, I've known the theory before but I want to apply the control theory to coding in arduino properly)
I might misunderstand about the code. the analog input might be just reading the potentiometer value instead of reading the actual PWM which is supplied to the LED.

jremington:

This is the site of who develops this PID library. It says error = Setpoint - Input. If the setpoint and the input don't have the same range, they will never be cancel out completely and the error will always be present.

Range is not an issue. In that code, error, Setpoint and Input are all floating point variables.

With PID, it is not important whether the Setpoint and Input cancel exactly. They almost never will and there will almost always be an error term. If the code is properly tuned, the output will drive Input to approach Setpoint as closely as the system can manage, and that is the main purpose.

I have the book named "Modern Control Engineering 5th edition, Ogata". As in the attached picture, it says obviously that the input(feedback) and the setpoint should have the same dimensions and same units.

You may say "Oh! if the error becomes zero, what will drive the system to produce the output in the steady state?". The answer is the integral term because the integral term sums the error up from the very beginning. When the system reaches the steady state point, the error at that time is zero (if we correctly tune it) but the integral term (the sum) is not. The integral term will be constant this time because no more error is added to increase this term as the error is zero. Because the integral term is constant, the output is constant at the steady state. The proportional and the derivative terms depend on error at that time. When the error is zero, these terms are just 0

This is just a theory.... but I suck at coding lol