#Question 1
from machine import Pin,I2C,Timer,ADC
from pico_i2c_lcd import I2cLcd
import utime
import random
#can only use 0,1 or 21,22 in hardware pi pico
sda=Pin(0)
scl=Pin(1)
i2c=I2C(0,sda=sda,scl=scl,freq=400000)
devices=i2c.scan()
i2caddr=devices[0]
pot=ADC(Pin(26))# set the poteniometer
lcd=I2cLcd(i2c,i2caddr,2,16)
while True:
val=pot.read_u16()
print(val)
angle=90-(val/65535)*90 #65535 is the max value
lcd.clear()
lcd.putstr(str(angle))
utime.sleep(1)
# Question 2
from machine import Pin,I2C,Timer,ADC
from pico_i2c_lcd import I2cLcd
import utime
import random
import time
import math
#can only use 0,1 or 21,22 in hardware pi pico
sda=Pin(0)
scl=Pin(1)
i2c=I2C(0,sda=sda,scl=scl,freq=400000)
devices=i2c.scan()
i2caddr=devices[0]
ntc=ADC(Pin(27))# set the
lcd=I2cLcd(i2c,i2caddr,2,16)
def adc_to_temperature(adc_value):
temp_celsius = 1 / (math.log((1 / (65536/adc_value)-1) / 3950 + (1 / 298.15))) - 273.15
return temp_celsius
red_led=Pin(4,Pin.OUT)
green_led=Pin(2,Pin.OUT)
yellow_led=Pin(3,Pin.OUT)
temp_out_of_range_start = None
def fn(timer):
global temp_out_of_range_start
adc_value = ntc.read_u16()
temp_celsius = adc_to_temperature(adc_value)
print(time.time())
lcd.clear()
lcd.putstr(str(temp_celsius))
if 10 <= temp_celsius <= 33:
green_led.on()
yellow_led.off()
red_led.off()
temp_out_of_range_start = None
else:
yellow_led.on()
green_led.off()
red_led.off()
# Start counting time outside the optimal range( for first time)
if temp_out_of_range_start is None:
temp_out_of_range_start = time.time()
#when temp_out_of_range value is set(not None)
elif time.time() - temp_out_of_range_start > 5:
yellow_led.off()
red_led.on()
lcd.clear()
lcd.putstr("DANGER!")
utime.sleep(1)
timer=Timer(freq=1,mode=Timer.PERIODIC,callback=fn)