Esp8266 Team Hack Temperature Sensing

by querry43 in Circuits > Microcontrollers

282 Views, 0 Favorites, 0 Comments

Esp8266 Team Hack Temperature Sensing

IMG_8561.JPG
thermistor pins.png
IMG_8563.JPG

The team hack kit also includes a temperature sensor. It is very similar to the photoresistor; temperature effects the resistance of a thermistor, and with the resistor on the yellow leg, forms a voltage divider which we read with an ADC pin. Unlike the photoresistor configuration, the voltage will fall when the sensor gets hotter. This is the most common configuration for this sort of sensor.

Connect the thermistor following the images and the mapping below.

Yellow -> 3V3
Brown  -> GND
White  -> A0

Reading Temperature

We already know how to read from ADC (see Esp8266 Team Hack Light Sensing) but it is more useful to get an actual temperature value. We are going to borrow the steinhart temperature function from adafruit. This block of code will read the temperature every second and print it out in celcius.

# read_temp.py

from machine import ADC
from time import sleep_ms
import math

def steinhart_temperature_C(r, Ro=10000.0, To=25.0, beta=3950.0):
    steinhart = math.log(r / Ro) / beta      # log(R/Ro) / beta
    steinhart += 1.0 / (To + 273.15)         # log(R/Ro) / beta + 1/To
    steinhart = (1.0 / steinhart) - 273.15   # Invert, convert to C
    return steinhart

therm = ADC(0)

while True:
    r = 10000 / (65535 / therm.read_u16() - 1)
    temp = steinhart_temperature_C(r)
    print(temp)
    sleep_ms(1000)

Load it and run it as usual.

import read_temp

Nest Steps

What can we do with this?

  • Combine this with a smart outlet controlling a heat source to maintain the temperature of your yogurt or bread starter.
  • Water your garden more when it is sunny and hot.
  • Tweet if your fish tank leaves safe temperatures.