Skip to content

Commit

Permalink
Added support for MAX44009 and BH1750 sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
ogiewon committed Jul 4, 2018
1 parent 92c8d3b commit 54b2316
Show file tree
Hide file tree
Showing 29 changed files with 1,559 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
// - 1 x BMP280 Temperature and Pressure sensor (same I2C address as BEP280)
// - 1 x TCS34725 Color Illuminance sensor
// - 1 x TSL2561 Illuminance sensor
// - 1 x MAX44009 Illuminance sensor
// - 1 x BH1750 Illuminance sensor
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2015-01-03 Dan & Daniel Original Creation
// 2018-07-02 Dan Ogorchock Revised to demonstrate I2C sensors
// 2018-07-04 Dan Ogorchock Added MAX44009 and BH1750 Lux Sensors
//
//******************************************************************************************
//******************************************************************************************
Expand Down Expand Up @@ -57,6 +60,8 @@
#include <PS_AdafruitAM2320_TempHumid.h> //Implements a Polling Sensor (PS) to measure Temperature and humidity using AM2320 via I2C
#include <PS_AdafruitTCS34725_Illum_Color.h> //Implements a Polling Sensor (PS) to measure Color Illuminance using TCS34725 via I2C
#include <PS_AdafruitTSL2561_Illuminance.h> //Implements a Polling Sensor (PS) to measure Illuminance using TSL2561 via I2C
#include <PS_MAX44009_Illuminance.h> //Implements a Polling Sensor (PS) to measure Illuminance using MAX44009 via I2C
#include <PS_BH1750_Illuminance.h> //Implements a Polling Sensor (PS) to measure Illuminance using BH1750 via I2C

//*************************************************************************************************
//NodeMCU v1.0 ESP8266-12e Pin Definitions (makes it much easier as these match the board markings)
Expand Down Expand Up @@ -141,10 +146,12 @@ void setup()

//Polling Sensors (eaxmples of various I2C sensors supported in ST_Anything)
static st::PS_AdafruitBME280_TempHumidPress sensor1(F("BME280_1"), 60, 0, "temperature1", "humidity1", "pressure1", false, 100, 0x77); //both BME280 and BMP280 use address 0x77 - only use one at a time
//static st::PS_AdafruitBMP280_TempPress sensor2(F("BMP280_1"), 60, 10, "temperature2", "pressure2", false, 100, 0x77); //both BME280 and BMP280 use address 0x77 - only use one at a time
// static st::PS_AdafruitBMP280_TempPress sensor2(F("BMP280_1"), 60, 10, "temperature2", "pressure2", false, 100, 0x77); //both BME280 and BMP280 use address 0x77 - only use one at a time
static st::PS_AdafruitAM2320_TempHumid sensor3(F("AM2320_1"), 60, 20, "temperature3", "humidity3", false, 100);
static st::PS_AdafruitTCS34725_Illum_Color sensor4(F("illuminancergb1"), 60, 30, TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_4X);
static st::PS_AdafruitTSL2561_Illuminance sensor5(F("illuminance1"), 60, 40, TSL2561_ADDR_FLOAT, TSL2561_INTEGRATIONTIME_13MS, TSL2561_GAIN_1X);
static st::PS_MAX44009_Illuminance sensor6(F("illuminance2"), 60, 50, MAX44009_A0_LOW);
static st::PS_BH1750_Illuminance sensor7(F("illuminance3"), 60, 55, BH1750_ADDR_LOW);


//Executors
Expand Down Expand Up @@ -181,13 +188,14 @@ void setup()
//*****************************************************************************
//Add each sensor to the "Everything" Class
//*****************************************************************************
st::Everything::addSensor(&sensor1);
// st::Everything::addSensor(&sensor2);
st::Everything::addSensor(&sensor1); //if uncommented, must comment out sensor2 below as they both use same I2C address by default
// st::Everything::addSensor(&sensor2); //if uncommented, must comment out sensor1 above as they both use same I2C address by default
st::Everything::addSensor(&sensor3);
st::Everything::addSensor(&sensor4);
st::Everything::addSensor(&sensor5);


st::Everything::addSensor(&sensor6);
st::Everything::addSensor(&sensor7);

//*****************************************************************************
//Add each executor to the "Everything" Class
//*****************************************************************************
Expand All @@ -210,4 +218,4 @@ void loop()
//Execute the Everything run method which takes care of "Everything"
//*****************************************************************************
st::Everything::run();
}
}
1 change: 1 addition & 0 deletions Arduino/libraries/BH1750/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
27 changes: 27 additions & 0 deletions Arduino/libraries/BH1750/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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
# 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
# esp8266
# - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570test/BH1750test.ino
# - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570advanced/BH1750advanced.ino
207 changes: 207 additions & 0 deletions Arduino/libraries/BH1750/BH1750.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
This is a library for the BH1750FVI Digital Light Sensor breakout board.
The BH1750 board uses I2C for communication. Two pins are required to
interface to the device. Configuring the I2C bus is expected to be done
in user code. The BH1750 library doesn't do this automatically.
Written by Christopher Laws, March, 2013.
Modified by Dan Ogorchock on 2018-07-04 for ST_Anything compatability
*/

#include "BH1750.h"

// Define milliseconds delay for ESP8266 platform
#if defined(ESP8266)

#include <pgmspace.h>
#define _delay_ms(ms) delayMicroseconds((ms) * 1000)

// Use _delay_ms from utils for AVR-based platforms
#elif defined(__avr__)
#include <util/delay.h>

// Use Wiring's delay for compability with another platforms
#else
#define _delay_ms(ms) delay(ms)
#endif


// Legacy Wire.write() function fix
#if (ARDUINO >= 100)
#define __wire_write(d) Wire.write(d)
#else
#define __wire_write(d) Wire.send(d)
#endif


// Legacy Wire.read() function fix
#if (ARDUINO >= 100)
#define __wire_read() Wire.read()
#else
#define __wire_read() Wire.receive()
#endif


/**
* Constructor
* @params addr Sensor address (0x23 or 0x5C, see datasheet)
*
* Default for BH1750 is 0x23
*/
BH1750::BH1750(byte addr) {

BH1750_I2CADDR = addr;

}


/**
* Configure sensor
* @param mode Measurement mode
*/
bool BH1750::begin(Mode mode) {

// I2C is expected to be initialized outside this library
//DGO Revision for ST_Anything compatability - all other I2C sensor initialize I2C.
// This one was the exception, but I have modified it to be like the others.
Wire.begin();
// Configure sensor in specified mode
return configure(mode);

}


/**
* Configure BH1750 with specified mode
* @param mode Measurement mode
*/
bool BH1750::configure(Mode mode) {

// default transmission result to a value out of normal range
byte ack = 5;

// 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:

// Send mode to sensor
Wire.beginTransmission(BH1750_I2CADDR);
__wire_write((uint8_t)BH1750_MODE);
ack = Wire.endTransmission();

// 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;
return true;
case 1: // too long for transmit buffer
Serial.println(F("[BH1750] ERROR: too long for transmit buffer"));
case 2: // received NACK on transmit of address
Serial.println(F("[BH1750] ERROR: received NACK on transmit of address"));
case 3: // received NACK on transmit of data
Serial.println(F("[BH1750] ERROR: received NACK on transmit of data"));
case 4: // other error
Serial.println(F("[BH1750] ERROR: other error"));
default:
Serial.println(F("[BH1750] ERROR: undefined error"));
break;
}

return false;

}


/**
* Read light level from sensor
* @return Light level in lux (0 ~ 65535)
*/
uint16_t BH1750::readLightLevel(bool maxWait) {

if (BH1750_MODE == UNCONFIGURED) {
Serial.println(F("[BH1750] Device is not configured!"));
return 0;
}

// Measurement result will be stored here
uint16_t level=65535;

// Send mode to sensor
Wire.beginTransmission(BH1750_I2CADDR);
__wire_write((uint8_t)BH1750_MODE);
Wire.endTransmission();

// Wait for measurement to be taken.
// Measurements have a maximum measurement time and a typical measurement
// time. The maxWait argument determines which measurement wait time is
// used when a one-time mode is being used. The typical (shorter)
// measurement time is used by default and if maxWait is set to True then
// the maximum measurement time will be used. See data sheet pages 2, 5
// and 7 for more details.
// A continuous mode measurement can be read immediately after re-sending
// the mode command.

switch (BH1750_MODE) {

case BH1750::ONE_TIME_LOW_RES_MODE:
case BH1750::ONE_TIME_HIGH_RES_MODE:
case BH1750::ONE_TIME_HIGH_RES_MODE_2:

if (BH1750_MODE == BH1750::ONE_TIME_LOW_RES_MODE) {
maxWait ? _delay_ms(24) : _delay_ms(16);
}
else {
maxWait ? _delay_ms(180) :_delay_ms(120);
}
break;
default: break;
}

// Read two bytes from the sensor, which are low and high parts of the sensor
// value
Wire.requestFrom(BH1750_I2CADDR, 2);
if (Wire.available() == 2) {
level = __wire_read();
level <<= 8;
level |= __wire_read();
}

// Print raw value if debug enabled
#ifdef BH1750_DEBUG
Serial.print(F("[BH1750] Raw value: "));
Serial.println(level);
#endif

// Convert raw value to lux
level /= 1.2;

// Print converted value if debug enabled
#ifdef BH1750_DEBUG
Serial.print(F("[BH1750] Converted value: "));
Serial.println(level);
#endif

return level;

}
76 changes: 76 additions & 0 deletions Arduino/libraries/BH1750/BH1750.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
This is a library for the BH1750FVI Digital Light Sensor
breakout board.
The BH1750 board uses I2C for communication. Two pins are required to
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://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
Written by Christopher Laws, March, 2013.
Modified by Dan Ogorchock on 2018-07-04 for ST_Anything compatability
*/

#ifndef BH1750_h
#define BH1750_h

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include "Wire.h"

// Uncomment, to enable debug messages
// #define BH1750_DEBUG

// No active state
#define BH1750_POWER_DOWN 0x00

// Wating for measurement command
#define BH1750_POWER_ON 0x01

// Reset data register value - not accepted in POWER_DOWN mode
#define BH1750_RESET 0x07

//I2C Addresses
#define BH1750_ADDR_LOW 0x23
#define BH1750_ADDR_HIGH 0x5C

class BH1750 {

public:

enum Mode
{
UNCONFIGURED = 0,
// Measurement at 1lx resolution. Measurement time is approx 120ms.
CONTINUOUS_HIGH_RES_MODE = 0x10,
// Measurement at 0.5lx resolution. Measurement time is approx 120ms.
CONTINUOUS_HIGH_RES_MODE_2 = 0x11,
// Measurement at 4lx resolution. Measurement time is approx 16ms.
CONTINUOUS_LOW_RES_MODE = 0x13,
// Measurement at 1lx resolution. Measurement time is approx 120ms.
ONE_TIME_HIGH_RES_MODE = 0x20,
// Measurement at 0.5lx resolution. Measurement time is approx 120ms.
ONE_TIME_HIGH_RES_MODE_2 = 0x21,
// Measurement at 1lx resolution. Measurement time is approx 120ms.
ONE_TIME_LOW_RES_MODE = 0x23
};

BH1750(byte addr = BH1750_ADDR_LOW);
bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE);
bool configure(Mode mode);
uint16_t readLightLevel(bool maxWait = false);

private:
int BH1750_I2CADDR;
Mode BH1750_MODE = UNCONFIGURED;

};

#endif
Loading

0 comments on commit 54b2316

Please sign in to comment.