# Project 7: Read analog value from ADC and convert to temperature
print("Hello, ESP32!")
from machine import Pin, ADC #importing Pin and ADC class
from time import sleep #importing sleep class
import math
#BETA = 3950
Vref = 5 # Volt
potentiometer = ADC(Pin(32)) #creating potentiometer object
sensor = ADC(Pin(33))
#potentiometer.atten(ADC.ATTN_11DB) #3.3V full range of voltage
while True:
ADCout = potentiometer.read() #reading analog pin
Vin = Vref * ADCout / 4095 # Vin = Vref * ADCout /(2^n-1)
Temperature = 25*Vin-25 # Temperature = 25*Vin-25 (characteristic of sensor Pt-100)
print("ADCout =", ADCout) #printing the ADC value
print("Vin =", Vin, "V" ) #printing the analog value
print("Temperature =", Temperature, "oC)") #printing the temperature in oC
sleep(1)
# sensor_ADC_value = sensor.read() # value from the ADC
# sensor_value = 5000 * sensor_ADC_value / 4095 # convert ADC value to V
# temperature = 94.23 - sensor_value * 25.42 # convert mV to oC according to the LTM70 datasheet (pages 12,13)
# temperature = 1 / (math.log(1 / (4095 /sensor_value -1 ))/ BETA + 1/ 298.15) - 273.15
# celsius = 1 / (math.log(1 / (1023. / sensor_value - 1)) / BETA + 1.0 / 298.15) - 273.15
# print("sensor_ADC_value ", sensor_ADC_value)
# print("sensor_value ", sensor_value)
# print("temperature ", temperature)
# sleep(1)