-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Description
The readReg() method in DFRobot_EnvironmentalSensor.cpp calls _pWire->begin() every time a register is read. This causes repeated "Bus already started in Master Mode" warnings on ESP32 platforms, especially when reading multiple sensor values (temperature, humidity, UV, light, pressure, elevation).
Current Behavior
Every sensor reading triggers a Wire.begin() call, resulting in console warnings:
[30000][W][Wire.cpp:300] begin(): Bus already started in Master Mode.
[30007][V][esp32-hal-i2c-ng.c:272] i2cWrite(): i2c_master_transmit: bus=0 addr=0x22
[30017][V][esp32-hal-i2c-ng.c:318] i2cRead(): i2c_master_receive: bus=0 addr=0x22
[30027][W][Wire.cpp:300] begin(): Bus already started in Master Mode.
[30033][V][esp32-hal-i2c-ng.c:272] i2cWrite(): i2c_master_transmit: bus=0 addr=0x22
[30043][V][esp32-hal-i2c-ng.c:318] i2cRead(): i2c_master_receive: bus=0 addr=0x22
When reading all environmental values in update(), this generates 6 consecutive warnings (one per sensor reading).
Expected Behavior
Wire.begin() should only be called once during initialization, not on every register read. I2C bus initialization is the responsibility of the main application, not the library.
Proposed Solution
Remove the _pWire->begin(); call from readReg():
uint8_t DFRobot_EnvironmentalSensor::readReg(uint16_t reg, void *pBuf, uint8_t size)
{
uint8_t* _pBuf = (uint8_t*)pBuf;
if(pBuf == NULL){
DBG("data error");
return 0;
}
if(_pWire){
uint8_t _reg = reg * 2;
// Wire.begin() should be called once by the user in their setup()
// _pWire->begin(); // ← Remove this line
_pWire->beginTransmission(_addr);
_pWire->write(_reg);
_pWire->endTransmission();
_pWire->requestFrom(_addr, size);
for(uint8_t i = 0; i < size; i++)
_pBuf[i] = _pWire->read();
return size;
}else{
return readInputRegister(_addr, reg, _pBuf, size);
}
}
Reproduction
#include "DFRobot_EnvironmentalSensor.h"
DFRobot_EnvironmentalSensor sensor(SEN050X_DEFAULT_DEVICE_ADDRESS, &Wire);
void setup() {
Serial.begin(115200);
Wire.begin();
sensor.begin();
}
void loop() {
// Each of these calls triggers a Wire.begin() warning
float temp = sensor.getTemperature(TEMP_C);
float humidity = sensor.getHumidity();
float uv = sensor.getUltravioletIntensity();
float lux = sensor.getLuminousIntensity();
uint16_t pressure = sensor.getAtmospherePressure(HPA);
float elevation = sensor.getElevation();
delay(1000);
}
Console output: 6 "Bus already started" warnings per loop iteration.
Workaround
Users can currently patch the library locally by commenting out the _pWire->begin(); line in DFRobot_EnvironmentalSensor.cpp. The sensor works perfectly without this line since Wire.begin() is already called in the user's setup code.
Additional Context
The constructor already accepts a TwoWire *pWire pointer, implying the user is responsible for initializing the I2C bus before passing it to the library. The redundant begin() call in readReg() contradicts this design pattern.
Environment
- Platform: ESP32 (also affects other Arduino platforms)
- Arduino Framework: 2.x / 3.x
- Library Version: Latest (tested with 1.1)
- Sensor: SEN0501 Multifunctional Environmental Sensor