-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2cLDR.py
35 lines (26 loc) · 972 Bytes
/
i2cLDR.py
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
26
27
28
29
30
31
32
33
34
#!/usr/bin/python
import smbus
import time
# Define some constants from the datasheet
# http://www.pibits.net/code/raspberry-pi-bh1750-light-sensor.php
class LDR:
DEVICE = 0x23 # Default device I2C address
POWER_DOWN = 0x00 # No active state
POWER_ON = 0x01 # Power on
RESET = 0x07 # Reset data register value
ONE_TIME_HIGH_RES_MODE = 0x20
def __init__(self):
self.bus = smbus.SMBus(1) # Rev 2 Pi uses 1
def __convertToNumber(self, data):
# Simple function to convert 2 bytes of data
# into a decimal number
return (data[1] + (256 * data[0])) / 1.2
def readLight(self, addr=None):
addr = addr if addr else LDR.DEVICE
data = self.bus.read_i2c_block_data(addr, LDR.ONE_TIME_HIGH_RES_MODE)
return self.__convertToNumber(data)
if __name__ == "__main__":
ldr = LDR()
while True:
print("Light Level : " + str(ldr.readLight()) + " lux")
time.sleep(0.5)