import math
from machine import Pin, I2C, ADC
import time
from i2c_lcd import I2cLcd
# Konstanta
rl10 = 50e3
gamma = 0.7
# Inisialisasi I2C untuk LCD
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Inisialisasi ADC untuk LDR
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
def calculate_resistance():
value = adc.read()
voltage_ratio = value / (4095 - value)
return 10e3 * voltage_ratio
def calculate_lux(resistance):
return 10 * math.pow(rl10 / resistance, 1 / gamma)
def setup():
lcd.clear()
lcd.putstr("Welcome To My Project...")
time.sleep(2)
lcd.clear()
def main():
setup()
while True:
# Hitung resistansi dari pembacaan ADC
resistance = calculate_resistance()
# Hitung nilai lux dari resistansi
lux_value = calculate_lux(resistance)
# Tampilkan hasil di LCD
lcd.clear()
lcd.putstr("Lux: {:.1f}".format(lux_value))
# Tampilkan juga di konsol
print("Resistance: {:.2f} kΩ, Lux: {:.2f}".format(resistance / 1000, lux_value))
time.sleep(2)
if __name__ == "__main__":
main()