Hello. I'm doing this project: https://www.sciencebuddies.org/science- ... l-pancreas. I'm trying to write a pid code to finetune the "pancreas" function using the arduino coding language (C++). The code is as follows:
#include <PID_v1.h>
// Define constants
const double setpoint = 700; // Setpoint value
// Define PID parameters
const double Kp = 1.0; // Proportional gain
const double Ki = 0.1; // Integral gain
const double Kd = 0.05; // Derivative gain
// Define PID objects
double input, output;
PID myPID(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);
// Define pump pin
const int pumpPin = 5;
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize PID
myPID.SetMode(AUTOMATIC);
// Set the pump pin as an output
pinMode(pumpPin, OUTPUT);
}
void loop() {
// Simulate sensor input (replace this with your actual sensor reading)
int sensorValue = analogRead(A0); // Replace A0 with your sensor pin
input = map(sensorValue, 0, 1023, 0, 1023);
// Compute PID output
myPID.Compute();
// Scale the PID output to control the pump (adjust as needed)
int pumpSpeed = map(output, 0, 255, 0, 255);
// Constrain pumpSpeed to prevent exceeding the valid range
pumpSpeed = constrain(pumpSpeed, 200, 255);
// Control the pump using PID output
pinMode(pumpPin, INPUT);
analogWrite(pumpPin, pumpSpeed);
// Print input, setpoint, and output for debugging
Serial.print("Input: ");
Serial.print(input);
Serial.print(" | Setpoint: ");
Serial.print(setpoint);
Serial.print(" | Output: ");
Serial.println(output);
// Simulate a delay (adjust as needed)
delay(1000);
}
My question is that whenever the code is running, the original error (setpoint - input) becomes bigger and bigger as more tap water is pumped into the purified water, getting farther away from the goal, as opposed to getting smaller and smaller and getting closer to the goal. This is a problem because the code becomes irrelevant in this sense. Even when you try to reverse the situation, making the setpoint less than the input, will cause the output and thus the pumpSpeed to be 0. Because of this, my project has been blocked from continuing. How should I fix this?
Thanks :)
pid (proportional integral derivative) code not working(2)
Moderators: kgudger, bfinio, MadelineB, Moderators
-
- Posts: 2
- Joined: Fri Dec 22, 2023 2:28 am
- Occupation: Parent
-
- Expert
- Posts: 761
- Joined: Mon Aug 12, 2013 2:41 pm
- Occupation: Science Buddies Staff
- Project Question: Expert
- Project Due Date: n/a
- Project Status: Not applicable
Re: pid (proportional integral derivative) code not working(2)
Hi - first, it looks like another file or section of code is missing from your post, because I don't see the actual PID equation where you're multiplying the constants by the error, integral of the error, and derivative of the error respectively. Is that code all in PID_v1.h and if so can you include that in your reply?
Second, fixing this could just be a matter of switching the sign on your error calculation, making it (input - setpoint) instead of (setpoint - input). That way, as the input gets smaller, the error will approach zero instead of getting larger.
Second, fixing this could just be a matter of switching the sign on your error calculation, making it (input - setpoint) instead of (setpoint - input). That way, as the input gets smaller, the error will approach zero instead of getting larger.
-
- Posts: 2
- Joined: Fri Dec 22, 2023 2:28 am
- Occupation: Parent
Re: pid (proportional integral derivative) code not working(2)
We got the code fixed! It works now. Thank you!