Library for digital light sensor BH1750 (GY-30). SPI Test Comunication with ArduinoMega

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BH1750.h Source File

BH1750.h

00001 /*
00002 
00003 This is a library for the BH1750FVI (GY-30) Digital Light Sensor
00004 breakout board.
00005 
00006 The board uses I2C for communication. 2 pins are required to
00007 interface to the device and in most cases also pull up resistors.
00008 
00009 Datasheet:
00010 http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
00011 
00012 Inspired by Christopher Laws, March, 2013.
00013 Written by Michal Stehlik, August, 2014.
00014 
00015 */
00016 
00017 #ifndef BH1750_h
00018 #define BH1750_h
00019 
00020 #include "mbed.h"
00021 
00022 #define BH1750_I2CADDR 0x46
00023 
00024 // No active state
00025 #define BH1750_POWER_DOWN 0x00
00026 
00027 // Wating for measurment command
00028 #define BH1750_POWER_ON 0x01
00029 
00030 // Reset data register value - not accepted in POWER_DOWN mode
00031 #define BH1750_RESET 0x07
00032 
00033 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
00034 #define BH1750_CONTINUOUS_HIGH_RES_MODE  0x10
00035 
00036 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
00037 #define BH1750_CONTINUOUS_HIGH_RES_MODE_2  0x11
00038 
00039 // Start measurement at 4lx resolution. Measurement time is approx 16ms.
00040 #define BH1750_CONTINUOUS_LOW_RES_MODE  0x13
00041 
00042 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
00043 // Device is automatically set to Power Down after measurement.
00044 #define BH1750_ONE_TIME_HIGH_RES_MODE  0x20
00045 
00046 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
00047 // Device is automatically set to Power Down after measurement.
00048 #define BH1750_ONE_TIME_HIGH_RES_MODE_2  0x21
00049 
00050 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
00051 // Device is automatically set to Power Down after measurement.
00052 #define BH1750_ONE_TIME_LOW_RES_MODE  0x23
00053 
00054 /**
00055 * @code
00056 * #include "mbed.h"
00057 * #include "BH1750.h"
00058 *
00059 * BH1750 lightSensor(I2C_SDA, I2C_SCL);
00060 * Serial pc(SERIAL_TX, SERIAL_RX);
00061 *
00062 * int main() 
00063 * {
00064 *    lightSensor.init();
00065 *
00066 *    while(1) {
00067 *        pc.printf("Light intensity: %.4f Lux\r\n", lightSensor.readIntesity());
00068 *        wait(5);
00069 *    }
00070 * }
00071 * @endcode
00072 */
00073 
00074 class BH1750 
00075 {
00076 public:
00077     /**
00078     * @param sda
00079     * @param scl
00080     */
00081     BH1750 (PinName sda, PinName slc);
00082 
00083     /**
00084     * Initialize sensor with specified mode.
00085     * Default mode is set to BH1750_CONTINUOUS_HIGH_RES_MODE.
00086     * @param mode of sensor operation.
00087     */
00088     void init(char mode = BH1750_CONTINUOUS_HIGH_RES_MODE);
00089 
00090     /**
00091     * Configure sensor with specified mode.
00092     * This function is called also in init(char);
00093     * @param mode of sensor operation.
00094     */
00095     void configure(char mode);
00096 
00097     /**
00098     * Read light intensity.
00099     * In addition also error code and raw data are filled.
00100     * @return light intensity in Lux.
00101     */
00102     float readIntesity(void);
00103 
00104     /**
00105     * Get raw data obtained after sucessfull executing of readIntesity(void).
00106     * @return unprocessed raw data readed from sensor.
00107     */
00108     char* getRawData(void);
00109 
00110     /**
00111     * @returns
00112     *     0 no error.
00113     *     !=0 I2C error code.
00114     */
00115     int getStatus (void);
00116 
00117 private:
00118     I2C i2c;
00119     char rawDataBuffer[2];
00120     int status;
00121 };
00122 
00123 #endif