27 lines
657 B
Python
27 lines
657 B
Python
import time
|
||
from machine import Pin, ADC, I2C
|
||
from screen import Screen
|
||
|
||
# ----- ADC (Grove Light Sensor) -----
|
||
light_adc = ADC(Pin(1))
|
||
light_adc.atten(ADC.ATTN_11DB) # 0–3.3V range
|
||
light_adc.width(ADC.WIDTH_12BIT) # 0–4095
|
||
|
||
screen = Screen()
|
||
|
||
def read_light(samples=8):
|
||
total = 0
|
||
for _ in range(samples):
|
||
total += light_adc.read()
|
||
time.sleep_ms(5)
|
||
return total // samples
|
||
|
||
while True:
|
||
raw = read_light()
|
||
percent = int((raw / 4095) * 100)
|
||
|
||
screen.display("Light Sensor", 15, 0)
|
||
screen.display("Raw: {}".format(raw), 15, 20)
|
||
screen.display("Level: {}%".format(percent), 15, 40)
|
||
|
||
time.sleep(0.5) |