I bought an Arduino SK to my daughter. She loves technology.
I'm helping her with a school project. It's just an Temperature regulator, with a old micro fan.
She has designed the code with PID, but something must be wrong, cause It doesn't work.
If we use just the BASIC PID sample (Arduino Playground - PIDLibaryBasicExample) and print in SERIAL the values of INPUT and OUTPUT, everything runs fine.
But when we try to insert just a couple of PinMode lines ( to setup L293D and control the fan), the OUTPUT of PID always is ZERO. The code is:
/********************************************************
* 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
[b] pinMode(7,OUTPUT);
pinMode(8,OUTPUT);[/b]
Serial.begin(9600);
Input = analogRead(0);
Setpoint = 100;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Input = analogRead(0);
myPID.Compute();
// analogWrite(11,Output);
Serial.print(Input);
Serial.print("\t");
Serial.println(Output);
}
I don't understand the cause. I'm sure that the solution is very simple, but I don't find it.
I will very grateful to you by your help
Since the input says 153-4 and the setpoint is 100 I am not very surprised that the output is 0. I think the default is that you are running a heater and can only influence the system in the positive direction. The only way to get from 154 to 100 in that case would be to add 0 heat and wait for the system to cool down by itself.
If the fan's supposed to be cooling something, shouldn't the direction parameter be reverse? Otherwise, as John observes, the PID believes it's action is a heating one and sees the input as indicating that it can't help.
I'm a little perplexed as to why you don't get zeroes in your first example for the same reason though.
I don't think the pin mode statement has anything to do with your issue, although it implies that you've changed your wiring - is that what's producing the ~154 readings rather than the ~100s?
One other issue to consider is the sample time. AnalogRead will slow your code down a little, but you may find that some of your calls to compute are being ignored because the default 200ms sample time hasn't elapsed.