Skip to content

Instantly share code, notes, and snippets.

@nobodyguy
Last active September 27, 2023 18:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobodyguy/ac286f8ccba23211c27a604f6a0f8af9 to your computer and use it in GitHub Desktop.
Save nobodyguy/ac286f8ccba23211c27a604f6a0f8af9 to your computer and use it in GitHub Desktop.
Arduino PID controller with relay and DS18B20
#include <PID_v1.h>
// Libraries for the DS18B20 sensor
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 on PIN 6 on the Arduino
#define ONE_WIRE_BUS 6
//Solid state relay on PIN 5 on the Arduino
#define RELAY_PIN 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=5, Ki=3, Kd=3;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int WindowSize = 10000;
unsigned long windowStartTime;
void setup()
{
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.begin(115200);
Serial.println("Starting");
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 68;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
sensors.requestTemperatures();
Input = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(Input);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if (millis() - windowStartTime > WindowSize)
{
//time to shift the Relay Window
windowStartTime += WindowSize;
}
if (Output < millis() - windowStartTime)
digitalWrite(RELAY_PIN, HIGH);
else
digitalWrite(RELAY_PIN, LOW);
}
@tamasdravai13
Copy link

Hy. where can i find PID_v1. h file?

@nobodyguy
Copy link
Author

@tamasdravai13 Install Arduino PID Library via library manager.
Its sources are here: https://github.com/br3ttb/Arduino-PID-Library

@tamasdravai13
Copy link

tamasdravai13 commented Aug 10, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment