TCLab Function Help

connections.png

Connect/Disconnect

lab = tclab.TCLab() Connect and create new lab object, lab.close() disconnects lab.

LED

lab.LED() Percentage of output light for Hot Light.

Heaters

lab.Q1() and lab.Q2() Percentage of power to heaters.

Temperatures

lab.T1 and lab.T2 Value of current heater tempertures in Celsius.

TCLab Function Example Description
TCLab() tclab.TCLab() Create new lab object and connect
LED lab.LED(45) Turn on the LED to 45%. Valid range is 0-100%
Q1 lab.Q1(63) Turn on heater 1 (Q1) to 63%. Valid range is 0-100%
Q2 lab.Q2(28) Turn on heater 2 (Q2) to 28%. Valid range is 0-100%
T1 print(lab.T1) Read temperature 1 (T1) in °C. valid range is -40 to 150°C (TMP36 sensor)
T2 print(lab.T2) Read temperature 2 (T2) in °C. with +/- 1°C accuracy (TMP36 sensor)
close() lab.close() Close serial USB connection to TCLab - not needed if using with to open

Errors

debug.png

Submit an error to us at support@apmonitor.com so we can fix the problem and add it to this list. There is also a list of troubleshooting items with frequently asked questions. If you get the error about already having an open connection to the TCLab, try restarting the kernel. Go to the top of the page to the Kernel tab, click it, then click restart.

How to Run a Cell

Use the symbol to run the needed cell, left of the program. If you don't see a symbol, try selecting the cell and holding Ctrl, then pressing Enter. Another option is clicking the "Run" button at the top of the page when the cell is selected.

Install Temperature Control Lab

temperature.png

This code trys to import tclab. If it if it fails to find the package, it installs the program with pip. The pip installation is required once to let you use the Temperature Control Lab for all of the exercises. It does not need to be installed again, even if the IPython session is restarted.

In [ ]:
# install tclab
try:
    import tclab
except:
    # Needed to communicate through usb port
    !pip install --user pyserial
    # The --user is put in for accounts without admin privileges
    !pip install --user tclab 
    # restart kernel if this doesn't import
    import tclab

Update Temperature Control Lab

Updates package to latest version.

In [ ]:
!pip install tclab --upgrade --user

Connection Check

usb.png

The following code connects, reads temperatures 1 and 2, sets heaters 1 and 2, turns on the LED to 45%, and then disconnects from the TCLab. If an error is shown, try unplugging your lab and/or restarting Jupyter's kernel from the Jupyter notebook menu.

In [ ]:
import tclab
lab = tclab.TCLab()
print(lab.T1,lab.T2)
lab.Q1(50); lab.Q2(40)
lab.LED(45)
lab.close()

expert.png

Another way to connect to the TCLab is the with statement. The advantage of the with statement is that the connection automatically closes if there is a fatal error (bug) in the code and the program never reaches the lab.close() statement. This prevents a kernel restart to reset the USB connection to the TCLab Arduino.

In [ ]:
import tclab
import time
with tclab.TCLab() as lab:
    print(lab.T1,lab.T2)
    lab.Q1(50); lab.Q2(40)
    lab.LED(45)