Skip to content

Commit ee00a24

Browse files
authored
Merge pull request #63 from protohaus/master
Add support for setting TwoWire instance
2 parents 0643631 + dbaf749 commit ee00a24

3 files changed

Lines changed: 101 additions & 19 deletions

File tree

BH1750.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030

3131
// Legacy Wire.write() function fix
3232
#if (ARDUINO >= 100)
33-
#define __wire_write(d) Wire.write(d)
33+
#define __wire_write(d) I2C->write(d)
3434
#else
35-
#define __wire_write(d) Wire.send(d)
35+
#define __wire_write(d) I2C->send(d)
3636
#endif
3737

3838

3939
// Legacy Wire.read() function fix
4040
#if (ARDUINO >= 100)
41-
#define __wire_read() Wire.read()
41+
#define __wire_read() I2C->read()
4242
#else
43-
#define __wire_read() Wire.receive()
43+
#define __wire_read() I2C->receive()
4444
#endif
4545

4646

@@ -53,17 +53,27 @@
5353
BH1750::BH1750(byte addr) {
5454

5555
BH1750_I2CADDR = addr;
56-
56+
// Allows user to change TwoWire instance
57+
I2C = &Wire;
5758
}
5859

5960

6061
/**
6162
* Configure sensor
6263
* @param mode Measurement mode
64+
* @param addr Address of the sensor
65+
* @param i2c TwoWire instance connected to I2C bus
6366
*/
64-
bool BH1750::begin(Mode mode) {
67+
bool BH1750::begin(Mode mode, byte addr, TwoWire *i2c) {
6568

6669
// I2C is expected to be initialized outside this library
70+
// But, allows a different address and TwoWire instance to be used
71+
if(i2c) {
72+
I2C = i2c;
73+
}
74+
if(addr) {
75+
BH1750_I2CADDR = addr;
76+
}
6777

6878
// Configure sensor in specified mode
6979
return configure(mode);
@@ -91,9 +101,9 @@ bool BH1750::configure(Mode mode) {
91101
case BH1750::ONE_TIME_LOW_RES_MODE:
92102

93103
// Send mode to sensor
94-
Wire.beginTransmission(BH1750_I2CADDR);
104+
I2C->beginTransmission(BH1750_I2CADDR);
95105
__wire_write((uint8_t)BH1750_MODE);
96-
ack = Wire.endTransmission();
106+
ack = I2C->endTransmission();
97107

98108
// Wait a few moments to wake up
99109
_delay_ms(10);
@@ -149,15 +159,15 @@ bool BH1750::setMTreg(byte MTreg) {
149159
// Send MTreg and the current mode to the sensor
150160
// High bit: 01000_MT[7,6,5]
151161
// Low bit: 011_MT[4,3,2,1,0]
152-
Wire.beginTransmission(BH1750_I2CADDR);
162+
I2C->beginTransmission(BH1750_I2CADDR);
153163
__wire_write((0b01000 << 3) | (MTreg >> 5));
154-
ack = Wire.endTransmission();
155-
Wire.beginTransmission(BH1750_I2CADDR);
164+
ack = I2C->endTransmission();
165+
I2C->beginTransmission(BH1750_I2CADDR);
156166
__wire_write((0b011 << 5 ) | (MTreg & 0b11111));
157-
ack = ack | Wire.endTransmission();
158-
Wire.beginTransmission(BH1750_I2CADDR);
167+
ack = ack | I2C->endTransmission();
168+
I2C->beginTransmission(BH1750_I2CADDR);
159169
__wire_write(BH1750_MODE);
160-
ack = ack | Wire.endTransmission();
170+
ack = ack | I2C->endTransmission();
161171

162172
// Wait a few moments to wake up
163173
_delay_ms(10);
@@ -218,9 +228,9 @@ float BH1750::readLightLevel(bool maxWait) {
218228
float level = -1.0;
219229

220230
// Send mode to sensor
221-
Wire.beginTransmission(BH1750_I2CADDR);
231+
I2C->beginTransmission(BH1750_I2CADDR);
222232
__wire_write((uint8_t)BH1750_MODE);
223-
Wire.endTransmission();
233+
I2C->endTransmission();
224234

225235
// Wait for measurement to be taken.
226236
// Measurements have a maximum measurement time and a typical measurement
@@ -247,7 +257,7 @@ float BH1750::readLightLevel(bool maxWait) {
247257

248258
// Read two bytes from the sensor, which are low and high parts of the sensor
249259
// value
250-
if (2 == Wire.requestFrom((int)BH1750_I2CADDR, (int)2)) {
260+
if (2 == I2C->requestFrom((int)BH1750_I2CADDR, (int)2)) {
251261
unsigned int tmp = 0;
252262
tmp = __wire_read();
253263
tmp <<= 8;

BH1750.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class BH1750 {
6060
};
6161

6262
BH1750(byte addr = 0x23);
63-
bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE);
63+
bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE, byte addr = 0x23,
64+
TwoWire* i2c = nullptr);
6465
bool configure(Mode mode);
6566
bool setMTreg(byte MTreg);
6667
float readLightLevel(bool maxWait = false);
@@ -73,7 +74,7 @@ class BH1750 {
7374
// for more information.
7475
const float BH1750_CONV_FACTOR = 1.2;
7576
Mode BH1750_MODE = UNCONFIGURED;
76-
77+
TwoWire* I2C;
7778
};
7879

7980
#endif
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
3+
Example of BH1750 library usage.
4+
5+
This example initialises two BH1750 objects using different TwoWire
6+
instances (Wire and Wire1) and then makes a light level reading every second.
7+
This is the case for boards such as the ESP8266 and ESP32
8+
9+
Connection:
10+
11+
BH1750 A:
12+
VCC -> 3V3 or 5V
13+
GND -> GND
14+
SCL -> SCL (19 in this example)
15+
SDA -> SDA (18 in this example)
16+
ADD -> (not connected) or GND
17+
18+
BH1750 B:
19+
VCC -> 3V3 or 5V
20+
GND -> GND
21+
SCL -> SCL (22 in this example)
22+
SDA -> SDA (21 in this example)
23+
ADD -> (not connected) or GND
24+
25+
ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
26+
0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
27+
0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
28+
be 0x23 (by default).
29+
30+
*/
31+
32+
#include "BH1750.h"
33+
#include "Wire.h"
34+
35+
BH1750 bh1750_a;
36+
BH1750 bh1750_b;
37+
38+
void setup() {
39+
Serial.begin(115200);
40+
Wire.begin(18, 19);
41+
Wire1.begin(21, 22);
42+
bh1750_a.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire);
43+
bh1750_b.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire1);
44+
}
45+
46+
int error_counter_1_a = 0;
47+
int error_counter_2_a = 0;
48+
int error_counter_1_b = 0;
49+
int error_counter_2_b = 0;
50+
51+
void loop() {
52+
const float light_level_a = bh1750_a.readLightLevel();
53+
const float light_level_b = bh1750_b.readLightLevel();
54+
55+
if (lround(light_level_a) == -1) {
56+
error_counter_1_a++;
57+
}
58+
if (lround(light_level_a) == -2) {
59+
error_counter_2_a++;
60+
}
61+
if (lround(light_level_b) == -1) {
62+
error_counter_1_b++;
63+
}
64+
if (lround(light_level_b) == -2) {
65+
error_counter_2_b++;
66+
}
67+
Serial.printf("A: %.0f lux %d:%d :: B: %.0f lux %d:%d\n", light_level_a,
68+
error_counter_1_a, error_counter_2_a, light_level_b,
69+
error_counter_1_b, error_counter_2_b);
70+
delay(1000);
71+
}

0 commit comments

Comments
 (0)