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?
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.
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","")) return(t) # 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 = get_smooth.t get_smooth.t = get_smooth.t get_smooth.t = x xs = (get_smooth.t+get_smooth.t+get_smooth.t)/3 return(xs) 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))) time.sleep(5)