from machine import ADC
import math
import utime
from machine import SoftI2C, Pin
from ssd1306 import SSD1306_I2C
# Setup OLED
i2c = SoftI2C(scl=Pin(3), sda=Pin(2))
oled = SSD1306_I2C(128, 64, i2c)
# NTC constant parameters
ntc_resistance = 10000 # NTC resistance value(Unit: ohms)
ntc_beta = 3950 # NTC Beta coefficency
# Initilize ADC
adc = ADC(28) #set GPIO 28 as the ADC input channel for NTC output
# Read NTC sensor ADC and calculate temperature
def read_temperature():
# Read ADC
adc_value = adc.read_u16()
adc_value = adc_value & 0xFFF
#print(adc_value)
# Calculate resistance
resistance = (4095 / adc_value) * 3300 - 3300
# Calculate temperature with Steinhart-Hart formula
steinhart = resistance / ntc_resistance
steinhart = math.log(steinhart)
steinhart /= ntc_beta
steinhart += 1.0 / (25 + 273.15)
steinhart = 1.0 / steinhart
temperature = steinhart - 273.15
return temperature
# Main Function
# Startup display
oled.text("JX College",25,30)
oled.show()
utime.sleep(2)
oled.fill(0)
while True:
temp = read_temperature()
print("Temperature: {} degrees Celsius".format(temp))
oled.text("Real Temperature:",0,25)
oled.text(str('%.2f C'%(temp)),35,35)
# OLED start show
oled.show()
utime.sleep(1)
oled.fill(0)