-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldr.py
More file actions
25 lines (20 loc) · 654 Bytes
/
Copy pathldr.py
File metadata and controls
25 lines (20 loc) · 654 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from machine import ADC
import time
class LDR:
def __init__(self, pin: int = 27):
self.ldr = ADC(pin)
def measure(self, dt: float = 1e-2) -> float:
""" Measures an inverse light level metric over the next `dt` seconds.
Typical values: dark room >5000, lit room ~2000, no LDR connected ~15000.
"""
n = val = 0
t_stop = time.time_ns() + int(dt*1e9)
while time.time_ns() < t_stop or n == 0:
val += self.ldr.read_u16()
n += 1
return val/n
if __name__ == "__main__":
ldr = LDR()
while True:
print(ldr.measure())
time.sleep(1)