B4J Library [Raspberry Pi] jBH1750 - Ambient Light Sensor

Library for the BH1750 Ambient Light I2C sensor connected to a Raspberry Pi.

upload_2017-3-6_11-44-58.png


Install
Copy jbh1750.jar, jbh1750.xml to the B4J additional libraries folder.

Dependecies
On jPi4J and the pi4j jar files (core, device, gpio-extension, service).
Ensure the additional library jar files are located in the B4J additional libraries folder.

Wiring
B4X:
BH1750 = Raspberry Pi
3.3v = 3.3v
SDA = SDA [Physical Pin#3]
SCL = SCL [Physical Pin#5]
GND = GND

I2C Address
0x23

Example B4J Non-UI Program
B4X:
Sub Process_Globals
  'Define the BH1750 device. The I2C address used is 0x23 - check out with sudo i2cdetect
  Private bh As BH1750
  'Define the request timer and interval
  Private RequestTimer As Timer
  Private RequestInterval As Long = 2000
End Sub

Sub AppStart (Args() As String)
  ' Init the BH1750 device with device address 0x23 or 0x5C and bus number 0 (Pi 1) or 1 (Pi 2,3)
  If bh.Initialize(0x23, 1) = False Then Return
  ' Power on and set the measurement mode
  bh.PowerOnSetMode(bh.CONTINUOUS_HIGH_RES_MODE_1)
  Log("BH1750 Initialized")
  ' Get the first value
  RequestTimer_Tick
  ' Init and start the request timer
  RequestTimer.Initialize("RequestTimer", RequestInterval)
  RequestTimer.Enabled = True
  ' Do not forget to start the message loop
  StartMessageLoop
End Sub

' Handle timer requests
Sub RequestTimer_Tick
  Log($"${DateTime.Time(DateTime.Now)} ${NumberFormat(bh.LightIntensity,0,0)} lux"$)
End Sub

Output
11:30:56 110 lux
11:30:58 62 lux
11:31:00 290 lux
11:31:02 288 lux
11:31:04 288 lux

ChangeLog
20170307 v1.0
 

Attachments

  • jBH1750.zip
    5.6 KB · Views: 390
Top