Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@

M5Stack-**Unit & Hat DLight** driver library, based on BH1750FVI sensor to achieve ambient light detection.

## Initialization

`begin()` now returns a `bool` that indicates whether the device acknowledged the `POWER_ON` command on the configured I2C address.

- `true`: a device acknowledged at the configured address, which is treated as sensor present
- `false`: no device acknowledged at the configured address

Existing code that ignores the return value will continue to work.

```cpp
M5_DLight sensor;

if (sensor.begin()) {
uint16_t lux = sensor.getLUX();
}
```

## Related Link

[Document & Datasheet - M5Unit-DLight](https://docs.m5stack.com/en/unit/dlight)
Expand Down
18 changes: 16 additions & 2 deletions examples/Hat_DLight_M5StickC/Hat_DLight_M5StickC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 获取更多资料请访问: http://docs.m5stack.com/zh_CN/hat/hat_dlight
*
* Product: Hat DLight.
* Date: 2023/05/29
* Date: 2026/04/21
*******************************************************************************
Measure and output light intensity on screen and serial port
测量并在屏幕和串口输出光照强度
Expand All @@ -21,6 +21,7 @@ M5GFX display;
M5Canvas canvas(&display);

M5_DLight sensor;
bool sensorPresent = false;
uint16_t lux;

void setup() {
Expand All @@ -34,7 +35,15 @@ void setup() {
canvas.createSprite(display.width(), display.height());
canvas.setPaletteColor(1, ORANGE);
Serial.println("Sensor begin.....");
sensor.begin(&Wire, 0, 26); // HAT DLight
sensorPresent = sensor.begin(&Wire, 0, 26); // HAT DLight

if (!sensorPresent) {
Serial.println("Sensor not found");
canvas.fillSprite(BLACK);
canvas.drawString("Sensor not found", 80, 40);
canvas.pushSprite(0, 0);
return;
}

// CONTINUOUSLY_H_RESOLUTION_MODE
// CONTINUOUSLY_H_RESOLUTION_MODE2
Expand All @@ -48,6 +57,11 @@ void setup() {
char info[40];

void loop() {
if (!sensorPresent) {
delay(100);
return;
}

lux = sensor.getLUX();
sprintf(info, "lux: %d", lux);
canvas.fillSprite(BLACK);
Expand Down
18 changes: 16 additions & 2 deletions examples/Hat_DLight_M5StickCPlus/Hat_DLight_M5StickCPlus.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 获取更多资料请访问: http://docs.m5stack.com/zh_CN/hat/hat_dlight
*
* Product: Hat DLight.
* Date: 2022/07/27
* Date: 2026/04/21
*******************************************************************************
Measure and output light intensity on screen and serial port
测量并在屏幕和串口输出光照强度
Expand All @@ -21,6 +21,7 @@ M5GFX display;
M5Canvas canvas(&display);

M5_DLight sensor;
bool sensorPresent = false;
uint16_t lux;

void setup() {
Expand All @@ -34,7 +35,15 @@ void setup() {
canvas.createSprite(display.width(), display.height());
canvas.setPaletteColor(1, ORANGE);
Serial.println("Sensor begin.....");
sensor.begin(&Wire, 0, 26); // HAT DLight
sensorPresent = sensor.begin(&Wire, 0, 26); // HAT DLight

if (!sensorPresent) {
Serial.println("Sensor not found");
canvas.fillSprite(BLACK);
canvas.drawString("Sensor not found", 60, 60);
canvas.pushSprite(0, 0);
return;
}

// CONTINUOUSLY_H_RESOLUTION_MODE
// CONTINUOUSLY_H_RESOLUTION_MODE2
Expand All @@ -48,6 +57,11 @@ void setup() {
char info[40];

void loop() {
if (!sensorPresent) {
delay(100);
return;
}

lux = sensor.getLUX();
sprintf(info, "lux: %d", lux);
canvas.fillSprite(BLACK);
Expand Down
16 changes: 14 additions & 2 deletions examples/Unit_DLight_M5Atom/Unit_DLight_M5Atom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/dlight
*
* Product: Unit DLight
* Date: 2023/05/29
* Date: 2026/04/21
*******************************************************************************
Please connect the sensor to port, the Lux value will be output
on the serial after successful initialization
Expand All @@ -18,12 +18,19 @@
#include <M5_DLight.h>

M5_DLight sensor;
bool sensorPresent = false;
uint16_t lux;

void setup() {
M5.begin(true, true, true);
Serial.println("Sensor begin.....");
sensor.begin();
sensorPresent = sensor.begin();

if (!sensorPresent) {
Serial.println("Sensor not found");
M5.dis.fillpix(0xff0000);
return;
}

// CONTINUOUSLY_H_RESOLUTION_MODE
// CONTINUOUSLY_H_RESOLUTION_MODE2
Expand All @@ -35,6 +42,11 @@ void setup() {
}

void loop() {
if (!sensorPresent) {
delay(200);
return;
}

M5.dis.fillpix(0x00ff00);
lux = sensor.getLUX();
Serial.printf("lux: %d\n", lux);
Expand Down
18 changes: 16 additions & 2 deletions examples/Unit_DLight_M5Core/Unit_DLight_M5Core.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/dlight
*
* Describe: Unit DLight
* Date: 2023/5/29
* Date: 2026/04/21
*******************************************************************************
Please connect the sensor to port A (22, 21), the Lux value will be displayed
on the display after successful initialization
Expand All @@ -22,6 +22,7 @@ M5GFX display;
M5Canvas canvas(&display);

M5_DLight sensor;
bool sensorPresent = false;
uint16_t lux;

void setup() {
Expand All @@ -34,7 +35,15 @@ void setup() {
canvas.createSprite(display.width(), display.height());
canvas.setPaletteColor(1, ORANGE);
Serial.println("Sensor begin.....");
sensor.begin();
sensorPresent = sensor.begin();

if (!sensorPresent) {
Serial.println("Sensor not found");
canvas.fillSprite(BLACK);
canvas.drawString("Sensor not found", 160, 120);
canvas.pushSprite(0, 0);
return;
}

// CONTINUOUSLY_H_RESOLUTION_MODE
// CONTINUOUSLY_H_RESOLUTION_MODE2
Expand All @@ -48,6 +57,11 @@ void setup() {
char info[40];

void loop() {
if (!sensorPresent) {
delay(100);
return;
}

lux = sensor.getLUX();
sprintf(info, "lux: %d", lux);
canvas.fillSprite(BLACK);
Expand Down
18 changes: 16 additions & 2 deletions examples/Unit_DLight_M5Core2/Unit_DLight_M5Core2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/dlight
*
* Product: Unit DLight
* Date: 2022/7/27
* Date: 2026/04/21
*******************************************************************************
Please connect the sensor to port A (32,33), the Lux value will be displayed
on the display after successful initialization 请将传感器连接至端口A(32,33),
Expand All @@ -22,6 +22,7 @@ M5GFX display;
M5Canvas canvas(&display);

M5_DLight sensor;
bool sensorPresent = false;
uint16_t lux;

void setup() {
Expand All @@ -34,7 +35,15 @@ void setup() {
canvas.createSprite(display.width(), display.height());
canvas.setPaletteColor(1, ORANGE);
Serial.println("Sensor begin.....");
sensor.begin();
sensorPresent = sensor.begin();

if (!sensorPresent) {
Serial.println("Sensor not found");
canvas.fillSprite(BLACK);
canvas.drawString("Sensor not found", 160, 120);
canvas.pushSprite(0, 0);
return;
}

// CONTINUOUSLY_H_RESOLUTION_MODE
// CONTINUOUSLY_H_RESOLUTION_MODE2
Expand All @@ -48,6 +57,11 @@ void setup() {
char info[40];

void loop() {
if (!sensorPresent) {
delay(100);
return;
}

lux = sensor.getLUX();
sprintf(info, "lux: %d", lux);
canvas.fillSprite(BLACK);
Expand Down
18 changes: 16 additions & 2 deletions examples/Unit_DLight_M5StickC/Unit_DLight_M5StickC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/dlight
*
* Product: Unit DLight
* Date: 2022/7/27
* Date: 2026/04/21
*******************************************************************************
Please connect the sensor to port, the Lux value will be displayed
on the display after successful initialization 请将传感器连接至端口,
Expand All @@ -22,6 +22,7 @@ M5GFX display;
M5Canvas canvas(&display);

M5_DLight sensor;
bool sensorPresent = false;
uint16_t lux;

void setup() {
Expand All @@ -35,7 +36,15 @@ void setup() {
canvas.createSprite(display.width(), display.height());
canvas.setPaletteColor(1, ORANGE);
Serial.println("Sensor begin.....");
sensor.begin();
sensorPresent = sensor.begin();

if (!sensorPresent) {
Serial.println("Sensor not found");
canvas.fillSprite(BLACK);
canvas.drawString("Sensor not found", 80, 40);
canvas.pushSprite(0, 0);
return;
}

// CONTINUOUSLY_H_RESOLUTION_MODE
// CONTINUOUSLY_H_RESOLUTION_MODE2
Expand All @@ -49,6 +58,11 @@ void setup() {
char info[40];

void loop() {
if (!sensorPresent) {
delay(100);
return;
}

lux = sensor.getLUX();
sprintf(info, "lux: %d", lux);
canvas.fillSprite(BLACK);
Expand Down
18 changes: 16 additions & 2 deletions examples/Unit_DLight_M5StickCPlus/Unit_DLight_M5StickCPlus.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/dlight
*
* Product: Unit DLight
* Date: 2022/7/27
* Date: 2026/04/21
*******************************************************************************
Please connect the sensor to port, the Lux value will be displayed
on the display after successful initialization 请将传感器连接至端口,
Expand All @@ -22,6 +22,7 @@ M5GFX display;
M5Canvas canvas(&display);

M5_DLight sensor;
bool sensorPresent = false;
uint16_t lux;

void setup() {
Expand All @@ -35,7 +36,15 @@ void setup() {
canvas.createSprite(display.width(), display.height());
canvas.setPaletteColor(1, ORANGE);
Serial.println("Sensor begin.....");
sensor.begin();
sensorPresent = sensor.begin();

if (!sensorPresent) {
Serial.println("Sensor not found");
canvas.fillSprite(BLACK);
canvas.drawString("Sensor not found", 80, 60);
canvas.pushSprite(0, 0);
return;
}

// CONTINUOUSLY_H_RESOLUTION_MODE
// CONTINUOUSLY_H_RESOLUTION_MODE2
Expand All @@ -49,6 +58,11 @@ void setup() {
char info[40];

void loop() {
if (!sensorPresent) {
delay(100);
return;
}

lux = sensor.getLUX();
sprintf(info, "lux: %d", lux);
canvas.fillSprite(BLACK);
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "git",
"url": "https://github.com/m5stack/M5-DLight.git"
},
"version": "0.0.5",
"version": "0.0.6",
"frameworks": "arduino",
"platforms": "espressif32"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=M5-DLight
version=0.0.5
version=0.0.6
author=M5Stack
maintainer=M5Stack
sentence=Library for M5Stack Unit & HAT DLight
Expand Down
12 changes: 6 additions & 6 deletions src/M5_DLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ M5_DLight::M5_DLight(uint8_t addr)
}

/*! @brief Initialize the EXTIO2.*/
void M5_DLight::begin(TwoWire *wire, uint8_t sda, uint8_t scl, uint32_t freq)
bool M5_DLight::begin(TwoWire *wire, uint8_t sda, uint8_t scl, uint32_t freq)
{
_wire = wire;
_sda = sda;
_scl = scl;
_freq = freq;
_wire->begin((int)_sda, (int)_scl, _freq);
powerOn();
return powerOn();
}

/*! @brief Write data to the register address. */
void M5_DLight::writeByte(byte cmd)
bool M5_DLight::writeByte(byte cmd)
{
_wire->beginTransmission(_addr);
_wire->write(cmd);
_wire->endTransmission();
return _wire->endTransmission() == 0;
}

/*! @brief Write the specified length of data to the register.*/
Expand Down Expand Up @@ -53,9 +53,9 @@ uint16_t M5_DLight::getLUX()
}

/*! @brief Turn on the power.*/
void M5_DLight::powerOn()
bool M5_DLight::powerOn()
{
writeByte(POWER_ON);
return writeByte(POWER_ON);
}

/*! @brief Turn off the power.*/
Expand Down
Loading