Kenji Arai / BH1750
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BH1750.h Source File

BH1750.h

00001 /*
00002  * mbed library program
00003  *  light intensity sensor module with built-in a 16 bit AD converter generating digital signal.
00004  *  BH1750 by ROHM Co.,Ltd.
00005  *
00006  * Copyright (c) 2015,'17 Kenji Arai / JH1PJL
00007  *  http://www.page.sannet.ne.jp/kenjia/index.html
00008  *  http://mbed.org/users/kenjiArai/
00009  *      Created: March       7th, 2015
00010  *      Revised: August     23rd, 2017
00011  */
00012 /*
00013  *---------------- REFERENCE ----------------------------------------------------------------------
00014  * Original Information
00015  *  http://www.rohm.co.jp/web/japan/products/-/product/BH1750FVI
00016  * Device kit
00017  *  http://www.aitendo.com/product/10240
00018  */
00019 
00020 #ifndef BH1750_H
00021 #define BH1750_H
00022 
00023 #include "mbed.h"
00024 
00025 // light intensity sensor, BH1750
00026 // 7bit address = 0b0100011(0x23) or 0b1011100(0x5c)
00027 #define BH1750_G_CHIP_ADDR         (0x23 << 1)
00028 #define BH1750_V_CHIP_ADDR         (0x5c << 1)
00029 
00030 ////////////// COMMAND ////////////////////////////////////
00031 #define CMD_PWR_DWN                0x00
00032 #define CMD_PWR_UP                 0x01
00033 #define CMD_RESET                  0x07
00034 #define CMD_C_H_RES_M              0x10
00035 #define CMD_C_H_RES_M2             0x11
00036 #define CMD_C_L_RES_M              0x13
00037 #define CMD_1_H_RES_M              0x20
00038 #define CMD_1_H_RES_M2             0x21
00039 #define CMD_1_L_RES_M              0x23
00040 #define CMD_M_TIME_H               0x40
00041 #define CMD_M_TIME_L               0x60
00042 
00043 ////////////// SENSITIVITY ////////////////////////////////
00044 #define SENS_1R00                  69
00045 #define SENS_3R68                  254
00046 #define SENS_0R45                  31
00047 #define SENS_2R00                  138
00048 #define SENS_0R50                  35
00049 
00050 /** Interface for Luminosity sensor, BH1750
00051  * @code
00052  * #include "mbed.h"
00053  * #include "BH1750.h"
00054  *
00055  * // I2C Communication
00056  *  BH1750      lum(dp5,dp27);    // BH1750 SDA, SCL
00057  * // If you connected I2C line not only this device but also other devices,
00058  * //     you need to declare following method.
00059  *  I2C         i2c(dp5,dp27);    // SDA, SCL
00060  *  BH1750      lum(i2c);         // BH1750 SDA, SCL (Data available every 120mSec)
00061  *
00062  * int main() {;
00063  *   while(true){
00064  *      printf("Illuminance: %+7.2f [Lux]\r\n", lum.lux());
00065  *      wait(1.0);
00066  *   }
00067  * }
00068  * @endcode
00069  */
00070 
00071 class BH1750
00072 {
00073 public:
00074     /** Configure data pin
00075       * @param data SDA and SCL pins
00076       */
00077     BH1750(PinName p_sda, PinName p_scl);
00078     BH1750(PinName p_sda, PinName p_scl, uint8_t addr);
00079 
00080     /** Configure data pin (with other devices on I2C line)
00081       * @param I2C previous definition
00082       */
00083     BH1750(I2C& p_i2c);
00084     BH1750(I2C& p_i2c, uint8_t addr);
00085 
00086     /** Get Illuminance, unit of Lux
00087       * @param none
00088       * @return Lux
00089       */
00090     float lux(void);
00091 
00092     /** Set sensor sensitivity adjustment
00093       * @param sensitivity parameter
00094       * @return none
00095       */
00096     void set_sensitivity(uint8_t parameter);
00097 
00098     /** Set I2C clock frequency
00099       * @param freq.
00100       * @return none
00101       */
00102     void frequency(int hz);
00103 
00104     /** Power Up/Down
00105       * @param none
00106       * @return none
00107       */
00108     void power_up(void);
00109     void power_down(void);
00110 
00111 protected:
00112     I2C *_i2c_p;
00113     I2C &_i2c;
00114 
00115     void init(void);
00116 
00117 private:
00118     uint8_t  BH1750_addr;
00119     uint8_t  dt[4];
00120     int8_t   sensitivity;
00121 };
00122 
00123 #endif      // BH1750_H
00124