diff --git a/hardware/arduino/zuno/libraries/ZUNO_BH1750/ZUNO_BH1750.cpp b/hardware/arduino/zuno/libraries/ZUNO_BH1750/ZUNO_BH1750.cpp new file mode 100644 index 0000000..fc03f79 --- /dev/null +++ b/hardware/arduino/zuno/libraries/ZUNO_BH1750/ZUNO_BH1750.cpp @@ -0,0 +1,94 @@ +/*************************************************** + This is a library for the BH1750(FVI) light sensor + + This sensor use I2C to communicate, 2 pins are required to + interface + + Datasheet: + http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf + Based on BH1750 lib written by Christopher Laws, March, 2013. + Adapted to ZUNO + ****************************************************/ + +#include "ZUNO_BH1750.h" +#include "Wire.h" + +#define BH1750_DEBUG 0 + +BH1750::BH1750() { + +} + +void BH1750::begin(uint8_t mode, uint8_t addr) { + + Wire.begin(); + // initialization sensor + configure(mode, addr); +} + +void BH1750::configure(uint8_t mode, uint8_t addr) { + + Wire.beginTransmission(addr); + 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 + Wire.write(mode); + delay(10); + break; + default: + // Invalid measurement mode + #if BH1750_DEBUG == 1 + Serial.println("Invalid measurement mode"); + #endif + break; + } + Wire.endTransmission(); +} + +void BH1750::powerOn(uint8_t addr) { + Wire.beginTransmission(addr); + Wire.write(BH1750_POWER_ON); + Wire.endTransmission(); +} +void BH1750::powerDown(uint8_t addr) { + Wire.beginTransmission(addr); + Wire.write(BH1750_POWER_DOWN); + Wire.endTransmission(); +} +void BH1750::reset(uint8_t addr) { + Wire.beginTransmission(addr); + Wire.write(BH1750_RESET); + Wire.endTransmission(); +} + +uint16_t BH1750::readLightLevel(uint8_t addr) { + + uint16_t level; + + Wire.beginTransmission(addr); + Wire.requestFrom(addr, 2); + + level = Wire.read(); + level <<= 8; + level |= Wire.read(); + + 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; +} diff --git a/hardware/arduino/zuno/libraries/ZUNO_BH1750/ZUNO_BH1750.h b/hardware/arduino/zuno/libraries/ZUNO_BH1750/ZUNO_BH1750.h new file mode 100644 index 0000000..72cd28b --- /dev/null +++ b/hardware/arduino/zuno/libraries/ZUNO_BH1750/ZUNO_BH1750.h @@ -0,0 +1,75 @@ +/*************************************************** + This is a library for the BH1750(FVI) light sensor + + This sensor use I2C to communicate, 2 pins are required to + interface + + Datasheet: + http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf + Based on BH1750 lib written by Christopher Laws, March, 2013. + Adapted to ZUNO + ****************************************************/ + +#ifndef BH1750_h +#define BH1750_h + +#include "Arduino.h" + +// BH1750 address on the I2C bus, depending on the level of ADDR pin +#define BH1750_I2CADDR_L 0x23 +#define BH1750_I2CADDR_H 0x5C +// define low address as default +#define BH1750_I2CADDR BH1750_I2CADDR_L + +// 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 1 lx resolution. Measurement time is approx 120ms. +#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 + +// Start measurement at 0.5 lx resolution. Measurement time is approx 120ms. +#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 + +// Start measurement at 4 lx resolution. Measurement time is approx 16ms. +#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 + +// Start measurement at 1 lx 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.5 lx 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 1 lx resolution. Measurement time is approx 120ms. +// Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_LOW_RES_MODE 0x23 + +// max measurement times in ms +#define BH1750_LOW_RES_MODE_MAXTIME 24 +#define BH1750_HIGH_RES_MODE_MAXTIME 180 +#define BH1750_HIGH_RES_MODE_2_MAXTIME 180 + + +class BH1750 { + public: + BH1750(); + void begin(uint8_t mode = BH1750_CONTINUOUS_HIGH_RES_MODE, uint8_t addr = BH1750_I2CADDR_L); + void configure(uint8_t mode = BH1750_CONTINUOUS_HIGH_RES_MODE, uint8_t addr = BH1750_I2CADDR_L); + void powerOn(uint8_t addr = BH1750_I2CADDR_L); + void powerDown(uint8_t addr = BH1750_I2CADDR_L); + void reset(uint8_t addr = BH1750_I2CADDR_L); + uint16_t readLightLevel(uint8_t addr = BH1750_I2CADDR_L); + + private: + uint8_t test; // dummy to get rid of error during compilation if no private part exist +}; + +#endif + diff --git a/hardware/examples/BH1750_LibBsp/BH1750_LibBsp.ino b/hardware/examples/BH1750_LibBsp/BH1750_LibBsp.ino new file mode 100644 index 0000000..1ba2581 --- /dev/null +++ b/hardware/examples/BH1750_LibBsp/BH1750_LibBsp.ino @@ -0,0 +1,53 @@ +// demo sketch for connecting I2C light sensor BH1750 to Z-Uno using ZUNO_BH1750 library + +#include "ZUNO_BH1750.h" +#define DEBUG + +BH1750 bh1750; + +// BH1750 address on the bus: BH1750_I2CADDR_L = 0x23 +#define BH1750_I2CADDR BH1750_I2CADDR_L + +word lightLux; + +// set up channel +//ZUNO_SETUP_CHANNELS( +// ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_LUMINANCE, +// SENSOR_MULTILEVEL_SCALE_LUX, +// SENSOR_MULTILEVEL_SIZE_TWO_BYTES, +// SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, +// getterLight) +//); +// channel definition as one line to get error message reported on correct line (will be fixed in later version of ZUNO) +ZUNO_SETUP_CHANNELS( ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_LUMINANCE, SENSOR_MULTILEVEL_SCALE_LUX, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getterLight)); + +void setup() { + #ifdef DEBUG + Serial.begin(); + #endif + + // Start continous measurement in HIGH_RES_MODE at 1 lx resolution + // Measurement time is approx 120ms. max time 180ms defined as BH1750_HIGH_RES_MODE_MAXTIME + bh1750.begin(BH1750_CONTINUOUS_HIGH_RES_MODE, BH1750_I2CADDR); + + #ifdef DEBUG + Serial.println("start"); + #endif +} + +void loop() { + lightLux = bh1750.readLightLevel(BH1750_I2CADDR); + + #ifdef DEBUG + Serial.print("light = "); + Serial.println(lightLux); + #endif + zunoSendReport(1); + // send every 30 sec + delay(30000); +} + +word getterLight() { + return lightLux; +} +