The Adafruit Circuit Playground Express comes packed with sensors, and for this tutorial, we are going to learn to use the onboard thermometer (technically a thermistor)! We will take temperature readings with the thermometer and print out the readings to a serial monitor, and illuminate the Neopixel ring, all using CircuitPython!
The Code
# Visual Thermometer for Adafruit Circuit Playground Express import adafruit_thermistor # import libraries required import board import time import neopixel thermistor = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000, 10000, 22, 3950) pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.01) pixels.fill((0, 0, 0)) pixels.show() n_pixels = 10 # Number of pixels you are using mintemp = 0 # For adjustment of graph low maxtemp = 40 # For adjustment of graph high def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition b - g - r - back to b. if (pos < 0) or (pos > 255): return (0, 0, 0) if (pos < 85): return (int(255 - pos*3), 0, int(pos*3)) # blue elif (pos < 170): pos -= 85 return (0, int(pos*3), int(255 - pos*3)) # green else: pos -= 170 return (int(pos * 3), int(255 - (pos*3)), 0) # red def remapRange(value, leftMin, leftMax, rightMin, rightMax): # this remaps a value from original (left) range to new (right) range # Figure out how 'wide' each range is leftSpan = leftMax - leftMin rightSpan = rightMax - rightMin # Convert the left range into a 0-1 range (int) valueScaled = int(value - leftMin) / int(leftSpan) # Convert the 0-1 range into a value in the right range. return int(rightMin + (valueScaled * rightSpan)) while True: print("Temperature is: %f C" % (thermistor.temperature)) # Store thermistor reading as a variable temp = thermistor.temperature # Calculate bar height based on adjustable min/max temperature: height = n_pixels * (temp - mintemp) / (maxtemp - mintemp) # Color pixels based on rainbow gradient for i in range(0, len(pixels)): if (i >= height): pixels[i] = [0, 0, 0] elif (temp >= maxtemp): pixels[i] = [255, 0, 0] else: pixels[i] = wheel(remapRange(i, 0, (n_pixels), 85, 255)) time.sleep(0.5) # reduces flickering
Let's break down the code and explain what’s happening. In this first section, we import the libraries needed for our program. We need the adafruit_thermistor library to use a thermistor, and the board library to utilize the thermistor that comes installed on the Circuit Playground Express. If you need help installing libraries on your Circuit Playground Express or setting up CircuitPython, check out our Introduction to CircuitPython Tutorial.
import adafruit_thermistor # import libraries required import board import time import neopixel
To use a thermistor to read temperature we need to first set it up. In this section of code, we set up the variables that convert the rough analog signal that a thermistor outputs to a usable temperature in Celsius. Let's break down the Thermistor initializer:
- Analog input- The first parameter is the name of the input connected to the thermistor. Since the Circuit Playground Express has a thermistor built in, we use the board.TEMPERATURE as the identifier.
- Series resistance- The second parameter is the value of the series resistor connected to the thermistor. Keep this at 10000 ohms.
- Nominal resistance- The third parameter is the value of the resistance of the thermistor at nominal temperature. The Circuit Playground Express thermistor is 10,000 ohms.
- Nominal Temperature- The fourth Parameter is the temperature of the thermistor at nominal resistance. This is normally 25, changing this number will affect the temperature the thermistor reads.
- Beta coefficient- The fifth parameter is the thermistors beta coefficient. Keep this at 3950.
If you are using a separate thermistor, you can find these parameters in the datasheet.
We then initialize the NeoPixels and create some variables to set the desired limits of our thermometer.
thermistor = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000, 10000, 22, 3950) pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.01) pixels.fill((0, 0, 0)) pixels.show() n_pixels = 10 # Number of pixels you are using mintemp = 0 # For adjustment of graph low maxtemp = 40 # For adjustment of graph high
In the next block of code, we first have an operation to create a color wheel on the Neopixels. In this example, it goes from blue to red so it looks like cold to hot!
We then have a remapRange() operation. This converts the range of temperatures that we receive, within the limits we have set as 0-40 Degrees, to a range of 0-10 (the number of NeoPixels to display on).
def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition b - g - r - back to b. if (pos < 0) or (pos > 255): return (0, 0, 0) if (pos < 85): return (int(255 - pos*3), 0, int(pos*3)) # blue elif (pos < 170): pos -= 85 return (0, int(pos*3), int(255 - pos*3)) # green else: pos -= 170 return (int(pos * 3), int(255 - (pos*3)), 0) # red def remapRange(value, leftMin, leftMax, rightMin, rightMax): # this remaps a value from original (left) range to new (right) range # Figure out how 'wide' each range is leftSpan = leftMax - leftMin rightSpan = rightMax - rightMin # Convert the left range into a 0-1 range (int) valueScaled = int(value - leftMin) / int(leftSpan) # Convert the 0-1 range into a value in the right range. return int(rightMin + (valueScaled * rightSpan))
The loop portion of the code first reads and prints the value the temperature. In the final block of code we have some logic that checks first if there is enough of a reading to turn any lights on, then checks to see if the temperature is beyond the maximum in the range. If this is true it turns all the lights red. If the temperature is within the set range then the else command uses the wheel command and the remapRange to light up the wheel of NeoPixels. Finally, we have a short delay, we don’t need to sample the temperature very rapidly since it's not something that typically changes very rapidly.
while True: print("Temperature is: %f C" % (thermistor.temperature)) # Store thermistor reading as a variable temp = thermistor.temperature # Calculate bar height based on adjustable min/max temperature: height = n_pixels * (temp - mintemp) / (maxtemp - mintemp) # Color pixels based on rainbow gradient for i in range(0, len(pixels)): if (i >= height): pixels[i] = [0, 0, 0] elif (temp >= maxtemp): pixels[i] = [255, 0, 0] else: pixels[i] = wheel(remapRange(i, 0, (n_pixels), 85, 255)) time.sleep(0.5) # reduces flickering
Run It!
To put the code on your Circuit Playground Express, just connect your board and save your code as main.py or code.py on its drive. For more information about flashing your code, check out or CircuitPython Overview Tutorial!
If you run the serial monitor, you will see the temperature reading of the thermistor every half second. Place your finger over the thermistor on the Circuit Playground Express and see that the temperature climbs. If you have a hairdryer you can try blowing some hot air on the sensor to see the lights all turn red when the temperature exceeds 40 C. Be careful not to overheat your board!
Making a light up thermometer isn’t the end of the road for the Adafruit Circuit Playground Express and CircuitPython! There are still so many inputs to learn and incorporate into your next project! Check out our Circuit Playground Express Tutorials for more projects and info about the Circuit Playground Express, CircuitPython, and MakeCode.