The Circuit Playground Express and Bluefruit have a temperature sensor built in, next to the little thermometer printed on the board. Though the images are of the Circuit Playground Express, the sensor is in essentially the same location on the Bluefruit. It's near the A9 label on the board. It returns the temperature in Celsius.
Add the following code to your code.py. Remember, if you need help with this, check here.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """This example uses the temperature sensor on the Circuit Playground, located next to the image of a thermometer on the board. It prints the temperature in both C and F to the serial console. Try putting your finger over the sensor to see the numbers change!""" import time from adafruit_circuitplayground import cp while True: print("Temperature C:", cp.temperature) print("Temperature F:", cp.temperature * 1.8 + 32) time.sleep(1)
Open the serial console to see the temperature printed out. Try holding your finger over the thermometer printed on the board to see the values change!
Let's take a look at the code. We import time
and cp
.
Inside our loop, we print Temperature C:
, followed by the temperature value, cp.temperature
. This prints the temperature in Celsius.
But what if you're used to the temperature in Fahrenheit? It's as easy as a little math to display that as well. After printing the temp in C, we print Temperature F:
, followed by cp.temperature
again, this time modified by * 1.8 + 32
, to convert it to Fahrenheit.
Then we have a time.sleep(1)
to slow down the readings. If they're too fast, they're hard to read!
Plotting Temperature
Let's take a look at these values on the Mu plotter! Add the following code to your code.py:
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """If you're using Mu, this example will plot the temperature in C and F on the plotter! Click "Plotter" to open it, and place your finger over the sensor to see the numbers change. The sensor is located next to the picture of the thermometer on the CPX.""" import time from adafruit_circuitplayground import cp while True: print("Temperature C:", cp.temperature) print("Temperature F:", cp.temperature * 1.8 + 32) print((cp.temperature, cp.temperature * 1.8 + 32)) time.sleep(0.1)
The code is almost identical, but we've added one line: print((cp.temperature, cp.temperature * 1.8 + 32))
.
Note that the Mu plotter looks for tuple values to plot. Tuples in Python come in parentheses ()
with comma separators. If you have two values, a tuple would look like (1.0, 3.14)
- note the parentheses around the number set, and the comma between. That's why there's an extra set of parenthesis around and a comma between the two temperature values in print((cp.temperature, cp.temperature * 1.8 +32))
.
As well, the Mu plotter requires that the tuple value be on a line all its own. That's why we can't simply add extra parenthesis and a comma to the print("Temperature C:", cp.temperature)
line. The plotter doesn't know what to do with it if there's other information in there.
Click on the Plotter button on the top of Mu to see the plotter. Try breathing on your Circuit Playground to watch the plotter go up. Try setting it on an ice pack to watch the plotter go down!
Temperature Meter
You can also use the temperature values to create a fun light meter using the NeoPixels on your Circuit Playground! Add the following code to your code.py:
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ This example use the temperature sensor on the Circuit Playground, located next to the picture of the thermometer on the board. Try warming up the board to watch the number of NeoPixels lit up increase, or cooling it down to see the number decrease. You can set the min and max temperatures to make it more or less sensitive to temperature changes. """ import time from adafruit_circuitplayground import cp cp.pixels.auto_write = False cp.pixels.brightness = 0.3 # Set these based on your ambient temperature in Celsius for best results! minimum_temp = 24 maximum_temp = 30 def scale_range(value): """Scale a value from the range of minimum_temp to maximum_temp (temperature range) to 0-10 (the number of NeoPixels). Allows remapping temperature value to pixel position.""" return int((value - minimum_temp) / (maximum_temp - minimum_temp) * 10) while True: peak = scale_range(cp.temperature) print(cp.temperature) print(int(peak)) for i in range(10): if i <= peak: cp.pixels[i] = (0, 255, 255) else: cp.pixels[i] = (0, 0, 0) cp.pixels.show() time.sleep(0.05)
Now try holding your finger over the thermometer printed on your Circuit Playground and watch the LEDs light up! Remove your finger to watch the number of LEDs lit up change.
Let's take a look at the code. First we import time
, and cp
.
Next, we set cp.pixels.auto_write = False
. This means that anything we tell the LEDs to do will not happen automatically. By default, this is set to True
. This means, we tell the LEDs to turn on, and they turn on. If it's set to False
, it means we have to include cp.pixels.show()
after anything we try to tell the LEDs to do. This is required for this code to work since the LEDs turn on based on the temperature values.
We set the brightness
to 0.3
, or 30%.
You should be able to see what the temperature changes are from when the Circuit Playground is simply sitting on your desk and when you're holding your finger over it. For best results, change the minimum_temp
and maximum_temp
to fit your ambient temperature values. Otherwise, you might not get the best results from the temperature meter. When sitting here, the minimum was about 24 degrees, and when holding a finger on it, the maximum was about 30. This is how we chose the values already in the code.
Next we have a helper function called scale_range
. The temperature range is currently 24-30 but there are 10 NeoPixels. So, we include a helper function that scales the 24-30 range to 0-9 so we can map light levels to pixel position.
Our loop begins with setting peak = scale_range(cp.temperature)
. Then we print the cp.temperature
values and the peak
values.
The next section takes the peak
value and says for the total number of LEDs, whatever number peak
is equal to or less than, light up that many LEDs, and otherwise turn them off. So, if peak is 4, light up 4 LEDs!
Then we have cp.pixels.show()
to make the LEDs light up. And a time.sleep(0.05)
to create a little delay.
You can change the number values in cp.pixels[i] = (0, 255, 255)
to change the color of the temperature meter. Give it a try!
Page last edited January 22, 2025
Text editor powered by tinymce.