User Tools

Site Tools


bh1750

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

bh1750 [03.01.2017 14:43]
andreas created
bh1750 [03.01.2017 14:46] (current)
andreas [Test-Program in C]
Line 115: Line 115:
 Current light: 108 Lux (0%) Current light: 108 Lux (0%)
 Current light: 103 Lux (0%) Current light: 103 Lux (0%)
 +</​code>​
 +
 +===== Test-Program in Python =====
 +
 +Small simple test programm to read every second the light value from the sensorusing python:
 +
 +<code python>
 +#​!/​usr/​bin/​python
 +import smbus
 +import time
 +
 +# Constants taken from the datasheet
 +
 +DEVICE ​    = 0x23 # Default device I2C address
 +
 +POWER_DOWN = 0x00 # No active state
 +POWER_ON ​  = 0x01 # Power on
 +RESET      = 0x07 # Reset data register value
 +
 +# Start measurement at 4 lx resolution. Time typically 16ms.
 +CONTINUOUS_LOW_RES_MODE = 0x13
 +
 +# Start measurement at 1 lx resolution. Time typically 120ms
 +CONTINUOUS_HIGH_RES_MODE_1 = 0x10
 +
 +# Start measurement at 0.5 lx resolution. Time typically 120ms
 +CONTINUOUS_HIGH_RES_MODE_2 = 0x11
 +
 +# Start measurement at 1 lx resolution. Time typically 120ms
 +# Device is automatically set to Power Down after measurement.
 +ONE_TIME_HIGH_RES_MODE_1 = 0x20
 +
 +# Start measurement at 0.5 lx resolution. Time typically 120ms
 +# Device is automatically set to Power Down after measurement.
 +ONE_TIME_HIGH_RES_MODE_2 = 0x21
 +
 +# Start measurement at 1 lx resolution. Time typically 120ms
 +# Device is automatically set to Power Down after measurement.
 +ONE_TIME_LOW_RES_MODE = 0x23
 +
 +#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
 +bus = smbus.SMBus(1) ​ # Rev 2 Pi uses 1
 +
 +def convertToNumber(data):​
 +  # Simple function to convert 2 bytes of data
 +  # into a decimal number
 +  return ((data[1] + (256 * data[0])) / 1.2)
 +
 +def readLight(addr=DEVICE):​
 +  data = bus.read_i2c_block_data(addr,​ ONE_TIME_HIGH_RES_MODE_2)
 +  return convertToNumber(data)
 +
 +def main():
 +
 +  while True:
 +    print "Light Level : " + str(readLight()) + " lx"
 +    time.sleep(1)
 +
 +if __name__=="​__main__":​
 +   ​main()
 +
 +</​code>​
 +
 +This creates an output like:
 +
 +<​code>​
 +pi@DevPi:​~/​BH1750 $ python bh1750.py ​
 +Light Level : 190.0 lx
 +Light Level : 188.333333333 lx
 +Light Level : 189.166666667 lx
 +Light Level : 190.833333333 lx
 +Light Level : 186.666666667 lx
 +Light Level : 190.833333333 lx
 +Light Level : 189.166666667 lx
 +Light Level : 187.5 lx
 +Light Level : 191.666666667 lx
 </​code>​ </​code>​
bh1750.txt ยท Last modified: 03.01.2017 14:46 by andreas