Temperature

We're going to use CircuitPython, Mu and the temperature sensor built into the Circuit Playground Express to plot temperature change. We'll run this code on our Circuit Playground Express and use Mu to plot the sensor data that CircuitPython prints out.

Save the following as code.py on your Circuit Playground Express board, using the Mu editor:

import time

import adafruit_thermistor
import board

thermistor = adafruit_thermistor.Thermistor(
    board.TEMPERATURE, 10000, 10000, 25, 3950)

while True:
    print((thermistor.temperature,))
    # print(((thermistor.temperature * 9 / 5 + 32),))  # Fahrenheit
    time.sleep(0.25)

Our code is quite simple. We import adafruit_thermistor, board and time. Then we setup our temperature sensor. Inside our while loop, we print the temperature in Celsius.

If you'd like to see Fahrenheit instead, place a (# + space) before the line print((thermistor.temperature,))  and remove the before the line print(((thermistor.temperature * 9 / 5 + 32),)). The temperature is in Celsius by default, so we include a little math (the * 9 / 5 + 32) to convert it to Fahrenheit.

Note that the Mu plotter looks for tuple values to print. Tuples in Python come in parentheses () with comma separators. If you have two values, a tuple would look like (1.0, 3.14). Since we have only one value, we need to have it print out like (1.0,) note the parentheses around the number, and the comma after the number. Thus the extra parentheses and comma in print((thermistor.temperature,)).

Once you have everything loaded and running, you can place your finger over the temperature sensor to see the plotter immediately respond! Try breathing on your CPX temperature sensor to increase the temperature and watch the plotter go up! Try setting it on a cool surface to watch the plotter go down!

This is a great way to tell the temperature and plot temperature changes in Celsius (or Fahrenheit).

This guide was first published on Apr 09, 2018. It was last updated on Nov 19, 2018. This page (Temperature) was last updated on May 18, 2018.