Library for the BH1750 Digital 16-bit Serial Output Type Ambient Light Sensor IC.
BH1750.h@1:8115dc8275eb, 2017-08-10 (annotated)
- Committer:
- mcm
- Date:
- Thu Aug 10 16:28:23 2017 +0000
- Revision:
- 1:8115dc8275eb
- Parent:
- 0:bea4477679ee
The BH1750 library has been completed and tested, it works as expected.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mcm | 1:8115dc8275eb | 1 | /** |
mcm | 1:8115dc8275eb | 2 | * @brief BH1750.h |
mcm | 1:8115dc8275eb | 3 | * @details Digital 16-bit Serial Output Type Ambient Light Sensor IC. |
mcm | 1:8115dc8275eb | 4 | * Header file. |
mcm | 1:8115dc8275eb | 5 | * |
mcm | 1:8115dc8275eb | 6 | * |
mcm | 1:8115dc8275eb | 7 | * @return NA |
mcm | 1:8115dc8275eb | 8 | * |
mcm | 1:8115dc8275eb | 9 | * @author Manuel Caballero |
mcm | 1:8115dc8275eb | 10 | * @date 10/August/2017 |
mcm | 1:8115dc8275eb | 11 | * @version 10/August/2017 The ORIGIN |
mcm | 1:8115dc8275eb | 12 | * @pre NaN |
mcm | 1:8115dc8275eb | 13 | * @warning NaN |
mcm | 1:8115dc8275eb | 14 | * @pre This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ). |
mcm | 1:8115dc8275eb | 15 | */ |
mcm | 0:bea4477679ee | 16 | #ifndef BH1750_H |
mcm | 0:bea4477679ee | 17 | #define BH1750_H |
mcm | 1:8115dc8275eb | 18 | |
mcm | 0:bea4477679ee | 19 | #include "mbed.h" |
mcm | 0:bea4477679ee | 20 | |
mcm | 0:bea4477679ee | 21 | |
mcm | 0:bea4477679ee | 22 | /** |
mcm | 1:8115dc8275eb | 23 | Example: |
mcm | 1:8115dc8275eb | 24 | |
mcm | 1:8115dc8275eb | 25 | #include "mbed.h" |
mcm | 1:8115dc8275eb | 26 | #include "BH1750.h" |
mcm | 1:8115dc8275eb | 27 | |
mcm | 1:8115dc8275eb | 28 | BH1750 myLightSensor ( I2C_SDA, I2C_SCL, BH1750::BH1750_ADDR_L, 400000 ); //[todo] |
mcm | 1:8115dc8275eb | 29 | Serial pc ( USBTX, USBRX ); // tx, rx |
mcm | 1:8115dc8275eb | 30 | |
mcm | 1:8115dc8275eb | 31 | Ticker serial; |
mcm | 1:8115dc8275eb | 32 | |
mcm | 1:8115dc8275eb | 33 | DigitalOut myled(LED1); |
mcm | 0:bea4477679ee | 34 | |
mcm | 0:bea4477679ee | 35 | |
mcm | 1:8115dc8275eb | 36 | void sendDATA ( void ) |
mcm | 1:8115dc8275eb | 37 | { |
mcm | 1:8115dc8275eb | 38 | float myLux = 0; |
mcm | 1:8115dc8275eb | 39 | |
mcm | 1:8115dc8275eb | 40 | myled = 0; |
mcm | 1:8115dc8275eb | 41 | |
mcm | 1:8115dc8275eb | 42 | myLightSensor.BH1750_TriggerMeasurement ( BH1750::BH1750_ONE_TIME_H_RESOLUTION_MODE ); |
mcm | 1:8115dc8275eb | 43 | wait ( 1 ); |
mcm | 1:8115dc8275eb | 44 | myLightSensor.BH1750_ReadLux ( &myLux ); |
mcm | 1:8115dc8275eb | 45 | |
mcm | 1:8115dc8275eb | 46 | pc.printf( "Lux: %0.1f\r\n", myLux ); |
mcm | 1:8115dc8275eb | 47 | |
mcm | 1:8115dc8275eb | 48 | myled = 1; |
mcm | 1:8115dc8275eb | 49 | |
mcm | 1:8115dc8275eb | 50 | } |
mcm | 0:bea4477679ee | 51 | |
mcm | 0:bea4477679ee | 52 | |
mcm | 1:8115dc8275eb | 53 | int main() { |
mcm | 1:8115dc8275eb | 54 | pc.baud ( 115200 ); |
mcm | 1:8115dc8275eb | 55 | serial.attach( &sendDATA, 3 ); // the address of the function to be attached ( sendDATA ) and the interval ( 3s ) |
mcm | 1:8115dc8275eb | 56 | |
mcm | 1:8115dc8275eb | 57 | // Let the callbacks take care of everything |
mcm | 1:8115dc8275eb | 58 | while(1) sleep(); |
mcm | 1:8115dc8275eb | 59 | } |
mcm | 1:8115dc8275eb | 60 | */ |
mcm | 0:bea4477679ee | 61 | |
mcm | 0:bea4477679ee | 62 | /*! |
mcm | 1:8115dc8275eb | 63 | Library for the BH1750 Digital 16-bit Serial Output Type Ambient Light Sensor IC. |
mcm | 0:bea4477679ee | 64 | */ |
mcm | 0:bea4477679ee | 65 | class BH1750 |
mcm | 0:bea4477679ee | 66 | { |
mcm | 0:bea4477679ee | 67 | public: |
mcm | 1:8115dc8275eb | 68 | /** Represents the different I2C address possibilities for the BH1750 |
mcm | 1:8115dc8275eb | 69 | */ |
mcm | 1:8115dc8275eb | 70 | enum BH1750_ADDRESS { |
mcm | 1:8115dc8275eb | 71 | BH1750_ADDR_L = ( 0x23 << 1 ), /**< Addr pin = GND */ |
mcm | 1:8115dc8275eb | 72 | BH1750_ADDR_H = ( 0x5C << 1 ) /**< Addr pin = VDD */ |
mcm | 1:8115dc8275eb | 73 | }; |
mcm | 1:8115dc8275eb | 74 | |
mcm | 1:8115dc8275eb | 75 | enum BH1750_COMMANDS { |
mcm | 1:8115dc8275eb | 76 | BH1750_POWER_DOWN = 0x00, /*!< No active state */ |
mcm | 1:8115dc8275eb | 77 | BH1750_POWER_ON = 0x01, /*!< Waiting for measurement command */ |
mcm | 1:8115dc8275eb | 78 | BH1750_RESET = 0x07, /*!< Reset Data register value. Reset command is not acceptable in Power Down mode */ |
mcm | 1:8115dc8275eb | 79 | BH1750_CONTINUOUSLY_H_RESOLUTION_MODE = 0x10, /*!< Start measurement at 1lx resolution. Measurement Time is typically 120ms */ |
mcm | 1:8115dc8275eb | 80 | BH1750_CONTINUOUSLY_H_RESOLUTION_MODE2 = 0x11, /*!< Start measurement at 0.5lx resolution. Measurement Time is typically 120ms */ |
mcm | 1:8115dc8275eb | 81 | BH1750_CONTINUOUSLY_L_RESOLUTION_MODE = 0x13, /*!< Start measurement at 4lx resolution. Measurement Time is typically 16ms */ |
mcm | 1:8115dc8275eb | 82 | 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 */ |
mcm | 1:8115dc8275eb | 83 | 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 */ |
mcm | 1:8115dc8275eb | 84 | 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 */ |
mcm | 1:8115dc8275eb | 85 | }; |
mcm | 1:8115dc8275eb | 86 | |
mcm | 1:8115dc8275eb | 87 | enum BH1750_SENSITIVITY { |
mcm | 1:8115dc8275eb | 88 | BH1750_SENSITIVITY_DEFAULT = 0x45 /*!< Measurement Time Register by default. This is for registration of measurement time */ |
mcm | 1:8115dc8275eb | 89 | }; |
mcm | 1:8115dc8275eb | 90 | |
mcm | 1:8115dc8275eb | 91 | |
mcm | 1:8115dc8275eb | 92 | enum BH1750_CONSTANT { |
mcm | 1:8115dc8275eb | 93 | BH1750_SUCCESS = 0x00, /*!< The communication was fine */ |
mcm | 1:8115dc8275eb | 94 | BH1750_FAILURE = 0x01, /*!< Something went wrong */ |
mcm | 1:8115dc8275eb | 95 | I2C_SUCCESS = 0x00 /*!< I2C communication was fine */ |
mcm | 1:8115dc8275eb | 96 | }; |
mcm | 1:8115dc8275eb | 97 | |
mcm | 1:8115dc8275eb | 98 | |
mcm | 1:8115dc8275eb | 99 | /** Create an BH1750 object connected to the specified I2C pins. |
mcm | 1:8115dc8275eb | 100 | * |
mcm | 1:8115dc8275eb | 101 | * @param sda I2C data pin |
mcm | 1:8115dc8275eb | 102 | * @param scl I2C clock pin |
mcm | 1:8115dc8275eb | 103 | * @param addr I2C slave address |
mcm | 1:8115dc8275eb | 104 | * @param freq I2C frequency in Hz. |
mcm | 1:8115dc8275eb | 105 | */ |
mcm | 1:8115dc8275eb | 106 | BH1750 ( PinName sda, PinName scl, uint32_t addr, uint32_t freq ); |
mcm | 0:bea4477679ee | 107 | |
mcm | 1:8115dc8275eb | 108 | /** Delete BH1750 object. |
mcm | 1:8115dc8275eb | 109 | */ |
mcm | 1:8115dc8275eb | 110 | ~BH1750(); |
mcm | 1:8115dc8275eb | 111 | |
mcm | 1:8115dc8275eb | 112 | |
mcm | 1:8115dc8275eb | 113 | /** It put the BH1750 sensor in the lowest power mode. |
mcm | 1:8115dc8275eb | 114 | */ |
mcm | 1:8115dc8275eb | 115 | uint32_t BH1750_PowerDown ( void ); |
mcm | 1:8115dc8275eb | 116 | |
mcm | 1:8115dc8275eb | 117 | /** It turns on the BH1750 sensor. |
mcm | 1:8115dc8275eb | 118 | */ |
mcm | 1:8115dc8275eb | 119 | uint32_t BH1750_PowerOn ( void ); |
mcm | 1:8115dc8275eb | 120 | |
mcm | 1:8115dc8275eb | 121 | /** It clears the Data register. |
mcm | 1:8115dc8275eb | 122 | */ |
mcm | 1:8115dc8275eb | 123 | uint32_t BH1750_ResetDataRegister ( void ); |
mcm | 1:8115dc8275eb | 124 | |
mcm | 1:8115dc8275eb | 125 | /** Create an BH1750 object connected to the specified I2C pins. |
mcm | 1:8115dc8275eb | 126 | * @param mode One-shot/Continuous mode in High, High2 or Low resolution. |
mcm | 1:8115dc8275eb | 127 | */ |
mcm | 1:8115dc8275eb | 128 | uint32_t BH1750_TriggerMeasurement ( BH1750_COMMANDS mode ); |
mcm | 1:8115dc8275eb | 129 | |
mcm | 1:8115dc8275eb | 130 | /** It configures a new sensitivity. |
mcm | 1:8115dc8275eb | 131 | */ |
mcm | 1:8115dc8275eb | 132 | uint32_t BH1750_NewSensitivity ( uint8_t newSensitivity ); |
mcm | 1:8115dc8275eb | 133 | |
mcm | 1:8115dc8275eb | 134 | /** It reads the raw data from the BH1750 sensor. |
mcm | 1:8115dc8275eb | 135 | * @param myRawData 2-Byte array. |
mcm | 1:8115dc8275eb | 136 | */ |
mcm | 1:8115dc8275eb | 137 | uint32_t BH1750_ReadRawData ( char* myRawData ); |
mcm | 1:8115dc8275eb | 138 | |
mcm | 1:8115dc8275eb | 139 | /** It reads a converted data from the BH1750 sensor. |
mcm | 1:8115dc8275eb | 140 | * @param myLux Light value. |
mcm | 1:8115dc8275eb | 141 | */ |
mcm | 1:8115dc8275eb | 142 | uint32_t BH1750_ReadLux ( float* myLux ); |
mcm | 1:8115dc8275eb | 143 | |
mcm | 1:8115dc8275eb | 144 | |
mcm | 0:bea4477679ee | 145 | private: |
mcm | 1:8115dc8275eb | 146 | I2C i2c; |
mcm | 1:8115dc8275eb | 147 | uint32_t BH1750_Addr; |
mcm | 1:8115dc8275eb | 148 | uint32_t BH1750_Mode; |
mcm | 0:bea4477679ee | 149 | }; |
mcm | 1:8115dc8275eb | 150 | |
mcm | 0:bea4477679ee | 151 | #endif |