From 1cb1143c94ad262cf684634080bc108d2df1f477 Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Sun, 2 Mar 2014 18:07:40 +0100 Subject: [PATCH] Added example for BH1750 lux sensor --- libraries/BH1750/BH1750.cpp | 97 +++++++++++++++++++ libraries/BH1750/BH1750.h | 72 ++++++++++++++ libraries/BH1750/README.md | 58 +++++++++++ .../BH1750/examples/BH1750test/BH1750test.pde | 37 +++++++ libraries/BH1750/keywords.txt | 27 ++++++ .../LightLuxSensor/LightLuxSensor.ino | 71 ++++++++++++++ 6 files changed, 362 insertions(+) create mode 100644 libraries/BH1750/BH1750.cpp create mode 100644 libraries/BH1750/BH1750.h create mode 100644 libraries/BH1750/README.md create mode 100644 libraries/BH1750/examples/BH1750test/BH1750test.pde create mode 100644 libraries/BH1750/keywords.txt create mode 100644 libraries/MySensors/examples/LightLuxSensor/LightLuxSensor.ino diff --git a/libraries/BH1750/BH1750.cpp b/libraries/BH1750/BH1750.cpp new file mode 100644 index 0000000..b9ff09c --- /dev/null +++ b/libraries/BH1750/BH1750.cpp @@ -0,0 +1,97 @@ +/* + +This is a library for the BH1750FVI Digital Light Sensor +breakout board. + +The board uses I2C for communication. 2 pins are required to +interface to the device. + + +Written by Christopher Laws, March, 2013. + +*/ + +#include "BH1750.h" +#include + + +BH1750::BH1750() { +} + +void BH1750::begin(uint8_t mode) { + + Wire.begin(); + //write8(mode); + configure(mode); +} + + +void BH1750::configure(uint8_t mode) { + + 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: + // apply a valid mode change + write8(mode); + _delay_ms(10); + break; + default: + // Invalid measurement mode + #if BH1750_DEBUG == 1 + Serial.println("Invalid measurement mode"); + #endif + break; + } +} + + +uint16_t BH1750::readLightLevel(void) { + + uint16_t level; + + Wire.beginTransmission(BH1750_I2CADDR); + Wire.requestFrom(BH1750_I2CADDR, 2); +#if (ARDUINO >= 100) + level = Wire.read(); + level <<= 8; + level |= Wire.read(); +#else + level = Wire.receive(); + level <<= 8; + level |= Wire.receive(); +#endif + Wire.endTransmission(); + +#if BH1750_DEBUG == 1 + Serial.print("Raw light level: "); + Serial.println(level); +#endif + + level = level/1.2; // convert to lux + +#if BH1750_DEBUG == 1 + Serial.print("Light level: "); + Serial.println(level); +#endif + return level; +} + + + +/*********************************************************************/ + + +void BH1750::write8(uint8_t d) { + Wire.beginTransmission(BH1750_I2CADDR); +#if (ARDUINO >= 100) + Wire.write(d); +#else + Wire.send(d); +#endif + Wire.endTransmission(); +} + diff --git a/libraries/BH1750/BH1750.h b/libraries/BH1750/BH1750.h new file mode 100644 index 0000000..176db0b --- /dev/null +++ b/libraries/BH1750/BH1750.h @@ -0,0 +1,72 @@ +/* + +This is a library for the BH1750FVI Digital Light Sensor +breakout board. + +The board uses I2C for communication. 2 pins are required to +interface to the device. + +Datasheet: +http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf + +Written by Christopher Laws, March, 2013. + +*/ + +#ifndef BH1750_h +#define BH1750_h + +#if (ARDUINO >= 100) +#include +#else +#include +#endif +#include "Wire.h" + +#define BH1750_DEBUG 0 + +#define BH1750_I2CADDR 0x23 + +// No active state +#define BH1750_POWER_DOWN 0x00 + +// Wating for measurment command +#define BH1750_POWER_ON 0x01 + +// Reset data register value - not accepted in POWER_DOWN mode +#define BH1750_RESET 0x07 + +// Start measurement at 1lx resolution. Measurement time is approx 120ms. +#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 + +// Start measurement at 0.5lx resolution. Measurement time is approx 120ms. +#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 + +// Start measurement at 4lx resolution. Measurement time is approx 16ms. +#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 + +// Start measurement at 1lx resolution. Measurement time is approx 120ms. +// Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_HIGH_RES_MODE 0x20 + +// Start measurement at 0.5lx resolution. Measurement time is approx 120ms. +// Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21 + +// Start measurement at 1lx resolution. Measurement time is approx 120ms. +// Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_LOW_RES_MODE 0x23 + +class BH1750 { + public: + BH1750(); + void begin(uint8_t mode = BH1750_CONTINUOUS_HIGH_RES_MODE); + void configure(uint8_t mode); + uint16_t readLightLevel(void); + + private: + void write8(uint8_t data); + +}; + +#endif \ No newline at end of file diff --git a/libraries/BH1750/README.md b/libraries/BH1750/README.md new file mode 100644 index 0000000..6995742 --- /dev/null +++ b/libraries/BH1750/README.md @@ -0,0 +1,58 @@ +BH1750 +====== + +An Arduino library for digital light sensor breakout boards containing the BH1750FVI IC. + +The board uses I2C for communication. + +2 pins are required to interface to the device. + +Datasheet for the light sensor: +http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf + + +To download, click the ZIP button above. Extract the zip file and rename the uncompressed folder to BH1750. Check that it contains BH1750.cpp and BH1750.h, keywords.txt. + +Place the BH1750 library folder your <arduinosketchfolder>/libraries/ folder. Restart the IDE. + + +Example usage: +```c +/* + +Example of BH1750 library usage. + +This example initalises the BH1750 object using the default +high resolution mode and then makes a light level reading every second. + +Connection: + VCC-5v + GND-GND + SCL-SCL(analog pin 5) + SDA-SDA(analog pin 4) + ADD-NC or GND + +*/ + +#include +#include + + +BH1750 lightMeter; + + +void setup(){ + Serial.begin(9600); + lightMeter.begin(); + Serial.println("Running..."); +} + + +void loop() { + uint16_t lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + delay(1000); +} +``` diff --git a/libraries/BH1750/examples/BH1750test/BH1750test.pde b/libraries/BH1750/examples/BH1750test/BH1750test.pde new file mode 100644 index 0000000..2b601be --- /dev/null +++ b/libraries/BH1750/examples/BH1750test/BH1750test.pde @@ -0,0 +1,37 @@ +/* + +Example of BH1750 library usage. + +This example initalises the BH1750 object using the default +high resolution mode and then makes a light level reading every second. + +Connection: + VCC-5v + GND-GND + SCL-SCL(analog pin 5) + SDA-SDA(analog pin 4) + ADD-NC or GND + +*/ + +#include +#include + + +BH1750 lightMeter; + + +void setup(){ + Serial.begin(9600); + lightMeter.begin(); + Serial.println("Running..."); +} + + +void loop() { + uint16_t lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + delay(1000); +} \ No newline at end of file diff --git a/libraries/BH1750/keywords.txt b/libraries/BH1750/keywords.txt new file mode 100644 index 0000000..febeef4 --- /dev/null +++ b/libraries/BH1750/keywords.txt @@ -0,0 +1,27 @@ +####################################### +# Syntax Coloring Map For BH1750 +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +BH1750 KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +configure KEYWORD2 +readLightLevel KEYWORD2 + + +####################################### +# Instances (KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +####################################### \ No newline at end of file diff --git a/libraries/MySensors/examples/LightLuxSensor/LightLuxSensor.ino b/libraries/MySensors/examples/LightLuxSensor/LightLuxSensor.ino new file mode 100644 index 0000000..63ca6ea --- /dev/null +++ b/libraries/MySensors/examples/LightLuxSensor/LightLuxSensor.ino @@ -0,0 +1,71 @@ +/* + Vera Arduino BH1750FVI Light sensor + communicate using I2C Protocol + this library enable 2 slave device addresses + Main address 0x23 + secondary address 0x5C + connect the sensor as follows : + + VCC >>> 5V + Gnd >>> Gnd + ADDR >>> NC or GND + SCL >>> A5 + SDA >>> A4 + + Contribution: idefix + +*/ + + +#include +#include +#include +#include +#include +#include +#include + +// Set RADIO_ID to something unique in your sensor network (1-254) +// or set to AUTO if you want gw to assign a RADIO_ID for you. +#define RADIO_ID AUTO +#define CHILD_ID_LIGHT 0 +#define LIGHT_SENSOR_ANALOG_PIN 0 + +BH1750 lightSensor; + +unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds) + +Sensor gw(9,10); + +int lastlux; +Sleep sleep; + +void setup() +{ + Serial.begin(BAUD_RATE); // Used to type in characters + gw.begin(RADIO_ID); + + // Register all sensors to gateway (they will be created as child devices) + gw.sendSensorPresentation(CHILD_ID_LIGHT, S_LIGHT_LEVEL); + + lightSensor.begin(); +} + +void loop() +{ + gw.powerUp(); // Power up radio + + uint16_t lux = lightSensor.readLightLevel();// Get Lux value + Serial.println(lux); + if (lux != lastlux) { + gw.sendVariable(CHILD_ID_LIGHT, V_LIGHT_LEVEL, lux); + lastlux = lux; + } + + // Power down the radio. Note that the radio will get powered back up + // on the next write() call. + delay(1000); //delay to allow serial to fully print before sleep + gw.powerDown(); + sleep.pwrDownMode(); //set sleep mode + sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime +}