Go Down

Topic: PID Example 1 (Read 980 times) previous topic - next topic

Bassam

Feb 08, 2011, 02:07 am Last Edit: Feb 08, 2011, 02:10 am by Bassam Reason: 1
Hello,

can I upload this Ex in Arduino duemilanove? because I can't find the "Analog Out" on the board.
can some one let me know what is the input and the output supposed to be in this example?
can I replace the analog INPUT and OUTPUT to degital ? How?
what is the range of the "Set point"?


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

#include <PID_Beta6.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);

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

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

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



Thanx in advance,

jraskell

analogWrite() works with the PWM pins, so pin 3 refers to digital pin 3, not analog pin 3.  The analog inputs are input only (as outputs they are digital, but without PWM capabilities)

And that sample is from the PIDLibrary in the playground here: http://www.arduino.cc/playground/Code/PIDLibrary

Bassam

#2
Feb 08, 2011, 11:44 pm Last Edit: Feb 08, 2011, 11:58 pm by Bassam Reason: 1
thnx for replying and thenx for posting the source, question, what kind of input can I use at ANALOG IN 0 ? just to get this example working?

also, is the output in analogWrite(3,Output), (0<output<255) to control the duty cycle of the PWM?

Msquare

+5V ----- Potentiometer----GND
                |
                \(the middle leg)
                   ------------Analog 0

That should give you an input voltage on the Analog 0 between 0 and 5V. The analogRead returns a value between 0 and 1023. Use a potentiometer with at least 10K, 100K would be fine.

Yes, The PWM control varies between 0 (0% on) and 255 (100% on)


Bassam

see what I"m trying to do is to control the PWM via distance. i'm using the following
http://www.arduino.cc/en/Tutorial/Ping

to read distance and i'm thinking of feeding the distance that the sensor is reading as a PID Input.
i'm unable to figure this out, can i do the following:

input = cm;

?

Msquare


Go Up