Arduino PID Controller - Temperature PID Controller

A PID controller, or a Proportional-Integral-Derivative controller, is a type of feedback control mechanism used in control systems to regulate processes and achieve desired setpoint. It is a widely used control algorithm in industrial plants. It is an algorithm which continuously calculates an error signal based on the difference between a desired setpoint and the measured process variable(temperature, pressure etc), and then adjusts the control output accordingly to minimize the error and bring the process variable to the setpoint. Here it is shown how Arduino can be used as an automatic PID controller to control temperature of an oven.

Here you will learn the followings:

  • Explore Arduino as PID Controller for temperature control using a K-type Thermocouple and AD8495 module.
  • Learn how to implement a PID (Proportional-Integral-Derivative) controller using the popular Arduino platform, and gain insights into the tuning parameters such as Proportional gain (Kp), Integral gain (Ki), and Differential gain (Kd) to achieve optimal temperature control.
  • Discover how to read and scale the set point from a potentiometer input, and convert the temperature input from the thermocouple to Celsius degrees using appropriate calibration factors.
  • Witness a practical demonstration of how the PID controller computes the output and sends it to an oven using PWM (Pulse Width Modulation) to regulate the temperature, while monitoring and displaying the temperature and set point on an LCD display and serial monitor.
  • Additionally, learn how to dynamically adjust the tuning parameters of the PID controller through the serial monitor, and see the immediate effects of changing the gains on the temperature control performance in real-time. Whether you're a beginner or an experienced Arduino enthusiast, this video will provide valuable insights into implementing PID control for temperature applications.

Arduino Temperature PID Controller Circuit Diagram and Working Principle

Here Arduino is programmed as a PID controller to controls the temperature of an oven. The following shows the circuit diagram of oven  temperature controller using Arduino PID controller.

arduino pid controller
A desired temperature, for example 50 degree, is set using a potentiometer which is connected to the analog pin A1. This is called the setpoint. The temperature of the oven is measured using thermocouple. The voltage of thermocouple is very small and so AD8495 thermocouple amplifier which is an instrumentation amplifier is used to amplify the thermocouple signal. The output of the AD8495 IC is connected to analog pin A0 of Arduino. Thus the temperature is read by Arduino and compared with the setpoint temperature. The Arduino perform PID algorithm, that is calculates error, uses the proportional constant(Kp), integral constant(Ki) and derivative constant(Kd) to calculate outputs value to reach the setpoint temperature. The output is sent to the digital switch that controls the oven temperature. The oven temperature is controlled using the PWM signal from Arduino pin 9. The PWM signal controls the CD4066 digital switch which turns on and off the oven. The value of Kp, Ki and Kd can be provided from the serial monitor. The setpoint and current temperature are displayed on the LCD. If the temperature reaches the setpoint range then green LED is turned on otherwise the red LED remains on. This LED blink is used to give visual indication that the desired temperature has reached.

Arduino PID Controller Code

The following is Arduino code for PID (Proportional-Integral-Derivative) controller for temperature control using a K-type thermocouple and an AD8495 amplifier module. The code reads the temperature from the thermocouple, compares it with a setpoint value obtained from a potentiometer, and adjusts the output to an oven using PID control algorithm. It also displays the temperature and setpoint on an LCD display, and controls two LEDs (GreenLED and RedLED) to indicate whether the temperature is within a desired range.

// Arduino PID Controller for temperature control with K-type Thermocouple and AD8495
// By

// library to drive the 16x1 LCD display
#include <LiquidCrystal.h>
// library for PID
#include <PID_v1.h>
// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

float   gain = 0.00488;
float   ref  = 1.25313;

const int GreenLED = 12; 
const int RedLED = 11; 

const int thermocouplePin = A0;     
const int potPin = A1; // Potentiometer input pin
const int ovenPin = 9; // LED output pin

// Tuning parameters
float Kp = 0; // Proportional gain
float Ki = 0.5; // Integral gain
float Kd = 0; // Differential gain
// Record the set point as well as the controller input(y) and output(u)
double Setpoint, y, u;
// Create a controller that is linked to the specified Input, Ouput and Setpoint
PID myPID(&y, &u, &Setpoint, Kp, Ki, Kd, DIRECT);
const int sampleRate = 1; // Time interval of the PID control

void setup(){ 
	//setup serial at 9600 bps
	//LCD setup
	lcd.begin(16, 2);
	//setup LEDs as output
	pinMode(GreenLED, OUTPUT);
    pinMode(RedLED, OUTPUT);

	myPID.SetMode(AUTOMATIC); 		// Turn on the PID control
	myPID.SetSampleTime(sampleRate); // Assign the sample rate of the control
void loop(){

	//-------PID Controller-------
	Setpoint = map(analogRead(potPin), 0, 1023, 0, 255); // read and scale the set point
	// read the temperature input convert to Celsius degree
	y = (double(analogRead(thermocouplePin)) * gain - ref)/0.005; 

	myPID.Compute(); // Calculates the PID output at a specified sample time
	analogWrite(ovenPin, u); // Send output to oven

	//Turn on LEDs according to whether the temperature is within range
	if(y <= 53 && y >= 47){
		digitalWrite(RedLED, LOW);
        digitalWrite(GreenLED, HIGH);
		digitalWrite(RedLED, HIGH);
		digitalWrite(GreenLED, LOW);

	//Display on Serial Monitor
	Serial.print("Temperature: ");
	Serial.print("°C, "); 

	Serial.print(", ");


	// The tuning parameters can be retrieved by the Arduino from the serial monitor: eg: 0,0.5,0 with Ki set to 0.5.
   // Commas are ignored by the Serial.parseFloat() command
			if (Serial.available() > 0){
				for (int i = 0; i < 4; i++){
					case 0:
					Kp = Serial.parseFloat();
					case 1:
					Ki = Serial.parseFloat();
					case 2:
					Kd = Serial.parseFloat();
					case 3:
						for (int j = Serial.available(); j == 0; j--){;
			Serial.print(" Kp,Ki,Kd = "); // Display the new parameters
			myPID.SetTunings(Kp, Ki, Kd); // Set the tuning of the PID loop
	//Display on LCD
	lcd.setCursor(0, 0);
	lcd.write(0xdf); // to display °
    lcd.setCursor(0, 1);
	lcd.write(0xdf); // to display °

	delay(500); // wait a bit

Here's how the code works:

  1. Library Inclusions: The code includes two libraries - LiquidCrystal.h for controlling the LCD display, and PID_v1.h for implementing the PID algorithm.
  2. Variable Declarations: The code declares various variables to store the gain, reference voltage, pin numbers for LEDs, thermocouple input, potentiometer input, and oven output. It also declares variables for the tuning parameters of the PID controller (Kp, Ki, Kd), and variables for storing the setpoint, controller input (y), and controller output (u) for the PID algorithm.
  3. Setup Function: The setup() function is called only once when the Arduino starts. It initializes the serial communication at 9600 bps, sets up the LCD display, configures the LED pins as outputs, sets the PID mode to AUTOMATIC, and assigns the sample rate for the PID control.
  4. Loop Function: The loop() function is called repeatedly after the setup() function. It implements the main logic of the PID controller.
  5. PID Control: The code reads the setpoint value from the potentiometer, scales it to the range of 0-255, and stores it in the Setpoint variable. It reads the thermocouple input, converts it to Celsius using the gain and reference voltage values, and stores it in the y variable. The PID algorithm is then executed using the myPID.Compute() function, which calculates the PID output (u) based on the setpoint, input (y), and tuning parameters (Kp, Ki, Kd) set earlier. The PID output is written to the oven output pin using the analogWrite() function.
  6. LED Control: The code checks if the temperature is within a desired range (between 47°C and 53°C), and turns on the GreenLED if it is, or the RedLED if it is not, using the digitalWrite() function.
  7. Serial Communication: The code displays the temperature, setpoint, and PID output values on the Serial Monitor using the Serial.print() and Serial.println() functions. It also listens for incoming data on the Serial Monitor to update the tuning parameters of the PID controller. If new tuning parameters are received, they are parsed using the Serial.parseFloat() function, and the PID tuning is updated using the myPID.SetTunings() function.
  8. LCD Display: The code updates the LCD display with the current setpoint and temperature values using the lcd.print() function. It also writes the degree symbol (°) to the display using the lcd.write() function.
  9. Delay: The code adds a delay of 500 milliseconds using the delay() function to avoid rapid updates of the PID control loop.

The loop() function repeats these steps continuously to implement the PID control for temperature control with the K-type thermocouple and AD8495 amplifier module, and provides feedback through the LCD display, LEDs, and Serial Monitor.

Video demonstration of Arduino PID Controller

The following video covers the basics of Arduino based PID control, how to tune PID parameters, how to control temperature of oven, read thermocouple temperature and amplify it, and how to interface with a 16x2 LCD display and LEDs for visual feedback.

References & Further Readings:

[1] LM324 Instrumentation Amplifier

[2] LM35 Temperature Sensor with Arduino and LCD

[3] Display Temperature on Web with NodeMCU, LM35 and Node-Red

[4] DHT11 Humidity and Temperature Sensor with Arduino 



Previous Post Next Post