Erriez BH1750 library for Arduino  1.1.2
This is a GY-302 breakout with an I2C BH1750 digital light sensor.
ErriezBH1750.cpp
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #include "ErriezBH1750.h"
34 #include "ErriezBH1750_regs.h"
35 
44 BH1750::BH1750(uint8_t addrPinLevel) : _completionTimestamp(0)
45 {
46  if (addrPinLevel == HIGH) {
47  _i2cAddr = BH1750_I2C_ADDR_H;
48  } else {
49  _i2cAddr = BH1750_I2C_ADDR_L;
50  }
51 }
52 
67 {
68  // Store mode and resolution in variable.
69  // Mode will be send in startConversion()
70  _mode = (mode & BH1750_MODE_MASK) | (resolution & BH1750_RES_MASK);
71 
72  // Power down
73  powerDown();
74 }
75 
81 {
82  _completionTimestamp = 0;
84 }
85 
91 {
92  if (IS_INITIALIZED(_mode)) {
93  writeInstruction(_mode);
94  setTimestamp();
95  }
96 }
97 
106 {
107  if (IS_INITIALIZED(_mode) == false) {
108  return false;
109  }
110 
111  if (_completionTimestamp && (millis() >= _completionTimestamp)) {
112  return true;
113  } else {
114  return false;
115  }
116 }
117 
126 {
127  uint8_t timeout;
128 
129  if (IS_INITIALIZED(_mode) == false) {
130  return false;
131  }
132 
133  if (IS_LOW_RESOLUTION(_mode) == true) {
134  timeout = BH1750_CONV_TIME_L;
135  } else {
136  timeout = BH1750_CONV_TIME_H;
137  }
138 
139  // Check if conversion completed in a loop
140  // Wait additional 2m
141  for (uint8_t i = 0; i < (timeout + 2); i++) {
142  if (isConversionCompleted()) {
143  return true;
144  }
145  delay(1);
146  }
147 
148  return false;
149 }
150 
162 uint16_t BH1750::read()
163 {
164  uint16_t level;
165 
166  // Check operation mode
167  if (IS_INITIALIZED(_mode) == false) {
168  return 0;
169  }
170 
171  if (IS_ONE_TIME_MODE(_mode)) {
172  // Clear conversion in one-time mode
173  // Application should call startConversion() again
174  _completionTimestamp = 0;
175  } else if (IS_CONTINUES_MODE(_mode)) {
176  setTimestamp();
177  }
178 
179  // Read two bytes from sensor
180  Wire.requestFrom((int) _i2cAddr, 2);
181 
182  // Read low and high bytes
183  level = (uint16_t) Wire.read();
184  level <<= 8;
185  level |= (uint8_t) Wire.read();
186 
187  //level = 0b1000001110010000; // 28067 lx
188  //level = 0b0000000100010000; // 227 lx
189  //level = 0b0000000000010010; // 7.5 lx
190 
191  // Convert raw value to LUX
192  level = ((((uint32_t) level) * 10) + 5) / 12;
193 
194  return level;
195 }
196 
197 //------------------------------------------------------------------------------
198 // Protected functions
199 //------------------------------------------------------------------------------
205 {
206  _completionTimestamp = millis() + GET_TIMEOUT(_mode);
207 }
208 
215 void BH1750::writeInstruction(uint8_t instruction)
216 {
217  Wire.beginTransmission(_i2cAddr);
218  Wire.write(instruction);
219  Wire.endTransmission();
220 }
BH1750_Resolution_e
Resolution register bits.
Definition: ErriezBH1750.h:46
bool waitForCompletion()
Wait for completion.
#define BH1750_I2C_ADDR_H
I2C address with ADDR pin high.
#define BH1750_MODE_MASK
Mode mask bits.
BH1750_Mode_e
Mode register bits.
Definition: ErriezBH1750.h:40
void writeInstruction(uint8_t instruction)
Write instruction to sensor.
#define BH1750_POWER_DOWN
Power down instruction.
BH1750(uint8_t addrPinLevel=LOW)
Constructor.
#define GET_TIMEOUT(mode)
void powerDown()
Power down. Call startConversion() to power-up automatically.
#define BH1750_I2C_ADDR_L
I2C address with ADDR pin low.
#define BH1750_CONV_TIME_H
Worst case conversion timing high res.
#define IS_INITIALIZED(mode)
#define BH1750_RES_MASK
Mode resolution mask bits.
BH1750 digital light sensor library for Arduino.
BH1750 digital light sensor library for Arduino.
#define IS_ONE_TIME_MODE(mode)
void begin(BH1750_Mode_e mode, BH1750_Resolution_e resolution)
Set mode and resolution.
#define BH1750_CONV_TIME_L
Worst case conversion timing low res.
#define IS_CONTINUES_MODE(mode)
void setTimestamp()
Save current time + minimum delay before reading next conversion in ms.
bool isConversionCompleted()
Wait for completion.
#define IS_LOW_RESOLUTION(mode)
uint16_t read()
Read light level asynchronous from sensor The application is responsible for wait or checking a compl...
void startConversion()
Start conversion.