Digital 16bit Serial Output Type Ambient Light Sensor IC by ROHM, Ambient light sensor (Illuminance to digital converter)
Revision 0:d95f6ac4c8d6, committed 2015-03-07
- Comitter:
- kenjiArai
- Date:
- Sat Mar 07 23:44:42 2015 +0000
- Child:
- 1:4a02a8fbf021
- Commit message:
- Digital 16bit Serial Output Type Ambient Light Sensor IC by ROHM, Ambient light sensor (Illuminance to digital converter)
Changed in this revision
BH1750.cpp | Show annotated file Show diff for this revision Revisions of this file |
BH1750.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BH1750.cpp Sat Mar 07 23:44:42 2015 +0000 @@ -0,0 +1,99 @@ +/* + * mbed library program + * light intensity sensor module with built-in a 16 bit AD converter generating digital signal. + * BH1750 by ROHM Co.,Ltd. + * + * Copyright (c) 2015 Kenji Arai / JH1PJL + * http://www.page.sannet.ne.jp/kenjia/index.html + * http://mbed.org/users/kenjiArai/ + * Created: March 7th, 2015 + * Revised: March 8th, 2015 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "BH1750.h" + +BH1750::BH1750 (PinName p_sda, PinName p_scl) : _i2c(p_sda, p_scl) +{ + BH1750_addr = BH1750_G_CHIP_ADDR; + init(); +} + +BH1750::BH1750 (PinName p_sda, PinName p_scl, uint8_t addr) : _i2c(p_sda, p_scl) +{ + BH1750_addr = addr; + init(); +} + +BH1750::BH1750 (I2C& p_i2c) : _i2c(p_i2c) +{ + BH1750_addr = BH1750_G_CHIP_ADDR; + init(); +} + +BH1750::BH1750 (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c) +{ + BH1750_addr = addr; + init(); +} + +/////////////// Read Lux from sensor ////////////////////// +float BH1750::lux() +{ + float lux; + + _i2c.read(BH1750_addr, (char *)dt, 2, false); + lux = (float)(dt[0] << 8 | dt[1]); + lux = lux / 1.2 * ((float)sensitivity/69); + return lux; +} + +/////////////// Initialize //////////////////////////////// +void BH1750::init() +{ + _i2c.frequency(100000); + power_up(); + sensitivity = SENS_1R00; + set_sensitivity(sensitivity); +} + +/////////////// Timing Register /////////////////////////// +void BH1750::set_sensitivity(uint8_t parameter) +{ + if (parameter > SENS_3R68){ + parameter = SENS_3R68; + } else if (parameter < SENS_0R45){ + parameter = SENS_0R45; + } + dt[0] = CMD_M_TIME_H | (parameter >> 5); // Set High byte + _i2c.write((int)BH1750_addr, (char *)dt, 1, false); + dt[0] = CMD_M_TIME_L | (parameter & 0x1f); // Set Low byte + _i2c.write((int)BH1750_addr, (char *)dt, 1, false); + dt[0] = CMD_C_H_RES_M; // Measurement mode: High Resolution + _i2c.write((int)BH1750_addr, (char *)dt, 1, false); + wait_ms(240); // need normal conversion time(120mS) x 2 +} + +/////////////// Power ON/OFF ////////////////////////////// +void BH1750::power_up() +{ + dt[0] = CMD_PWR_UP; + _i2c.write((int)BH1750_addr, (char *)dt, 1, false); +} + +void BH1750::power_down() +{ + dt[0] = CMD_PWR_DWN; + _i2c.write((int)BH1750_addr, (char *)dt, 1, false); +} + +/////////////// I2C Freq. ///////////////////////////////// +void BH1750::frequency(int hz) +{ + _i2c.frequency(hz); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BH1750.h Sat Mar 07 23:44:42 2015 +0000 @@ -0,0 +1,128 @@ +/* + * mbed library program + * light intensity sensor module with built-in a 16 bit AD converter generating digital signal. + * BH1750 by ROHM Co.,Ltd. + * + * Copyright (c) 2015 Kenji Arai / JH1PJL + * http://www.page.sannet.ne.jp/kenjia/index.html + * http://mbed.org/users/kenjiArai/ + * Created: March 7th, 2015 + * Revised: March 8th, 2015 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* + *---------------- REFERENCE ---------------------------------------------------------------------- + * Original Information + * http://www.rohm.co.jp/web/japan/products/-/product/BH1750FVI + * Device kit + * http://www.aitendo.com/product/10240 + */ + +#ifndef BH1750_H +#define BH1750_H + +#include "mbed.h" + +// light intensity sensor, BH1750 +// 7bit address = 0b0100011(0x23) or 0b1011100(0x5c) +#define BH1750_G_CHIP_ADDR (0x23 << 1) +#define BH1750_V_CHIP_ADDR (0x5c << 1) + +////////////// COMMAND //////////////////////////////////// +#define CMD_PWR_DWN 0x00 +#define CMD_PWR_UP 0x01 +#define CMD_RESET 0x07 +#define CMD_C_H_RES_M 0x10 +#define CMD_C_H_RES_M2 0x11 +#define CMD_C_L_RES_M 0x13 +#define CMD_1_H_RES_M 0x20 +#define CMD_1_H_RES_M2 0x21 +#define CMD_1_L_RES_M 0x23 +#define CMD_M_TIME_H 0x40 +#define CMD_M_TIME_L 0x60 + +////////////// SENSITIVITY //////////////////////////////// +#define SENS_1R00 69 +#define SENS_3R68 254 +#define SENS_0R45 31 +#define SENS_2R00 138 +#define SENS_0R50 35 + +/** Interface for Luminosity sensor, BH1750 + * @code + * #include "mbed.h" + * #include "BH1750.h" + * + * // I2C Communication + * BH1750 lum(dp5,dp27); // BH1750 SDA, SCL + * // If you connected I2C line not only this device but also other devices, + * // you need to declare following method. + * I2C i2c(dp5,dp27); // SDA, SCL + * BH1750 lum(i2c); // BH1750 SDA, SCL (Data available every 120mSec) + * + * int main() {; + * while(true){ + * printf("Illuminance: %+7.2f [Lux]\r\n", lum.lux()); + * wait(1.0); + * } + * } + * @endcode + */ + +class BH1750 +{ +public: + /** Configure data pin + * @param data SDA and SCL pins + */ + BH1750(PinName p_sda, PinName p_scl); + BH1750(PinName p_sda, PinName p_scl, uint8_t addr); + + /** Configure data pin (with other devices on I2C line) + * @param I2C previous definition + */ + BH1750(I2C& p_i2c); + BH1750(I2C& p_i2c, uint8_t addr); + + /** Get Illuminance, unit of Lux + * @param none + * @return Lux + */ + float lux(void); + + /** Set sensor sensitivity adjustment + * @param sensitivity parameter + * @return none + */ + void set_sensitivity(uint8_t parameter); + + /** Set I2C clock frequency + * @param freq. + * @return none + */ + void frequency(int hz); + + /** Power Up/Down + * @param none + * @return none + */ + void power_up(void); + void power_down(void); + +protected: + I2C _i2c; + + void init(void); + +private: + uint8_t BH1750_addr; + uint8_t dt[4]; + int8_t sensitivity; +}; + +#endif // BH1750_H