from machine import ADC, Pin, I2C
from i2c_lcd import I2cLcd
import time
AddressOfLcd = 0x27
i2c = I2C(0, scl=Pin(19), sda=Pin(18), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
class LDR:
def __init__(self, pin, min_value=0, max_value=100):
if min_value >= max_value:
raise Exception('Min value is greater or equal to max value')
self.adc = ADC(Pin(pin))
self.adc.atten(ADC.ATTN_11DB)
self.min_value = min_value
self.max_value = max_value
def read(self):
return self.adc.read()
def value(self):
return (self.max_value - self.min_value) * self.read() / 4095
ldr = LDR(35)
led1 = Pin(2, Pin.OUT)
led2 = Pin(4, Pin.OUT)
led3 = Pin(5, Pin.OUT)
while True:
value = ldr.value()
print('Value = {:.2f}'.format(value))
lcd.clear()
lcd.putstr('Value: {:.2f} ohm'.format(value))
if value <= 25:
led1.on()
led2.off()
led3.off()
elif value <= 75:
led1.off()
led2.on()
led3.off()
else:
led1.off()
led2.off()
led3.on()
time.sleep(1)
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
ldr1:VCC
ldr1:GND
ldr1:DO
ldr1:AO
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL