Chapter 5 Temperature and accuracy
Another sensor on the board is the temperature sensor. Unlike the light sensor, the data it returns is very easy to understand: It’s the temperature in Celcius.
import time
from adafruit_circuitplayground import cp
while True:
print(cp.temperature)
time.sleep(1)
If you save that to your board and look at the Serial output, you’ll see numbers probably around 24. That’s 24 degrees Celcius. You can add things to the print statement to help you out.
import time
from adafruit_circuitplayground import cp
while True:
print(cp.temperature, "C")
time.sleep(1)
Remember in Chapter 2 when we converted C to F? Let’s bring that back.
import time
from adafruit_circuitplayground import cp
def convert(temp):
fahren = (temp * 1.8) + 32
print(fahren)
while True:
convert(cp.temperature)
time.sleep(1)
Now that is telling me that my office is about 77.8 degrees Fahrenheit. Now I’m notoriously Dad Energy on this – air conditioning is expensive, so I don’t care if you’re warm – but my basement office is quite cool. It isn’t 77.8.
So what is it?
That raises questions about how accurate are your sensors. The truth is this is one place where you get what you pay for. Do you need extremely accurate temperature readings because any change will cost your business money? Then you spend whatever it takes to get the most accurate sensor. Are you making a journalism project where a little fuzziness is not a huge deal? Then the sensor isn’t going to cost as much.
But can we make this better without spending more money? We can.
5.1 Turning down the noise
When planning a sensor journalism project, there’s several questions you’re going to have to answer given your requirements and given your available equipment.
Two critical questions will be:
- How accurate does the measurement have to be? Is good enough good enough?
- How precise does the measurement need to be?
For example, my readings are saying things like 78.6844. Do we really need four digits of precision? Can you tell the difference between 78.6844 and 78.6855? Do people talk about temperature with decimals?
And notice that the sensor is sensitive enough that the temperature goes up and down with each reading, no matter how frequently you ask for it. We’ve got it set for every second. Do micro differences in temperature every second matter?
There are some things we can do in code to deal with these questions.
First, we can average a collection of temperature readings to smooth it out. To do this, we’re going to create an empty list, then populate it with readings every second. Then, when we hit a threshold level – we’ll start with 5 – we’ll average them together. We’ll clear out the list, and start it all over again.
import time
from adafruit_circuitplayground import cp
temps = [] # here's the empty list
def convert(temp):
fahren = (temp * 1.8) + 32
return fahren
while True: # do this forever
while len(temps) != 5: # while the number of readings in our list isn't 5
temps.append(convert(cp.temperature)) # append each temperature reading in Fahrenheit to the list
time.sleep(1) # wait a second and repeat it again
print(sum(temps)/5) # once the while loop breaks, we'll add them up and divide by 5 and print it
temps.clear() # empty the list so the inner while loop can start again
That, by itself, smooths out the readings some.
But what else can we do?
Let’s eliminate the decimal points.
import time
from adafruit_circuitplayground import cp
temps = []
def convert(temp):
fahren = (temp * 1.8) + 32
return fahren
while True:
while len(temps) != 5:
temps.append(convert(cp.temperature))
time.sleep(1)
avg = sum(temps)/5
print("%.0f" % round(avg, 0)) # the first formats the output to have zero digits, the second part rounds
temps.clear()
Now my sensor says its between 78 and 79 in my office, which is much more like how we are accustomed to talking about temperature. But it is right?
5.2 Low cost validation and calibration
As I said earlier, I’m pretty Dad Energy about the temperature in my house. But a basement office is generally cooler than the rest of the house – so much so that I have the vents closed so as to not add more cold air in the room. So how is it telling me that my office is 79 when the thermostat upstairs is set at 76?
One thing we can do is compare our sensor to an external sensor and compare. How different is our measurement from a better sensor.
My house has an Ecobee Smart Thermostat system and I’ve got a movable remote temperature sensor. Since I trust it to cool my house, I’m going to trust it here to give a better reading on the temperature in my office.
So what does the sensor say it is? 75.
The sensor, averaging five readings and averaging them together before rounding them to the nearest whole degree – now reads 78.
Simple solution? Knock three off our sensor reading to make it closer to the external sensor, which much more conforms with my understanding of what it actually feels like in my office.
Your situation is going to be different, so you’ll need an external temperature gauge to validate your sensor output and adjust accordingly. But here’s my code.
import time
from adafruit_circuitplayground import cp
temps = []
def convert(temp):
fahren = (temp * 1.8) + 32
return fahren
while True:
while len(temps) != 5:
temps.append(convert(cp.temperature))
time.sleep(1)
avg = (sum(temps)/5) - 3 # the - 3 is the adjustment to be as close to reality as we can get.
print("%.0f" % round(avg, 0))
temps.clear()