Main

Arduino Setup and Installation

MATLAB or Python must be installed to use the lab. MATLAB requires the Arduino support package. Python requires additional packages such as the tclab (see documentation) package that can be installed with pip.

 pip install tclab

The tclab package can also be installed from a Python script with the following code.

module='tclab'
try:
    from pip import main as pipmain
except:
    from pip._internal import main as pipmain
pipmain(['install',module])
# to upgrade: pipmain(['install','--upgrade',module])

The Arduino Uno can be switched between MATLAB and Python compatibility by loading the appropriate firmware.

The temperature control lab is pre-loaded with Python or MATLAB firmware. A first test is to send a command to the Arduino Uno to turn on the LED and adjust the LED level. The file tclab.py (in Python folder or pip installed) or tclab.m (in MATLAB folder) should be present in the search path or the run directory. The following scripts turn on the LED for 1 second.

Python

import tclab
import time
# Connect to Arduino
a = tclab.TCLab()
print('LED On')
a.LED(100)
# Pause for 1 second
time.sleep(1.0)
print('LED Off')
a.LED(0)
a.close()

MATLAB

clear all
% include tclab.m for initialization
tclab;
disp('LED on')
led(1)
% Pause for 1 second
pause(1.0);
disp('LED off')
led(0)

Once the LED test is successful, the heaters and temperature sensors can be tested. The top barrel jack powers the transistor heaters and should be plugged into a wall socket with the 5V power supply or else plugged into a second USB port on the computer. The following are minimal scripts to test the heaters and temperature sensors.

Python

import tclab
import numpy as np
import time
# Connect to Arduino
a = tclab.TCLab()
# Temperatures
print('Temperatures')
print('Temperature 1: ' + str(a.T1) + ' degC')
print('Temperature 2: ' + str(a.T2) + ' degC')
# Turn LED on
print('LED On')
a.LED(100)
# Turn on Heaters (0-100%)
print('Turn On Heaters (Q1=90%, Q2=80%)')
a.Q1(90.0)
a.Q2(80.0)
# Sleep (sec)
time.sleep(30.0)
# Turn Off Heaters
print('Turn Off Heaters')
a.Q1(0.0)
a.Q2(0.0)
# Temperatures
print('Temperatures')
print('Temperature 1: ' + str(a.T1) + ' degC')
print('Temperature 2: ' + str(a.T2) + ' degC')
a.close()

MATLAB

clear all
% include tclab.m for initialization
tclab;
% Test device
disp(['Temperature 1: ' num2str(T1C()) ' degC'])
disp(['Temperature 2: ' num2str(T2C()) ' degC'])
disp('Turn on Heater 1 to 30%, Heater 2 to 60%')
h1(30);  h2(60);
disp('Turn on LED for 30 seconds')
led(1);   % ON
pause(30.0);
led(0); % OFF
disp('Turn off Heaters')
h1(0);  h2(0);
disp(['Temperature 1: ' num2str(T1C()) ' degC'])
disp(['Temperature 2: ' num2str(T2C()) ' degC'])
disp('Temperature Test Complete')

Frequently Asked Questions


  • Q: Device isn't showing up as a device on my computer. What can I do to connect?
  • A: First, verify that the cables are attached correctly. There are two USB cables. One USB cable is for the top barrel jack to give power to the heaters. The other USB cable connects directly to a computer and gives power to the Arduino.
Only the blue cable is required for the purpose of testing the communication between the Arduino and computer. The heater power cable can be attached later.
The Arduino should now appear in the Device Manager (for Windows). Take note of the COM serial port assigned. In the example below, the COM port is COM3.
For Linux and MacOS, you can see the device with a terminal and the following command:
 ls /dev/*.
For MacOS, you will need to install drivers. After the driver is installed, search for /dev/tty.usbserial* or /dev/cn.* (on MacOS) and /dev/ttyUSB* or /dev/ttyACM* (on Linux).
For Windows 7, you will need to install driver from the CH340G / CH34G / CH34X chipset manufacturer. Windows 10 has the correct drivers and no driver installation is required.

  • Q: How do I fix an error that the package serial could not be found?
  • A: The package pyserial is missing. The tclab.py package attempts to install it automatically with pip.
try:
    import serial
except:
    import pip
    pip.main(['install','pyserial'])
    import serial

Pyserial can also be installed from the command line with conda. There is more information on installing Python and managing packages here. You may need to run pip from the command line with administrative privileges.

 pip install pyserial

  • Q: How can I resolve an error that access is denied when attempting to connect to the Arduino?
  • A: Reset the Arduino by either unplugging the USB connection and reconnecting or else press the red reset button. This should reset the Arduino device and allow it to connect again. This happens when there was an abnormal termination of the program and the serial connection is not closed.

  • Q: How can I connect to the lab with Python?
  • A: The tclab.py package automatically identifies the serial port (such as COM3) and attempts a connection. The temperature control labs are automatically loaded with tclab.ino (in the tclab folder) for Python. The Python tclab.ino program is recompiled and downloaded to the Arduino if the firmware is changed. The firmware is compiled and downloaded to the Arduino device with the Arduino software. The temperature control lab is pre-loaded with either Python or MATLAB firmware. See answer below if port is not found. No firmware changes should be required.

  • Q: How do I fix an error that the port is not found for the serial connection?
  • A: The tclab software automatically tries to find the USB serial port for connection. In some cases, the software is unable to find the correct port and tclab.py may need to be updated to select the correct port.
  • For Windows:
  • For MacOS:
  • For Linux
    • Open terminal and type: ls /dev/tty*
    • Search for /dev/ttyUSB* or /dev/ttyACM*

  • Q: How can I connect to the lab with MATLAB?
  • A: The Arduino support package can be added to MATLAB. Once installed, the command a = arduino('COM3','Uno') loads the MATLAB Arduino drivers when MATLAB connects for the first time. Afterwards, only a = arduino is needed to establish a connection. This is built into the tclab.m file for MATLAB and Simulink.

  • Q: The temperature is reading but the heaters don't seem to have an effect. How can I ensure that power is being supplied to the heaters?
  • A: A USB to barrel jack cord provide power to the transistor heaters. Ensure that the barrel jack (round) connection is inserted into the top slot labeled Power plug1 and not into the barrel jack slot on the Arduino board. The USB power connection can also be plugged into a second computer USB port to provide power to the transistors although the temperature sensors may experience more random variation due to the pulse width modulation of the heaters. If the heaters operate successfully with the computer USB connection but not with the power supply then the power supply may have an internal trigger to shutoff if there is too much current draw, especially when both heaters are on at 100%.

  • Q: How can I display real-time plots in Spyder (Python)?
  • A: In the Spyder IDE menu bar select Tools, Preferences, IPython Console, Graphics, Backend=Automatic. This will allow real-time plots to display as a new window instead of inline in the IPython console.

  • Q: How can I use the other pins on the Arduino if I'd like to add an additional sensor or actuator?
  • A: The firmware that comes pre-loaded (tclab.ino) is specific to this lab with 2 heaters, 2 temperature sensors, and 1 LED. A different firmware called Standard Firmata can be loaded onto the Arduino to allow the pyFirmata (Python) package to read from or write to any of the pins. This may be useful if you are adding additional sensors or want to modify the pin connections.

import time

try:
    from pyfirmata import Arduino, util
except:
    import pip
    pip.main(['install','pyfirmata'])
    from pyfirmata import Arduino, util

# for MacOS or Linux
# install CH340G driver from https://goo.gl/pWMcU3
#board = Arduino('/dev/cu.wchusbserial1420')

# for Windows
board = Arduino('COM3')

# Start iterator
iterator = util.Iterator(board)
iterator.start()

# TMP36 Temperature Sensor Calibration
def TMP36(Tv):
    return (Tv*5000.0-500.0)/10.0

# Read voltage (0-5 V) as (0-1)
Tv1 = board.get_pin('a:0:i')
Tv2 = board.get_pin('a:2:i')  

# Setup Temperature functions
def T1():
    return TMP36(Tv1.read())

def T2():
    return TMP36(Tv2.read())

# Setup Heater functions with PWM
H1 = board.get_pin('d:3:p')
H2 = board.get_pin('d:5:p')

time.sleep(1.0)

# Print Temperature 1
print('Temperature 1: ' + str(T1()))
print('Temperature 2: ' + str(T2()))

# Turn on heaters
print('Heater 1: 90% and Heater 2: 20%')
H1.write(0.9)
H2.write(0.2)

# Wait
print('Sleep for 25 seconds')
time.sleep(25.0)

# Print Temperatures 1 and 2
print('Temperature 1: ' + str(T1()))
print('Temperature 2: ' + str(T2()))

# Turn off heaters
H1.write(0.0)
H2.write(0.0)

board.exit()

Return to Temperature Control Lab Overview