# 4.3 Project 2: Temperature measurement – using the internal temperature sensor
# The internal temperature sensor is connected to ADC channel 4. The
# data read is converted into degrees Celsius using the following formula:
# Temperature (in degrees Celsius) = 27 – (reading – 0.706) / 0.001721
# En principi el programa està bé però al simulador surten valors extranys!!
from machine import Pin, ADC, I2C
from time import sleep
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
AnalogIn = ADC(4) # ADC channel 4
Conv = 3.3 / 65535 # Conversion factor
I2C_ADDR = 0x27 # Posar l'adreça que hem obtingut de l'scanner
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
while True: # Do forever
V = AnalogIn.read_u16() # Read temp
V = V * Conv # Convert to Volts
Temp = 27 - (V - 0.706) / 0.001721 # Convert to temp
lcd.clear() # Clear screen
Tempstr = str(Temp) # Convert to string
lcd.putstr(Tempstr) # Display
sleep(1) # Wait 1 second