I've just bought an Enviro pHAT. The temperature sensor on my pHAT reads around 31 degrees Celsius for my room

This sensor is close to the Pi, so it also gets a heatwave from the CPU

Is there a better way to approximate the actual temperature?

  • 1
    You could try a sliver of ceramic under the temp sensor. – goldilocks Feb 5 '17 at 15:14
  • @goldilocks What everyday item? – R Harrington Feb 6 '17 at 14:59
  • Orient the boards differently. If sandwiched together, orient them so air can freely flow upwards between them. Also: if there is a standard increase in temperature, you can compensate for that in your reporting. The ecobee 3 'calibrates' it's temperature -- and it just occurred to me it might be doing something like temperature compensation. – Dan Esparza Apr 22 '17 at 1:49

That's the main problem with the Enviro pHAT, and the SenseHAT for that matter - proximity to the Pi affecting results.

You're better off getting a Dallas 1-wire temperature sensor, preferably one with a long cable (such as, and this is just an example, the one contained in CamJam EduKit 2), or extend one without a lead by using jumper cables. This will let you get the sensor away from the Pi.

You can then use the 1-wire protocol to read the temperature. Instructions for how to do that can be found in this worksheet.

enter image description here

  • Can I put a male 40-pin header on the Enviro pHAT and use a ribbon cable to connect it to the pi? – R Harrington Feb 18 '17 at 8:14
  • Hmmm. Yeah. There's also something called a Black Hat Hackr which might be suitable. – recantha Feb 19 '17 at 20:34

If you really don't want to extend the temperature sensor away from the Pi, it may be possible to compensate for the effect of the CPU. This post on yaab-arduino.blogspot.co.uk outlines an attempt at this in Python, using a moving average of the readings from both the temperature and pressure sensors (the pressure sensor can also measure temperature):

import os
import time
from sense_hat import SenseHat

# get CPU temperature
def get_cpu_temp():
  res = os.popen("vcgencmd measure_temp").readline()
  t = float(res.replace("temp=","").replace("'C\n",""))

# use moving average to smooth readings
def get_smooth(x):
  if not hasattr(get_smooth, "t"):
    get_smooth.t = [x,x,x]
  get_smooth.t[2] = get_smooth.t[1]
  get_smooth.t[1] = get_smooth.t[0]
  get_smooth.t[0] = x
  xs = (get_smooth.t[0]+get_smooth.t[1]+get_smooth.t[2])/3

sense = SenseHat()

while True:
  t1 = sense.get_temperature_from_humidity()
  t2 = sense.get_temperature_from_pressure()
  t_cpu = get_cpu_temp()
  h = sense.get_humidity()
  p = sense.get_pressure()

  # calculates the real temperature compesating CPU heating
  t = (t1+t2)/2
  t_corr = t - ((t_cpu-t)/1.5)
  t_corr = get_smooth(t_corr)

  print("t1=%.1f  t2=%.1f  t_cpu=%.1f  t_corr=%.1f  h=%d  p=%d" % (t1, t2, t_cpu, t_corr, round(h), round(p)))

  • Is there any way you could make that code suitable for the Enviro pHAT? – R Harrington Feb 20 '17 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.