from machine import Pin, ADC
from utime import sleep
from math import log
import json
import random
beta = 3950
def convert_analogue_to_temperature(analogue_value, unit="°C"):
print('analogue value:', analogue_value)
if unit == "°C":
max_value = 65535
celsius = 1 / (log(1 / (max_value / int(analogue_value) - 1)) / beta + 1.0 / 298.15) - 273.15
return celsius
analogue_output = Pin(2, Pin.IN, Pin.PULL_UP)
while True:
print('the value?',analogue_output.value())
adc = ADC(analogue_output)
# with open('diagram.json','w') as file:
# config = json.load(file)
# new_temp = random.random(1000)
# print('new temp: ',new_temp)
# config['parts'][3]['attrs']['temperature'] = new_temp
# json.dump(config, indent=4)
print(f'temperature reading is {convert_analogue_to_temperature(adc.read_u16())}°C')
sleep(2)