#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <PID_v1.h>
// Define the pins for the temperature sensor
const int tempSensorPin = A0;
// Define the OLED display object
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// Define PID parameters
double Setpoint = 25.0; // Setpoint temperature in degrees Celsius
double Input, Output;
double Kp = 2.0; // Proportional term
double Ki = 5.0; // Integral term
double Kd = 1.0; // Derivative term
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup() {
// Initialize the OLED display
display.begin(SSD1306_I2C_ADDRESS, &Wire);
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
// Initialize the PID controller
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(1000); // 1 second sample time
myPID.SetOutputLimits(0, 255); // Adjust as needed for your setup
}
void loop() {
// Read the temperature from the sensor
int rawValue = analogRead(tempSensorPin);
double voltage = (rawValue / 1023.0) * 5.0; // Assuming a 5V reference voltage
Input = (voltage - 0.5) * 100.0; // Convert to degrees Celsius
// Compute PID control output
myPID.Compute();
// Display temperature and control information on OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Temp: ");
display.print(Input, 2);
display.println(" C");
display.print("Setpoint: ");
display.print(Setpoint, 2);
display.println(" C");
display.setCursor(0, 40);
display.print("Output: ");
display.print(Output, 2);
display.display();
delay(1000); // Update the display every 1 second
}