#include "PID_v1.h"
//#include "PID_AutoTune_v0.h"
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
double kp=2,ki=5,kd=1;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,kp,ki,kd, DIRECT);
//PID_ATune aTune(&Input, &Output);
int ledPin = 9; // LED connected to digital pin 9
int analogPin1 = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
int analogPin2 = 4; // potentiometer wiper (middle terminal) connected to analog pin 4
int var1 = 0; // variable to store the value read
int var2 = 0; // variable to store the value read
int var3 = 0; // variable to store the value read
void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
Serial.begin(9600); // setup serial
// var1 = analogRead(analogPin1); // read the input pin 3
// var2 = analogRead(analogPin2); // read the input pin 4
// Input = var1 - var2;
// Input = map(Input, 0, 1023, 0, 255);
Input = 50;
Setpoint = 150;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
// var1 = analogRead(analogPin1); // read the input pin
// var2 = analogRead(analogPin2); // read the input pin
// Input = var1 - var2;
// Input = map(Input, 0, 1023, 0, 255);
Input = 50;
myPID.Compute();
Serial.println(var1); // debug value
Serial.println(var2); // debug value
Serial.println(Input); // debug value
analogWrite(ledPin,Output);// analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
I only read 4,5V at pin 9 with a multimeter so my PID isn't working. What is wrong? I used some code the PID example code from the Arduino site..
What is the voltage on the +5V pin? I expect you will find that to be very close to 4.5V.
Your Setpoint is well above your Input so I would expect the Output to go to 255 (the default maximum) and stay there. That should turn the output pin full on. What did you expect it to do?
What value do you get for Output when the voltage measures 4.5V?
Note the output signal is a 0-5V squarewave, with higher dutycycle equating to a higher voltage being read by a meter.
CrossRoads:
Note the output signal is a 0-5V squarewave,
Shouldn't that be 0-VCC? I suspect his VCC is only 4.5V and that is why he is reading 4.5V on the output pin at 100% duty cycle. That's why I asked what the voltage on the "+5V" pin was.
As i wrote in dutch forum this will never work as there is no feedback at all,
try to make it simpler, so for simulation use one potmeter as input and the other as setpoint and one led as output.
all inputs and outputs are double, so you will need to convert inputs and later outputs.
by default the min and max is 0 and 255
now use only Kp for starters.
put the I and D function on 0
put the Kp on 1 for start so you can calculate it easy.
now you can start to play with the system.
for a schoolproject you can use a FET, with a heater or old lightbulb, and a NTC as sensor.
you will need to make a loopcontrol as any PID works only when it is in a loop.
also make the outputs on a print every second or so same as inputs.
btw read the fucking manual on the playground.
explanation is good, and the basic PID works great.
shooter:
As i wrote in dutch forum this will never work as there is no feedback at all,
try to make it simpler, so for simulation use one potmeter as input and the other as setpoint and one led as output.
all inputs and outputs are double, so you will need to convert inputs and later outputs.
by default the min and max is 0 and 255
now use only Kp for starters.
put the I and D function on 0
put the Kp on 1 for start so you can calculate it easy.
now you can start to play with the system.
for a schoolproject you can use a FET, with a heater or old lightbulb, and a NTC as sensor.
you will need to make a loopcontrol as any PID works only when it is in a loop.
also make the outputs on a print every second or so same as inputs.
btw read the fucking manual on the playground.
explanation is good, and the basic PID works great.
I use this current source:
I connect a 20 ohm resistor to the drain of the mosfet and i measure the voltage of it with the Arduino (A3 & A4).
#include "PID_v1.h"
//#include "PID_AutoTune_v0.h"
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
double kp=2,ki=0,kd=0;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,kp,ki,kd, DIRECT);
//PID_ATune aTune(&Input, &Output);
int ledPin = 9; // LED connected to digital pin 9
int analogPin1 = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
int analogPin2 = 4; // potentiometer wiper (middle terminal) connected to analog pin 4
int var1 = 0; // variable to store the value read
int var2 = 0; // variable to store the value read
int var3 = 0; // variable to store the value read
void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
Serial.begin(9600); // setup serial
var1 = analogRead(analogPin1); // read the input pin 3
var2 = analogRead(analogPin2); // read the input pin 4
Input = var1 - var2;
Input = map(Input, 0, 1023, 0, 255);
Setpoint = 100;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
var1 = analogRead(analogPin1); // read the input pin
var2 = analogRead(analogPin2); // read the input pin
Input = var1 - var2;
Input = map(Input, 0, 1023, 0, 255);
myPID.Compute();
Serial.println(var1); // debug value
Serial.println(var2); // debug value
Serial.println(Input); // debug value
analogWrite(ledPin,Output);// analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
I don't know why it's still not working... Help me plz.
You will have to be a little more specific than "still not working". As near as I can see the LED should go ON if Input is much below 100 and OFF if Input is much above 100. What does the LED do for various inputs?
johnwasser:
You will have to be a little more specific than "still not working". As near as I can see the LED should go ON if Input is much below 100 and OFF if Input is much above 100. What does the LED do for various inputs?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Include the two pots if you are using them as well as power supply.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Include the two pots if you are using them as well as power supply.
Looks like you have Pin 9 connected to Ground and have 7.5V (15V/2) connected to Pin A3. Neither is a good thing. It also is not clear what the circuit is supposed to be doing except heating up some resistors.
Yes. What else can I conclude when you have these lines of code:
int ledPin = 9; // LED connected to digital pin 9
pinMode(ledPin, OUTPUT); // sets the pin as output
myPID.Compute();
analogWrite(ledPin,Output); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
johnwasser:
Looks like you have Pin 9 connected to Ground and have 7.5V (15V/2) connected to Pin A3. Neither is a good thing. It also is not clear what the circuit is supposed to be doing except heating up some resistors.
No no sorry I use 2 10k and 30k resistors to decrease the voltage to max. 5 V anough to avoid clipping.
johnwasser:
Yes. What else can I conclude when you have these lines of code:
int ledPin = 9; // LED connected to digital pin 9
pinMode(ledPin, OUTPUT); // sets the pin as output
myPID.Compute();
analogWrite(ledPin,Output); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
It doesn't matter what name i give it, ledpin = pin 9 so the Arduino uses pin 9 as a source.
Philipp91:
No no sorry I use 2 10k and 30k resistors to decrease the voltage to max. 5 V anough to avoid clipping.
It doesn't matter what name i give it, ledpin = pin 9 so the Arduino uses pin 9 as a source.
It only matters if you are asking someone else to guess why your sketch isn't doing what you thought you were telling it to do.