diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..be05977
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,3 @@
+Language: Cpp
+IndentPPDirectives: AfterHash
+PointerAlignment: Left
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..0124dea
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,68 @@
+name: checks
+on: [push, pull_request]
+jobs:
+ lint:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout Repo
+ uses: actions/checkout@v2
+ - name: Check Lint
+ uses: arduino/arduino-lint-action@v1.0.0
+ with:
+ library-manager: update
+ compliance: strict
+ - name: Check Code Format
+ run: |
+ sudo apt install clang-format
+ clang-format --version
+ bash ./ci/code-format.bash
+ compile:
+ runs-on: ubuntu-latest
+ strategy:
+ matrix:
+ arduino-platform: ["arduino:avr", "esp8266:esp8266", "esp32:esp32"]
+ # In addition to the platform we also need to define the FQBN of the
+ # boards we want to test for each platform.
+ include:
+ - arduino-platform: "arduino:avr"
+ fqbn: "arduino:avr:uno"
+ additional-urls-arg: ""
+ - arduino-platform: "esp8266:esp8266"
+ fqbn: "esp8266:esp8266:generic"
+ additional-urls-arg: "--additional-urls http://arduino.esp8266.com/stable/package_esp8266com_index.json"
+ - arduino-platform: "esp32:esp32"
+ fqbn: "esp32:esp32:esp32"
+ additional-urls-arg: "--additional-urls https://dl.espressif.com/dl/package_esp32_index.json"
+ steps:
+ - name: Checkout Repo
+ uses: actions/checkout@master
+ - name: Setup Arduino CLI
+ uses: arduino/setup-arduino-cli@v1.1.1
+ - name: Setup Python for esptool used during ESP32 compile
+ uses: actions/setup-python@v2
+ if: ${{ matrix.arduino-platform == 'esp32:esp32' }}
+ with:
+ python-version: '3.9'
+ - name: Install Python Dependencies
+ if: ${{ matrix.arduino-platform == 'esp32:esp32' }}
+ run: |
+ python -m pip install --upgrade pip
+ pip install esptool
+ - name: Install Repo as Library
+ run: |
+ mkdir -p "$HOME/Arduino/libraries"
+ ln -s "$PWD" "$HOME/Arduino/libraries/."
+ - name: Install Arduino core and platform
+ run: |
+ arduino-cli core update-index ${{ matrix.additional-urls-arg }}
+ arduino-cli core install ${{ matrix.arduino-platform }} ${{ matrix.additional-urls-arg }}
+ - name: Compile Platform Common Sketches
+ run: |
+ arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750test/BH1750test.ino
+ arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750advanced/BH1750advanced.ino
+ arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750onetime/BH1750onetime.ino
+ arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750autoadjust/BH1750autoadjust.ino
+ - name: Compile ESP32 Platform Specific Sketches
+ if: ${{ matrix.arduino-platform == 'esp32:esp32' }}
+ run: |
+ arduino-cli compile --fqbn ${{ matrix.fqbn }} ./examples/BH1750two_i2c/BH1750two_i2c.ino
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index f1bb044..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,32 +0,0 @@
-language: cpp
-env:
- global:
- - ARDUINO_PACKAGE_VERSION=1.8.5
- - DISPLAY=:1.0
-
-before_install:
- # arduino requires an X server even with command line
- - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/xvfb_$TRAVIS_JOB_NUMBER.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
-
-install:
- - wget -q -O- http://downloads.arduino.cc/arduino-$ARDUINO_PACKAGE_VERSION-linux64.tar.xz | tar -Jxf -
- - sudo mv arduino-$ARDUINO_PACKAGE_VERSION /usr/local/share/arduino
- - sudo ln -s /usr/local/share/arduino/arduino /usr/local/bin/arduino
- # Add esp8266 board support
- # - arduino --pref "boardsmanager.additional.urls=http://arduino.esp8266.com/stable/package_esp8266com_index.json" --save-prefs
- # - arduino --install-boards esp8266:esp8266
- - arduino --pref "compiler.warning_level=all" --save-prefs
- # link project directory into Arduino libraries area.
- - ln -s $PWD /usr/local/share/arduino/libraries/BH1750
-
-script:
- # uno
- - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750test/BH1750test.ino
- - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750advanced/BH1750advanced.ino
- - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750onetime/BH1750onetime.ino
- - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750autoadjust/BH1750autoadjust.ino
- # esp8266
- # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570test/BH1750test.ino
- # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570advanced/BH1750advanced.ino
- # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1750onetime/BH1750onetime.ino
- # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1750autoadjust/BH1750autoadjust.ino
diff --git a/README.md b/README.md
index 8d6e0b6..fdaa909 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# BH1750
-[](https://travis-ci.org/claws/BH1750)
+[](https://github.com/claws/BH1750/actions)
This package contains an Arduino library for digital light sensor breakout boards containing the
BH1750FVI IC.
@@ -44,14 +44,14 @@ Usually you will get an integer value which represent the lux equivalent.
- High Resolution Mode - (generic range: 0.0 up to 54612.5 lux)
- High Resolution Mode 2 - (generic range: 0.0 up to 27306.25 lux)
-The sensor itself returns a 16 bit unsigned integer. Therefore the maximum value is limited in general.
+The sensor itself returns a 16 bit unsigned integer. Therefore the maximum value is limited in general.
The standard conversion between the so called 'counts' to lux is 1/1.2, that means you get a smaller value.
As we use float, if an error occurs you will get a negative value.
- -1 no valid data was transmitted from the sensor
- -2 device is not configured
-Otherwise the measured counts are converted to lux and returned. If no advanced parameters are changed the maximum lux value is 54612.5 lx.
-
-As the sensor counts impact of light in a specific time frame you could change this time frame.
+Otherwise the measured counts are converted to lux and returned. If no advanced parameters are changed the maximum lux value is 54612.5 lx.
+
+As the sensor counts impact of light in a specific time frame you could change this time frame.
This is needed if you use an overlay window or compensate for environmental influence like darkness.
This time frame is defined by a register which is called MTreg. Therefore you could choose a value between 32 and 254.
The default value is 69; keep in mind that the measurement time is changed accordingly.
@@ -62,19 +62,19 @@ The datasheet for the BH1750 chip can be obtained
## Installation [](https://www.ardu-badge.com/BH1750)
-- **(For Arduino >= 1.5.x)** Install this package by searching for it in the
+- **(For Arduino >= 1.5.x)** Install this package by searching for it in the
Arduino Library Manager and then clicking ``install``. Alternatively, this
- library can be installed manually by clicking "Clone or download" -> "Download ZIP"
- button. Then open Arduino IDE, click `Sketch -> Include library -> Add .ZIP library`
+ library can be installed manually by clicking "Clone or download" -> "Download ZIP"
+ button. Then open Arduino IDE, click `Sketch -> Include library -> Add .ZIP library`
and select the downloaded archive.
-- **(For Arduino < 1.5.x)** Download this package as an archive by clicking
- "Clone or download" -> "Download ZIP" button. Then extract the archive to
- ``/My Documents/Arduino/libraries/`` folder and rename
+- **(For Arduino < 1.5.x)** Download this package as an archive by clicking
+ "Clone or download" -> "Download ZIP" button. Then extract the archive to
+ ``/My Documents/Arduino/libraries/`` folder and rename
it to `BH1750`. Restart IDE.
The following YouTube [video](https://youtu.be/ACTMQvPVMLs) (specifically from
-7:20 onwards) provides a good overview of manually installing this library and
+7:20 onwards) provides a good overview of manually installing this library and
loading an example using the Arduino IDE.
[](https://youtu.be/ACTMQvPVMLs?t=437)
@@ -159,7 +159,69 @@ Light: 335.0 lx
Light: 332.0 lx
```
-## More Examples
+### More Examples
The ``examples`` directory contains more advanced use cases such as using different modes, I2C addresses and multiple Wire instances.
+## Developers
+
+The following information is for developers of this library.
+### Code Format
+
+The code in this project is formatted using ``clang-format`` tool.
+
+Good instructions for installing ``clang-format`` can be found
+[here](https://learn.adafruit.com/the-well-automated-arduino-library/formatting-with-clang-format)
+
+Once the ``clang-format`` tool has been install you can then run the
+convenience script (``ci/code-format.bash``) to check or apply the code
+formatting. The script should be run from the repo's top level directory.
+
+```shell
+$ ./ci/code-format.bash
+```
+This script is also run as part of the project's continuous integration
+checks.
+
+If you make changes to code files then the code format can be applied
+by simply passing *apply* as an argument to the script.
+
+```shell
+$ ./ci/code-format.bash apply
+```
+
+### Code Linting
+
+The code in this project is linted using ``arduino-lint``. The tool can be
+installed using the instructions [here](https://arduino.github.io/arduino-lint/latest/installation/).
+
+To run the linter over the project use the command below.
+
+```shell
+$ arduino-lint --library-manager update --compliance strict
+```
+
+The same command is run as part of the project's continuous integration
+checks.
+
+If errors or warnings are reported then fix them and re-run the script
+until they are resolved.
+
+### Build Locally
+
+The code in this project can be built locally using the ``arduino-cli`` tool.
+The tool can be installed using the instructions [here](https://github.com/arduino/arduino-cli#quickstart). Once you have the tool installed you can compile the
+examples scripts using the convenience script (``ci/compile-examples.bash``).
+
+```shell
+$ ./ci/compile-examples.bash
+```
+
+This script performs the same actions as the project's continuous integration
+compile checks.
+
+### Release Process
+
+- Update ``library.json`` and ``library.properties`` version strings.
+- Create a new project release and use the new version number as tag. Click Publish.
+- Now wait about an hour for it to appear in the Arduino library manager.
diff --git a/build-examples.bash b/build-examples.bash
deleted file mode 100755
index c2916bc..0000000
--- a/build-examples.bash
+++ /dev/null
@@ -1,36 +0,0 @@
-#!/usr/bin/env bash
-#
-# A simple script to automate building BH1750 examples.
-#
-# Example (MacOSX):
-# $ ARDUINO_IDE_PATH=/Applications/Arduino.app/Contents/Java ./build-examples.bash
-#
-
-# Path to script directory.
-SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
-LIBNAME="$(basename "$SCRIPTPATH")"
-
-if [[ -z "${ARDUINO_IDE_PATH}" ]]; then
- echo "ARDUINO_IDE_PATH env var is not set"
- exit 1
-fi
-
-# Link BH1750 library into Arduino libraries directory
-ln -s $SCRIPTPATH $ARDUINO_IDE_PATH/libraries/
-
-cd $ARDUINO_IDE_PATH
-
-for sketch in `find $SCRIPTPATH/examples -name '*.ino'`
-do
- echo "Compiling $sketch"
- ./arduino-builder -hardware ./hardware -tools ./hardware/tools/avr -tools ./tools-builder -libraries ./libraries -fqbn arduino:avr:uno --prefs "compiler.warning_level=all" $sketch
- # ./arduino-builder -hardware ./hardware -tools ./hardware/tools/avr -tools ./tools-builder -libraries ./libraries -fqbn esp8266:esp8266:generic --prefs "compiler.warning_level=all" $sketch
- if [ $? -ne 0 ]; then
- echo -e "\xe2\x9c\x96" # check icon
- else
- echo -e "\xe2\x9c\x93" # tick icon
- fi
-done
-
-# Unlink BH1750 library from Arduino libraries directory
-unlink $ARDUINO_IDE_PATH/libraries/$LIBNAME
diff --git a/ci/code-format.bash b/ci/code-format.bash
new file mode 100755
index 0000000..2d07a27
--- /dev/null
+++ b/ci/code-format.bash
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# Exit immediately if a command exits with a non-zero status.
+# set -e
+
+# Define colors
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+YELLOW='\033[1;33m'
+NORMAL='\033[0m'
+
+apply=0
+action="Checking"
+if [ $# -eq 1 ]; then
+ if [ "$1" == "apply" ]; then
+ apply=1
+ action="Applying"
+ else
+ echo "'apply' is the only argument supported"
+ exit 1
+ fi
+fi
+
+files=$(find . -type f -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" -or -name "*.ino")
+
+# Check clang-format output
+for f in $files ; do
+ if [ -f "$f" ]; then
+ echo "${action} code format for ${f}"
+ if [ $apply -eq 1 ]; then
+ clang-format -i $f
+ else
+ diff $f <(clang-format --assume-filename=main.cpp $f) --minimal
+ fi
+ if [ $? -ne 0 ]; then
+ echo -e "${RED}\xe2\x9c\x96${NORMAL}"; # Cross
+ exit 1
+ else
+ echo -e "${GREEN}\xe2\x9c\x93${NORMAL}"; # Tick
+ fi
+ fi
+done
diff --git a/ci/compile-examples.bash b/ci/compile-examples.bash
new file mode 100755
index 0000000..d17a3c7
--- /dev/null
+++ b/ci/compile-examples.bash
@@ -0,0 +1,30 @@
+#!/bin/bash
+#
+# A convenience script to automate building BH1750 examples.
+#
+
+# AVR
+echo "Compiling examples using FQBN arduino:avr:uno"
+arduino-cli compile --fqbn arduino:avr:uno ./examples/BH1750test/BH1750test.ino
+arduino-cli compile --fqbn arduino:avr:uno ./examples/BH1750advanced/BH1750advanced.ino
+arduino-cli compile --fqbn arduino:avr:uno ./examples/BH1750onetime/BH1750onetime.ino
+arduino-cli compile --fqbn arduino:avr:uno ./examples/BH1750autoadjust/BH1750autoadjust.ino
+
+# ESP8266
+echo "Compiling examples using FQBN esp8266:esp8266:generic"
+arduino-cli core update-index --additional-urls http://arduino.esp8266.com/stable/package_esp8266com_index.json
+arduino-cli core install esp8266:esp8266 --additional-urls http://arduino.esp8266.com/stable/package_esp8266com_index.json
+arduino-cli compile --fqbn esp8266:esp8266:generic ./examples/BH1750test/BH1750test.ino
+arduino-cli compile --fqbn esp8266:esp8266:generic ./examples/BH1750advanced/BH1750advanced.ino
+arduino-cli compile --fqbn esp8266:esp8266:generic ./examples/BH1750onetime/BH1750onetime.ino
+arduino-cli compile --fqbn esp8266:esp8266:generic ./examples/BH1750autoadjust/BH1750autoadjust.ino
+
+# ESP32
+echo "Compiling examples using FQBN esp32:esp32:esp32"
+arduino-cli core update-index --additional-urls https://dl.espressif.com/dl/package_esp32_index.json
+arduino-cli core install esp32:esp32 --additional-urls https://dl.espressif.com/dl/package_esp32_index.json
+arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750test/BH1750test.ino
+arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750advanced/BH1750advanced.ino
+arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750onetime/BH1750onetime.ino
+arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750autoadjust/BH1750autoadjust.ino
+arduino-cli compile --fqbn esp32:esp32:esp32 ./examples/BH1750two_i2c/BH1750two_i2c.ino
diff --git a/examples/BH1750advanced/BH1750advanced.ino b/examples/BH1750advanced/BH1750advanced.ino
index d03f051..bb0a440 100644
--- a/examples/BH1750advanced/BH1750advanced.ino
+++ b/examples/BH1750advanced/BH1750advanced.ino
@@ -1,26 +1,28 @@
/*
- Advanced BH1750 library usage example
+Advanced BH1750 library usage example
- This example has some comments about advanced usage features.
+This example has some comments about advanced usage features.
- Connection:
+Connections
- VCC -> 3V3 or 5V
- GND -> GND
- SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
- SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
- ADD -> (not connected) or GND
+ - VCC to 3V3 or 5V
+ - GND to GND
+ - SCL to SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on
+ esp8266 free selectable)
+ - SDA to SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on
+ esp8266 free selectable)
+ - ADD to (not connected) or GND
- ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
- 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
- 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
- be 0x23 (by default).
+ADD pin is used to set sensor I2C address. If it has voltage greater or equal
+to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
+0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address
+will be 0x23 (by default).
*/
-#include
#include
+#include
/*
BH1750 can be physically configured to use two I2C addresses:
@@ -33,7 +35,7 @@
*/
BH1750 lightMeter(0x23);
-void setup(){
+void setup() {
Serial.begin(9600);
@@ -44,9 +46,9 @@ void setup(){
/*
BH1750 has six different measurement modes. They are divided in two groups;
- continuous and one-time measurements. In continuous mode, sensor continuously
- measures lightness value. In one-time mode the sensor makes only one
- measurement and then goes into Power Down mode.
+ continuous and one-time measurements. In continuous mode, sensor
+ continuously measures lightness value. In one-time mode the sensor makes
+ only one measurement and then goes into Power Down mode.
Each mode, has three different precisions:
@@ -76,14 +78,11 @@ void setup(){
// begin returns a boolean that can be used to detect setup problems.
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("BH1750 Advanced begin"));
- }
- else {
+ } else {
Serial.println(F("Error initialising BH1750"));
}
-
}
-
void loop() {
if (lightMeter.measurementReady()) {
float lux = lightMeter.readLightLevel();
@@ -91,5 +90,4 @@ void loop() {
Serial.print(lux);
Serial.println(" lx");
}
-
}
diff --git a/examples/BH1750autoadjust/BH1750autoadjust.ino b/examples/BH1750autoadjust/BH1750autoadjust.ino
index 8ac7e71..ea57988 100644
--- a/examples/BH1750autoadjust/BH1750autoadjust.ino
+++ b/examples/BH1750autoadjust/BH1750autoadjust.ino
@@ -1,38 +1,40 @@
/*
- Example of BH1750 library usage.
+Example of BH1750 library usage.
- This example initialises the BH1750 object using the default high resolution
- one shot mode and then makes a light level reading every five seconds.
+This example initialises the BH1750 object using the default high resolution
+one shot mode and then makes a light level reading every five seconds.
- After the measurement the MTreg value is changed according to the result:
- lux > 40000 ==> MTreg = 32
- lux < 40000 ==> MTreg = 69 (default)
- lux < 10 ==> MTreg = 138
- Remember to test your specific sensor! Maybe the MTreg value range from 32
- up to 254 is not applicable to your unit.
+After the measurement the MTreg value is changed according to the result:
+lux > 40000 ==> MTreg = 32
+lux < 40000 ==> MTreg = 69 (default)
+lux < 10 ==> MTreg = 138
+Remember to test your specific sensor! Maybe the MTreg value range from 32
+up to 254 is not applicable to your unit.
- Connection:
+Connections
- VCC -> 3V3 or 5V
- GND -> GND
- SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
- SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
- ADD -> (not connected) or GND
+ - VCC to 3V3 or 5V
+ - GND to GND
+ - SCL to SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on
+ esp8266 free selectable)
+ - SDA to SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on
+ esp8266 free selectable)
+ - ADD to (not connected) or GND
- ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
- 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
- 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
- be 0x23 (by default).
+ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
+0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
+0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
+be 0x23 (by default).
*/
-#include
#include
+#include
BH1750 lightMeter;
-void setup(){
+void setup() {
Serial.begin(9600);
@@ -41,15 +43,14 @@ void setup(){
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE);
- //lightMeter.setMTreg(69); // not needed, only mentioning it
+ // lightMeter.setMTreg(69); // not needed, only mentioning it
Serial.println(F("BH1750 Test begin"));
-
}
void loop() {
- //we use here the maxWait option due fail save
- if (lightMeter.measurementReady(true)) {
+ // we use here the maxWait option due fail save
+ if (lightMeter.measurementReady(true)) {
float lux = lightMeter.readLightLevel();
Serial.print(F("Light: "));
Serial.print(lux);
@@ -57,40 +58,39 @@ void loop() {
if (lux < 0) {
Serial.println(F("Error condition detected"));
- }
- else {
+ } else {
if (lux > 40000.0) {
// reduce measurement time - needed in direct sun light
if (lightMeter.setMTreg(32)) {
- Serial.println(F("Setting MTReg to low value for high light environment"));
+ Serial.println(
+ F("Setting MTReg to low value for high light environment"));
+ } else {
+ Serial.println(
+ F("Error setting MTReg to low value for high light environment"));
}
- else {
- Serial.println(F("Error setting MTReg to low value for high light environment"));
- }
- }
- else {
- if (lux > 10.0) {
- // typical light environment
- if (lightMeter.setMTreg(69)) {
- Serial.println(F("Setting MTReg to default value for normal light environment"));
- }
- else {
- Serial.println(F("Error setting MTReg to default value for normal light environment"));
+ } else {
+ if (lux > 10.0) {
+ // typical light environment
+ if (lightMeter.setMTreg(69)) {
+ Serial.println(F(
+ "Setting MTReg to default value for normal light environment"));
+ } else {
+ Serial.println(F("Error setting MTReg to default value for normal "
+ "light environment"));
+ }
+ } else {
+ if (lux <= 10.0) {
+ // very low light environment
+ if (lightMeter.setMTreg(138)) {
+ Serial.println(
+ F("Setting MTReg to high value for low light environment"));
+ } else {
+ Serial.println(F("Error setting MTReg to high value for low "
+ "light environment"));
}
}
- else {
- if (lux <= 10.0) {
- //very low light environment
- if (lightMeter.setMTreg(138)) {
- Serial.println(F("Setting MTReg to high value for low light environment"));
- }
- else {
- Serial.println(F("Error setting MTReg to high value for low light environment"));
- }
- }
}
}
-
}
Serial.println(F("--------------------------------------"));
}
diff --git a/examples/BH1750onetime/BH1750onetime.ino b/examples/BH1750onetime/BH1750onetime.ino
index c22ff9a..ba7b71f 100644
--- a/examples/BH1750onetime/BH1750onetime.ino
+++ b/examples/BH1750onetime/BH1750onetime.ino
@@ -1,22 +1,22 @@
/*
- Example of BH1750 library usage.
+Example of BH1750 library usage.
- This example initialises the BH1750 object using the high resolution
- one-time mode and then makes a light level reading every second.
+This example initialises the BH1750 object using the high resolution
+one-time mode and then makes a light level reading every second.
- The BH1750 component starts up in default mode when it next powers up.
- The BH1750 library automatically reconfigures the one-time mode in
- preparation for the next measurement.
+The BH1750 component starts up in default mode when it next powers up.
+The BH1750 library automatically reconfigures the one-time mode in
+preparation for the next measurement.
*/
-#include
#include
+#include
BH1750 lightMeter;
-void setup(){
+void setup() {
Serial.begin(9600);
@@ -27,10 +27,8 @@ void setup(){
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE);
Serial.println(F("BH1750 One-Time Test"));
-
}
-
void loop() {
while (!lightMeter.measurementReady(true)) {
diff --git a/examples/BH1750test/BH1750test.ino b/examples/BH1750test/BH1750test.ino
index 4ca5c10..63cd611 100644
--- a/examples/BH1750test/BH1750test.ino
+++ b/examples/BH1750test/BH1750test.ino
@@ -1,54 +1,50 @@
/*
- Example of BH1750 library usage.
+Example of BH1750 library usage.
- This example initialises the BH1750 object using the default high resolution
- continuous mode and then makes a light level reading every second.
+This example initialises the BH1750 object using the default high resolution
+continuous mode and then makes a light level reading every second.
- Connection:
+Connections
- VCC -> 3V3 or 5V
- GND -> GND
- SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
- SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
- ADD -> (not connected) or GND
+ - VCC to 3V3 or 5V
+ - GND to GND
+ - SCL to SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on
+ esp8266 free selectable)
+ - SDA to SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on
+ esp8266 free selectable)
+ - ADD to (not connected) or GND
- ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
- 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
- 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
- be 0x23 (by default).
+ADD pin is used to set sensor I2C address. If it has voltage greater or equal
+to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
+0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address
+will be 0x23 (by default).
*/
-
-#include
#include
+#include
BH1750 lightMeter;
-
-void setup(){
-
+void setup() {
Serial.begin(9600);
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
Wire.begin();
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
- // For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use Wire.begin(D2, D1);
+ // For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use
+ // Wire.begin(D2, D1);
lightMeter.begin();
Serial.println(F("BH1750 Test begin"));
-
}
-
void loop() {
-
float lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
delay(1000);
-
}
diff --git a/examples/BH1750two_i2c/BH1750two_i2c.ino b/examples/BH1750two_i2c/BH1750two_i2c.ino
index 49bcd8e..6a17fb9 100644
--- a/examples/BH1750two_i2c/BH1750two_i2c.ino
+++ b/examples/BH1750two_i2c/BH1750two_i2c.ino
@@ -1,31 +1,31 @@
/*
- Example of BH1750 library usage.
+Example of BH1750 library usage.
- This example initialises two BH1750 objects using different TwoWire
- instances (Wire and Wire1) and then makes a light level reading every second.
- This is the case for boards such as the ESP8266 and ESP32
+This example initialises two BH1750 objects using different TwoWire
+instances (Wire and Wire1) and then makes a light level reading every second.
+This is the case for boards such as the ESP8266 and ESP32
- Connection:
+Connections:
- BH1750 A:
- VCC -> 3V3 or 5V
- GND -> GND
- SCL -> SCL (19 in this example)
- SDA -> SDA (18 in this example)
- ADD -> (not connected) or GND
+ BH1750 A:
+ VCC -> 3V3 or 5V
+ GND -> GND
+ SCL -> SCL (19 in this example)
+ SDA -> SDA (18 in this example)
+ ADD -> (not connected) or GND
- BH1750 B:
- VCC -> 3V3 or 5V
- GND -> GND
- SCL -> SCL (22 in this example)
- SDA -> SDA (21 in this example)
- ADD -> (not connected) or GND
+ BH1750 B:
+ VCC -> 3V3 or 5V
+ GND -> GND
+ SCL -> SCL (22 in this example)
+ SDA -> SDA (21 in this example)
+ ADD -> (not connected) or GND
- ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
- 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
- 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
- be 0x23 (by default).
+ADD pin is used to set sensor I2C address. If it has voltage greater or equal
+to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
+0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address
+will be 0x23 (by default).
*/
@@ -50,9 +50,13 @@ int error_counter_2_b = 0;
void loop() {
float light_level_a;
- if (bh1750_a.measurementReady()) { light_level_a = bh1750_a.readLightLevel(); }
- float light_level_b;
- if (bh1750_b.measurementReady()) { light_level_b = bh1750_b.readLightLevel(); }
+ if (bh1750_a.measurementReady()) {
+ light_level_a = bh1750_a.readLightLevel();
+ }
+ float light_level_b;
+ if (bh1750_b.measurementReady()) {
+ light_level_b = bh1750_b.readLightLevel();
+ }
if (lround(light_level_a) == -1) {
error_counter_1_a++;
diff --git a/src/BH1750.cpp b/src/BH1750.cpp
index f7916e6..1b3a05b 100644
--- a/src/BH1750.cpp
+++ b/src/BH1750.cpp
@@ -15,35 +15,32 @@
// Define milliseconds delay for ESP8266 platform
#if defined(ESP8266)
- #include
- #define _delay_ms(ms) delayMicroseconds((ms) * 1000)
+# include
+# define _delay_ms(ms) delayMicroseconds((ms)*1000)
// Use _delay_ms from utils for AVR-based platforms
#elif defined(__avr__)
- #include
+# include
// Use Wiring's delay for compability with another platforms
#else
- #define _delay_ms(ms) delay(ms)
+# define _delay_ms(ms) delay(ms)
#endif
-
// Legacy Wire.write() function fix
#if (ARDUINO >= 100)
- #define __wire_write(d) I2C->write(d)
+# define __wire_write(d) I2C->write(d)
#else
- #define __wire_write(d) I2C->send(d)
+# define __wire_write(d) I2C->send(d)
#endif
-
// Legacy Wire.read() function fix
#if (ARDUINO >= 100)
- #define __wire_read() I2C->read()
+# define __wire_read() I2C->read()
#else
- #define __wire_read() I2C->receive()
+# define __wire_read() I2C->receive()
#endif
-
/**
* Constructor
* @params addr Sensor address (0x76 or 0x72, see datasheet)
@@ -57,21 +54,20 @@ BH1750::BH1750(byte addr) {
I2C = &Wire;
}
-
/**
* Configure sensor
* @param mode Measurement mode
* @param addr Address of the sensor
* @param i2c TwoWire instance connected to I2C bus
*/
-bool BH1750::begin(Mode mode, byte addr, TwoWire *i2c) {
+bool BH1750::begin(Mode mode, byte addr, TwoWire* i2c) {
// I2C is expected to be initialized outside this library
// But, allows a different address and TwoWire instance to be used
- if(i2c) {
+ if (i2c) {
I2C = i2c;
}
- if(addr) {
+ if (addr) {
BH1750_I2CADDR = addr;
}
@@ -79,7 +75,6 @@ bool BH1750::begin(Mode mode, byte addr, TwoWire *i2c) {
return (configure(mode) && setMTreg(BH1750_DEFAULT_MTREG));
}
-
/**
* Configure BH1750 with specified mode
* @param mode Measurement mode
@@ -92,54 +87,52 @@ bool BH1750::configure(Mode mode) {
// Check measurement mode is valid
switch (mode) {
- case BH1750::CONTINUOUS_HIGH_RES_MODE:
- case BH1750::CONTINUOUS_HIGH_RES_MODE_2:
- case BH1750::CONTINUOUS_LOW_RES_MODE:
- case BH1750::ONE_TIME_HIGH_RES_MODE:
- case BH1750::ONE_TIME_HIGH_RES_MODE_2:
- case BH1750::ONE_TIME_LOW_RES_MODE:
+ case BH1750::CONTINUOUS_HIGH_RES_MODE:
+ case BH1750::CONTINUOUS_HIGH_RES_MODE_2:
+ case BH1750::CONTINUOUS_LOW_RES_MODE:
+ case BH1750::ONE_TIME_HIGH_RES_MODE:
+ case BH1750::ONE_TIME_HIGH_RES_MODE_2:
+ case BH1750::ONE_TIME_LOW_RES_MODE:
- // Send mode to sensor
- I2C->beginTransmission(BH1750_I2CADDR);
- __wire_write((uint8_t)mode);
- ack = I2C->endTransmission();
+ // Send mode to sensor
+ I2C->beginTransmission(BH1750_I2CADDR);
+ __wire_write((uint8_t)mode);
+ ack = I2C->endTransmission();
- // Wait a few moments to wake up
- _delay_ms(10);
- break;
-
- default:
- // Invalid measurement mode
- Serial.println(F("[BH1750] ERROR: Invalid mode"));
- break;
+ // Wait a few moments to wake up
+ _delay_ms(10);
+ break;
+ default:
+ // Invalid measurement mode
+ Serial.println(F("[BH1750] ERROR: Invalid mode"));
+ break;
}
// Check result code
switch (ack) {
- case 0:
- BH1750_MODE = mode;
- lastReadTimestamp = millis();
- return true;
- case 1: // too long for transmit buffer
- Serial.println(F("[BH1750] ERROR: too long for transmit buffer"));
- break;
- case 2: // received NACK on transmit of address
- Serial.println(F("[BH1750] ERROR: received NACK on transmit of address"));
- break;
- case 3: // received NACK on transmit of data
- Serial.println(F("[BH1750] ERROR: received NACK on transmit of data"));
- break;
- case 4: // other error
- Serial.println(F("[BH1750] ERROR: other error"));
- break;
- default:
- Serial.println(F("[BH1750] ERROR: undefined error"));
- break;
+ case 0:
+ BH1750_MODE = mode;
+ lastReadTimestamp = millis();
+ return true;
+ case 1: // too long for transmit buffer
+ Serial.println(F("[BH1750] ERROR: too long for transmit buffer"));
+ break;
+ case 2: // received NACK on transmit of address
+ Serial.println(F("[BH1750] ERROR: received NACK on transmit of address"));
+ break;
+ case 3: // received NACK on transmit of data
+ Serial.println(F("[BH1750] ERROR: received NACK on transmit of data"));
+ break;
+ case 4: // other error
+ Serial.println(F("[BH1750] ERROR: other error"));
+ break;
+ default:
+ Serial.println(F("[BH1750] ERROR: undefined error"));
+ break;
}
return false;
-
}
/**
@@ -150,7 +143,7 @@ bool BH1750::configure(Mode mode) {
* false if MTreg not changed or parameter out of range
*/
bool BH1750::setMTreg(byte MTreg) {
- //Bug: lowest value seems to be 32!
+ // Bug: lowest value seems to be 32!
if (MTreg <= 31 || MTreg > 254) {
Serial.println(F("[BH1750] ERROR: MTreg out of range"));
return false;
@@ -163,7 +156,7 @@ bool BH1750::setMTreg(byte MTreg) {
__wire_write((0b01000 << 3) | (MTreg >> 5));
ack = I2C->endTransmission();
I2C->beginTransmission(BH1750_I2CADDR);
- __wire_write((0b011 << 5 ) | (MTreg & 0b11111));
+ __wire_write((0b011 << 5) | (MTreg & 0b11111));
ack = ack | I2C->endTransmission();
I2C->beginTransmission(BH1750_I2CADDR);
__wire_write(BH1750_MODE);
@@ -174,24 +167,24 @@ bool BH1750::setMTreg(byte MTreg) {
// Check result code
switch (ack) {
- case 0:
- BH1750_MTreg = MTreg;
- return true;
- case 1: // too long for transmit buffer
- Serial.println(F("[BH1750] ERROR: too long for transmit buffer"));
- break;
- case 2: // received NACK on transmit of address
- Serial.println(F("[BH1750] ERROR: received NACK on transmit of address"));
- break;
- case 3: // received NACK on transmit of data
- Serial.println(F("[BH1750] ERROR: received NACK on transmit of data"));
- break;
- case 4: // other error
- Serial.println(F("[BH1750] ERROR: other error"));
- break;
- default:
- Serial.println(F("[BH1750] ERROR: undefined error"));
- break;
+ case 0:
+ BH1750_MTreg = MTreg;
+ return true;
+ case 1: // too long for transmit buffer
+ Serial.println(F("[BH1750] ERROR: too long for transmit buffer"));
+ break;
+ case 2: // received NACK on transmit of address
+ Serial.println(F("[BH1750] ERROR: received NACK on transmit of address"));
+ break;
+ case 3: // received NACK on transmit of data
+ Serial.println(F("[BH1750] ERROR: received NACK on transmit of data"));
+ break;
+ case 4: // other error
+ Serial.println(F("[BH1750] ERROR: other error"));
+ break;
+ default:
+ Serial.println(F("[BH1750] ERROR: undefined error"));
+ break;
}
return false;
@@ -201,24 +194,26 @@ bool BH1750::setMTreg(byte MTreg) {
* Checks whether enough time has gone to read a new value
* @param maxWait a boolean if to wait for typical or maximum delay
* @return a boolean if a new measurement is possible
- *
+ *
*/
bool BH1750::measurementReady(bool maxWait) {
unsigned long delaytime = 0;
switch (BH1750_MODE) {
- case BH1750::CONTINUOUS_HIGH_RES_MODE:
- case BH1750::CONTINUOUS_HIGH_RES_MODE_2:
- case BH1750::ONE_TIME_HIGH_RES_MODE:
- case BH1750::ONE_TIME_HIGH_RES_MODE_2:
- maxWait ? delaytime = (180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : delaytime = (120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG);
- break;
- case BH1750::CONTINUOUS_LOW_RES_MODE:
- case BH1750::ONE_TIME_LOW_RES_MODE:
- // Send mode to sensor
- maxWait ? delaytime = (24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : delaytime = (16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG);
- break;
- default:
- break;
+ case BH1750::CONTINUOUS_HIGH_RES_MODE:
+ case BH1750::CONTINUOUS_HIGH_RES_MODE_2:
+ case BH1750::ONE_TIME_HIGH_RES_MODE:
+ case BH1750::ONE_TIME_HIGH_RES_MODE_2:
+ maxWait ? delaytime = (180 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG)
+ : delaytime = (120 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG);
+ break;
+ case BH1750::CONTINUOUS_LOW_RES_MODE:
+ case BH1750::ONE_TIME_LOW_RES_MODE:
+ // Send mode to sensor
+ maxWait ? delaytime = (24 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG)
+ : delaytime = (16 * BH1750_MTreg / (byte)BH1750_DEFAULT_MTREG);
+ break;
+ default:
+ break;
}
// Wait for new measurement to be possible.
// Measurements have a maximum measurement time and a typical measurement
@@ -228,11 +223,10 @@ bool BH1750::measurementReady(bool maxWait) {
// the maximum measurement time will be used. See data sheet pages 2, 5
// and 7 for more details.
unsigned long currentTimestamp = millis();
- if (currentTimestamp - lastReadTimestamp >= delaytime) {
- return true;
- }
- else
- return false;
+ if (currentTimestamp - lastReadTimestamp >= delaytime) {
+ return true;
+ } else
+ return false;
}
/**
@@ -265,33 +259,34 @@ float BH1750::readLightLevel() {
lastReadTimestamp = millis();
if (level != -1.0) {
- // Print raw value if debug enabled
- #ifdef BH1750_DEBUG
+// Print raw value if debug enabled
+#ifdef BH1750_DEBUG
Serial.print(F("[BH1750] Raw value: "));
Serial.println(level);
- #endif
+#endif
if (BH1750_MTreg != BH1750_DEFAULT_MTREG) {
- level *= (float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg);
- // Print MTreg factor if debug enabled
- #ifdef BH1750_DEBUG
+ level *= (float)((byte)BH1750_DEFAULT_MTREG / (float)BH1750_MTreg);
+// Print MTreg factor if debug enabled
+#ifdef BH1750_DEBUG
Serial.print(F("[BH1750] MTreg factor: "));
- Serial.println( String((float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg)) );
- #endif
+ Serial.println(
+ String((float)((byte)BH1750_DEFAULT_MTREG / (float)BH1750_MTreg)));
+#endif
}
- if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) {
+ if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 ||
+ BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) {
level /= 2;
}
// Convert raw value to lux
level /= BH1750_CONV_FACTOR;
- // Print converted value if debug enabled
- #ifdef BH1750_DEBUG
+// Print converted value if debug enabled
+#ifdef BH1750_DEBUG
Serial.print(F("[BH1750] Converted float value: "));
Serial.println(level);
- #endif
+#endif
}
return level;
-
}
diff --git a/src/BH1750.h b/src/BH1750.h
index dd4575c..688f3b8 100644
--- a/src/BH1750.h
+++ b/src/BH1750.h
@@ -6,7 +6,8 @@
interface to the device. Configuring the I2C bus is expected to be done
in user code. The BH1750 library doesn't do this automatically.
- Datasheet: http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf
+ Datasheet:
+ http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf
Written by Christopher Laws, March, 2013.
@@ -16,9 +17,9 @@
#define BH1750_h
#if (ARDUINO >= 100)
- #include
+# include
#else
- #include
+# include
#endif
#include "Wire.h"
@@ -40,44 +41,42 @@
class BH1750 {
- public:
+public:
+ enum Mode {
+ // same as Power Down
+ UNCONFIGURED = 0,
+ // Measurement at 1 lux resolution. Measurement time is approx 120ms.
+ CONTINUOUS_HIGH_RES_MODE = 0x10,
+ // Measurement at 0.5 lux resolution. Measurement time is approx 120ms.
+ CONTINUOUS_HIGH_RES_MODE_2 = 0x11,
+ // Measurement at 4 lux resolution. Measurement time is approx 16ms.
+ CONTINUOUS_LOW_RES_MODE = 0x13,
+ // Measurement at 1 lux resolution. Measurement time is approx 120ms.
+ ONE_TIME_HIGH_RES_MODE = 0x20,
+ // Measurement at 0.5 lux resolution. Measurement time is approx 120ms.
+ ONE_TIME_HIGH_RES_MODE_2 = 0x21,
+ // Measurement at 4 lux resolution. Measurement time is approx 16ms.
+ ONE_TIME_LOW_RES_MODE = 0x23
+ };
- enum Mode
- {
- // same as Power Down
- UNCONFIGURED = 0,
- // Measurement at 1 lux resolution. Measurement time is approx 120ms.
- CONTINUOUS_HIGH_RES_MODE = 0x10,
- // Measurement at 0.5 lux resolution. Measurement time is approx 120ms.
- CONTINUOUS_HIGH_RES_MODE_2 = 0x11,
- // Measurement at 4 lux resolution. Measurement time is approx 16ms.
- CONTINUOUS_LOW_RES_MODE = 0x13,
- // Measurement at 1 lux resolution. Measurement time is approx 120ms.
- ONE_TIME_HIGH_RES_MODE = 0x20,
- // Measurement at 0.5 lux resolution. Measurement time is approx 120ms.
- ONE_TIME_HIGH_RES_MODE_2 = 0x21,
- // Measurement at 4 lux resolution. Measurement time is approx 16ms.
- ONE_TIME_LOW_RES_MODE = 0x23
- };
+ BH1750(byte addr = 0x23);
+ bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE, byte addr = 0x23,
+ TwoWire* i2c = nullptr);
+ bool configure(Mode mode);
+ bool setMTreg(byte MTreg);
+ bool measurementReady(bool maxWait = false);
+ float readLightLevel();
- BH1750(byte addr = 0x23);
- bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE, byte addr = 0x23,
- TwoWire* i2c = nullptr);
- bool configure(Mode mode);
- bool setMTreg(byte MTreg);
- bool measurementReady(bool maxWait = false);
- float readLightLevel();
-
- private:
- byte BH1750_I2CADDR;
- byte BH1750_MTreg = (byte)BH1750_DEFAULT_MTREG;
- // Correction factor used to calculate lux. Typical value is 1.2 but can
- // range from 0.96 to 1.44. See the data sheet (p.2, Measurement Accuracy)
- // for more information.
- const float BH1750_CONV_FACTOR = 1.2;
- Mode BH1750_MODE = UNCONFIGURED;
- TwoWire* I2C;
- unsigned long lastReadTimestamp;
+private:
+ byte BH1750_I2CADDR;
+ byte BH1750_MTreg = (byte)BH1750_DEFAULT_MTREG;
+ // Correction factor used to calculate lux. Typical value is 1.2 but can
+ // range from 0.96 to 1.44. See the data sheet (p.2, Measurement Accuracy)
+ // for more information.
+ const float BH1750_CONV_FACTOR = 1.2;
+ Mode BH1750_MODE = UNCONFIGURED;
+ TwoWire* I2C;
+ unsigned long lastReadTimestamp;
};
#endif