### Sensor Ambient Light

B4J Raspberry Pi Experiment - BH1750 Ambient Light.
Get, in regular intervals the light intensity (lux) from a BH1750 I2C sensor.
The BH1750 I2C sensor has default address of 0x23 - checkout with sudo i2cdetect.
Parts Needed
• 1x Raspberry Pi
• 1x BH1750
• 1x GPIO Extension Board with GPIO cable, Breadboard, several jumper wires
Wiring
BH1750 = Raspberry Pi
3.3v = 3.3v (red)
SDA = SDA [Physical Pin#3] (yellow)
SCL = SCL [Physical Pin#5] (blue)
GND = GND (black)
(wirecolor)
Description
Connecting the Parts
The RasPi GPIO is connected via the GPIO Extension Board (T-Cobbler) and GPIO cable to a breadboard.
On the Breadboard, connect the BH1750 according the wiring scheme.
Developing the B4J non-UI program
The B4J non-UI program uses the B4J Library jBH1750.
Dependencies: the jPi4J Library (see category Raspberry Pi Libraries > jPi4J).
The B4J pseudo code:
• Init the BH1750 device with I2C device address 0x23 and bus number 1 (as connected to a Raspberry Pi 2.
• Set power on and start measurement at 1lx resolution. Time typically 120ms.
• Start the message loop
• Log the light intensity in lx)
Running the Experiment
On the RasPi start the B4J-Bridge by login on as user Pi, open a terminal, go to folder /home/pi/b4j and run sudo java -jar b4-bridge.jar, note the MyIp address.
Open the B4J IDE and load the B4J project file, connect to the B4J-Bridge (Menu Tools > B4J Bridge > Connect > Enter IP address.
Run the application from the B4J IDE by pressing F5.
Download Source Code
Documentation on request.
Additional Libraries
jPi4J, jBH1750
Published External
No
ToDo
• Experiment is completed.
Version
20170304
B4J non-UI Example Code
`Sub Process_Globals
Private CAPPVERSION As String = "Raspberry Pi Experiment - BH1750 Ambient Light v20170304"
' 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)
Log(CAPPVERSION)
' 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`

Changed: 2018-06-07 09:21, Category: Raspberry Pi Sample Projects, File: T6C238M1745.htm, ID: 1745