from machine import Pin, SoftI2C, ADC
from math import log
from i2c_lcd import I2cLcd
from time import sleep
ADC_temp = ADC(Pin(2)) # pin 2 will be used to monitor temperature
ADC_light = ADC(Pin(15)) # pin 15 will be used to monitor light level
lcd1_address = 0x27
lcd2_address = 0x26
i2c = SoftI2C(scl = Pin(22), sda = Pin(23), freq = 400000)
lcd1 = I2cLcd(i2c, lcd1_address, 2, 16) # the display has 2 rows and 16 columns
lcd2 = I2cLcd(i2c, lcd2_address, 2, 16) # the display has 2 rows and 16 columns
lcd1.backlight_off()
my_char = bytearray([
0b00100,
0b10101,
0b10101,
0b11111,
0b00100,
0b00100,
0b00100,
0b00100
])
lcd2.custom_char(0, my_char) # store the new character at location 0
lcd2.putchar(chr(0)) # display character stored at index 0 in LCD memory
def get_temperature():
beta = 3950 # beta coefficient of the thermistor being used
kelvin = 273.15 # value of temperature in Kelvin at zero Celsius
temp_value = ADC_temp.read()
# Convert the analog value into temperature
temperature_in_celsius = 1 / (log(1 / (4096 / temp_value - 1))/beta + 1.0 / 298.15) - kelvin
temperature_in_celsius=round(temperature_in_celsius,1)
return temperature_in_celsius
def get_lux():
GAMMA = 0.7
RL10 = 50
light_value = ADC_light.read()
# Convert the analog value into an approximate lux value
voltage = light_value / 4095 * 5
resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))
lux = round(lux,1)
return lux
while True:
lcd1.move_to(0,0)
temp_reading = get_temperature()
lcd1.putstr('Temp is: ') # print to first LCD display
lcd1.putstr(str(temp_reading))
lcd1.putstr(' ')
lcd2.move_to(0,0)
lux_reading = get_lux()
lcd2.putstr(str(lux_reading)) # print to second LCD display
lcd2.putstr(' Lux ')
if lux_reading<1000:
lcd2.move_to(3, 1) # move cursor to column 5 and row 1
lcd2.putchar(chr(0)) # print Trident at RAM display storage location 0
lcd1.backlight_on()
else:
lcd2.move_to(3, 1) # move cursor to column 5 and row 1
lcd2.putstr(' ')
lcd1.backlight_off()