Manuel Caballero / BH1750
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BH1750.h Source File

BH1750.h

00001 /**
00002  * @brief       BH1750.h
00003  * @details     Digital 16-bit Serial Output Type Ambient Light Sensor IC.
00004  *              Header file.
00005  *
00006  *
00007  * @return      NA
00008  *
00009  * @author      Manuel Caballero
00010  * @date        10/August/2017
00011  * @version     10/August/2017    The ORIGIN
00012  * @pre         NaN
00013  * @warning     NaN
00014  * @pre         This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ).
00015  */
00016 #ifndef BH1750_H
00017 #define BH1750_H
00018 
00019 #include "mbed.h"
00020 
00021 
00022 /**
00023     Example:
00024     
00025     #include "mbed.h"
00026     #include "BH1750.h"
00027 
00028     BH1750 myLightSensor ( I2C_SDA, I2C_SCL, BH1750::BH1750_ADDR_L, 400000 );   //[todo]
00029     Serial pc            ( USBTX, USBRX );                              // tx, rx
00030 
00031     Ticker serial;
00032 
00033     DigitalOut myled(LED1);
00034 
00035 
00036     void sendDATA ( void )
00037     {
00038         float    myLux         =       0;
00039     
00040         myled = 0;
00041     
00042         myLightSensor.BH1750_TriggerMeasurement ( BH1750::BH1750_ONE_TIME_H_RESOLUTION_MODE );
00043         wait ( 1 );
00044         myLightSensor.BH1750_ReadLux    ( &myLux );
00045     
00046         pc.printf( "Lux: %0.1f\r\n", myLux );
00047     
00048         myled = 1;
00049 
00050     }
00051 
00052 
00053     int main() {
00054         pc.baud ( 115200 );
00055         serial.attach( &sendDATA, 3 );                      // the address of the function to be attached ( sendDATA ) and the interval ( 3s )
00056     
00057         // Let the callbacks take care of everything
00058         while(1)  sleep();
00059     }
00060 */
00061 
00062 /*!
00063  Library for the BH1750 Digital 16-bit Serial Output Type Ambient Light Sensor IC.
00064 */
00065 class BH1750
00066 {
00067 public:
00068     /** Represents the different I2C address possibilities for the BH1750
00069        */
00070     enum BH1750_ADDRESS {
00071         BH1750_ADDR_L   =    ( 0x23 << 1 ),         /**< Addr pin = GND */
00072         BH1750_ADDR_H   =    ( 0x5C << 1 )          /**< Addr pin = VDD */
00073     };
00074 
00075     enum BH1750_COMMANDS  {
00076         BH1750_POWER_DOWN                         =      0x00,        /*!<   No active state                                                                                                                                  */
00077         BH1750_POWER_ON                           =      0x01,        /*!<   Waiting for measurement command                                                                                                                  */
00078         BH1750_RESET                              =      0x07,        /*!<   Reset Data register value. Reset command is not acceptable in Power Down mode                                                                    */
00079         BH1750_CONTINUOUSLY_H_RESOLUTION_MODE     =      0x10,        /*!<   Start measurement at 1lx resolution. Measurement Time is typically 120ms                                                                         */
00080         BH1750_CONTINUOUSLY_H_RESOLUTION_MODE2    =      0x11,        /*!<   Start measurement at 0.5lx resolution. Measurement Time is typically 120ms                                                                       */
00081         BH1750_CONTINUOUSLY_L_RESOLUTION_MODE     =      0x13,        /*!<   Start measurement at 4lx resolution. Measurement Time is typically 16ms                                                                          */
00082         BH1750_ONE_TIME_H_RESOLUTION_MODE         =      0x20,        /*!<   Start measurement at 1lx resolution. Measurement Time is typically 120ms. It is automatically set to Power Down mode after measurement           */
00083         BH1750_ONE_TIME_H_RESOLUTION_MODE2        =      0x21,        /*!<   Start measurement at 0.5lx resolution. Measurement Time is typically 120ms. It is automatically set to Power Down mode after measurement         */
00084         BH1750_ONE_TIME_L_RESOLUTION_MODE         =      0x23         /*!<   Start measurement at 4lx resolution. Measurement Time is typically 16ms. It is automatically set to Power Down mode after measurement            */
00085     };
00086 
00087     enum BH1750_SENSITIVITY  {
00088         BH1750_SENSITIVITY_DEFAULT                =      0x45         /*!<   Measurement Time Register by default. This is for registration of measurement time                                                               */
00089     };
00090 
00091 
00092     enum BH1750_CONSTANT  {
00093         BH1750_SUCCESS                            =      0x00,        /*!<   The communication was fine                                                                                                                       */
00094         BH1750_FAILURE                            =      0x01,        /*!<   Something went wrong                                                                                                                             */
00095         I2C_SUCCESS                               =      0x00         /*!<   I2C communication was fine                                                                                                                       */
00096     };
00097 
00098 
00099     /** Create an BH1750 object connected to the specified I2C pins.
00100      *
00101      * @param sda     I2C data pin
00102      * @param scl     I2C clock pin
00103      * @param addr    I2C slave address
00104      * @param freq    I2C frequency in Hz.
00105      */
00106     BH1750 ( PinName sda, PinName scl, uint32_t addr, uint32_t freq );
00107 
00108     /** Delete BH1750 object.
00109      */
00110     ~BH1750();
00111 
00112 
00113     /** It put the BH1750 sensor in the lowest power mode.
00114      */
00115     uint32_t  BH1750_PowerDown          ( void );
00116     
00117     /** It turns on the BH1750 sensor.
00118      */
00119     uint32_t  BH1750_PowerOn            ( void );
00120     
00121     /** It clears the Data register.
00122      */
00123     uint32_t  BH1750_ResetDataRegister  ( void );
00124     
00125     /** Create an BH1750 object connected to the specified I2C pins.
00126      * @param mode    One-shot/Continuous mode in High, High2 or Low resolution.
00127      */
00128     uint32_t  BH1750_TriggerMeasurement ( BH1750_COMMANDS  mode );
00129     
00130     /** It configures a new sensitivity.
00131      */
00132     uint32_t  BH1750_NewSensitivity     ( uint8_t newSensitivity );
00133     
00134     /** It reads the raw data from the BH1750 sensor.
00135      * @param myRawData    2-Byte array.
00136      */
00137     uint32_t  BH1750_ReadRawData        ( char* myRawData );
00138     
00139     /** It reads a converted data from the BH1750 sensor.
00140      * @param myLux    Light value.
00141      */
00142     uint32_t  BH1750_ReadLux            ( float* myLux );
00143 
00144 
00145 private:
00146     I2C      i2c;
00147     uint32_t BH1750_Addr;
00148     uint32_t BH1750_Mode;
00149 };
00150 
00151 #endif