Temperature Control Lab Python Introduction

The TCLab (Temperature Control Lab) has an LED, two heaters, and two temperature sensors that are controlled with an Arduino. The heater power output is adjusted to maintain a desired temperature. Thermal energy from the heater is transferred by conduction, convection, and radiation to the temperature sensor. Heat is also transferred away from the device to the surroundings. This lab is a resource to learn data analysis, visualization, regression, and model analysis in Python with real data. Temperature control is an important problem in many applications such as:

  • Maintain temperature of a home during the winter
  • Pre-heat an oven for baking
  • Regulate the temperature in a chemical reactor
  • Reduce temperature variations during semiconductor production
  • Adjust the infrared heater for a newborn baby in an incubator to maintain body temperature
  • Regulate natural gas to a water heater to provide consistent hot water
  • Adjust flow through a heat exchanger to maintain outlet temperature
  • Others?

The heaters and LED can be adjusted between 0-100% output. The heater effect is shown in the plot below.

Temperature Control Lab

This Python lab covers the following topics:

  • Part 1: Loops, Files, and Plotting
  • Part 2: Linear, Quadratic, and Nonlinear Regression

Install TCLab and Load Packages

Retrieve tclab.py from Github if pip install is not successful such as for computers where the user does not have administrative privileges to install packages. Include tclab.py in the same directory as the Jupyter notebook.

In [64]:
# download tclab.py from 
try:
    import tclab
except:
    !pip install tclab
    import tclab

# import additional packages 
import time
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
from scipy.optimize import curve_fit

Connect Test TCLab to Computer with USB Cable

Connect the temperature control lab with the USB cable to the computer (MacOS, Windows, or Linux).

Temperature Control Lab Setup

Test TCLab LED

Run the following commands to make sure the board is communicating with the computer. The red LED light should turn on for 10 seconds. There is an installation guide if the connection is unsuccessful.

In [25]:
lab = tclab.TCLab()  # Connect to Arduino
lab.LED(100)         # Turn on LED (0-100%)
time.sleep(10.0)     # Pause for 10 seconds
lab.LED(0)           # Turn off LED
lab.close()          # Close connection
TCLab version 0.4.6
Arduino Leonardo connected on port COM3 at 115200 baud.
TCLab Firmware Version 1.01.
TCLab disconnected successfully.

Test TCLab Temperature Measurements

Run the following commands to read temperatures from sensors T1 and T2.

In [26]:
lab = tclab.TCLab()  # Connect to Arduino
print('T1: ' + str(lab.T1) + ' degC') # Print Temperature 1
print('T2: ' + str(lab.T2) + ' degC') # Print Temperature 2
lab.close()          # Close connection
TCLab version 0.4.6
Arduino Leonardo connected on port COM3 at 115200 baud.
TCLab Firmware Version 1.01.
T1: 28.18 degC
T2: 27.34 degC
TCLab disconnected successfully.

Test TCLab Heaters

Temperature Control Lab Heaters

Plug in the USB power cable to the power supply and the barrel jack connector to the top plug (not the one next to the USB connector). Run the following script to test the heaters to see if the temperature increases. Many of the TCLab kits have thermochromic dots on the back that turn pink once the temperature rises above 37 degC and turn back to black when the temperature drops below 37 degC.

In [17]:
# closes tclab connection when done
with tclab.TCLab() as lab:
    print('Turn on Heaters for 50 seconds')
    lab.Q1(100)          # Turn on Heater 1 to 100%
    lab.Q2(50)           # Turn on Heater 2 to 50%

    for i in range(10):
        print('T1: ' + str(lab.T1) + ' degC, T2: ' + str(lab.T2) + ' degC')
        time.sleep(5) # Pause for 5 seconds
TCLab version 0.4.6
Arduino Leonardo connected on port COM3 at 115200 baud.
TCLab Firmware Version 1.01.
Turn on Heaters for 50 seconds
T1: 29.86 degC, T2: 29.08 degC
T1: 29.83 degC, T2: 29.02 degC
T1: 30.24 degC, T2: 28.89 degC
T1: 31.21 degC, T2: 29.37 degC
T1: 32.5 degC, T2: 29.79 degC
T1: 34.05 degC, T2: 30.15 degC
T1: 35.08 degC, T2: 30.53 degC
T1: 36.59 degC, T2: 30.73 degC
T1: 38.14 degC, T2: 31.47 degC
T1: 39.59 degC, T2: 31.79 degC
TCLab disconnected successfully.

Part 1: Loops, Files, and Plotting

A: Loop to Control LED

Dim the LED from 100% to 0% over a period of 10 seconds. Decrease the LED intensity by 10% every second. Print the LED brightness every second.

In [ ]:
 

B: Record Temperatures

Turn heater 1 to 100% and heater 2 to 40%. Record the temperatures (lab.T1 and lab.T2) every 0.5 seconds for 30 seconds. Store the values for time, temperature 1, and temperature 2 in a Numpy array. Use time.time() to get the current time in seconds.

In [ ]:
 

C: Save Text File

Save the temperatures to a text file "HeaterTest.txt" with three columns with the following headers:

  • Time (sec)
  • T1 (degC)
  • T2 (degC)

The text file should be delimited with commas. Adjust the times so that they are relative to the start that is set to time=0. Load the text file with Pandas and print the first 5 rows to verify that the values are written correctly.

data = pd.read_csv('HeaterTest.txt')
print(data.head())
In [ ]:
 

D: Create Plot

Create a plot of the temperature data (y-axis) versus the time (x-axis). Label the plot (x-label, y-label, legend), change the temperature 1 trend to be red circles, and change the temperature 2 trend to be blue circles.

In [ ]:
 

Part 2: Regression

A: Linear Regression

Create a linear regression for Temperature 2. Report the sum of absolute difference between the measured values and the predicted values. Add the regression line as a black line to a plot with the measured temperature 2 as blue circles. Add appropriate labels to the plot.

In [ ]:
 

B: Quadratic Regression

Create a linear and quadratic regression for Temperature 1. Report the sum of absolute difference between the measured values and the predicted values. Add a black line for the linear regression and a blue dashed regression curve for the quadratic regression. Plot the measured temperature 1 as red circles. Add appropriate labels to the plot.

In [ ]:
 

C: Nonlinear Regression

Fit the $T_1$ data with a nonlinear regression of the form:

$T_1 = a + b \exp{(c \, t)}$

Report the sum of absolute difference between the measured values and the predicted values. Add a black line for the nonlinear regression. Plot the measured temperature 1 as red circles. Add appropriate labels to the plot.

In [ ]: