from machine import ADC, Pin, SoftI2C
import time
from servo import Servo
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
class LDR:
def __init__(self, pin):
self.ldr_pin = ADC(Pin(pin))
def get_raw_value(self):
return self.ldr_pin.read_u16()
def get_light_percentage(self):
return round(self.get_raw_value()/65535*100,2)
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
ldr = LDR(27)
motor=Servo(pin=5)
while True:
light_percent = ldr.get_light_percentage()
lcd.clear()
lcd.putstr("Light : ")
lcd.putstr(str(light_percent))
if light_percent > 10:
motor.move(90)
else: motor.move (0)
time.sleep(2)