pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Hat DLight Arduino Tutorial

1. Preparation

2. Example Program

Example Description
Hat DLight is a digital ambient light detection sensor compatible with the StickC series. It uses the BH1750FVI illuminance sensor IC (I2C interface), with a built-in 16-bit AD converter supporting 1 ~ 65535 lx illumination range detection. This example demonstrates how to use StickC-Plus2 to drive and control the Hat DLight through the I2C interface and print the data on the StickC-Plus2 screen as well as the computer’s serial monitor.

Complete Program

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <M5Unified.h>
#include <M5_DLight.h>

M5_DLight sensor;
uint16_t lux;       // store sensor reading

void setup() {
  // initialize M5StickC-Plus2
  auto cfg = M5.config();
  M5.begin(cfg);
  Serial.begin(115200);
  Serial.println("Initializing DLight sensor...");
  delay(2000);

  // initialize sensor
  sensor.begin(&Wire, 0, 26);   // Hat DLight (SDA:G0, SCL:G26)
  sensor.setMode(CONTINUOUSLY_H_RESOLUTION_MODE);

  // setup display
  M5.Display.setRotation(1);  // depends on the orientation of StickC_Plus2 being held
  M5.Display.setTextColor(GREEN);
  M5.Display.setTextSize(2);

  Serial.println("StickC-Plus2 with DLight is ready!");
}

void loop() {
  M5.update();

  lux = sensor.getLUX();  // get sensor reading

  // display on StickC-Plus2's screen
  M5.Display.startWrite();
  M5.Display.setCursor(64,55);
  M5.Display.fillScreen(BLACK); // clear screen
  M5.Display.printf("Lux: %d\n", lux);

  // display on serial terminal
  Serial.printf("Light Intensity: %d lux\n", lux);

  delay(1000);
}

3. Compile and Upload

  1. Enter Download Mode: Before uploading the program, different Stick devices require installation of the corresponding driver programs. The driver and installation steps may vary depending on the controller. Refer to the device download tutorial list at the bottom of the Arduino IDE Getting Started Tutorial page to find the specific procedure for your device.

  2. Select the device port, click the compile-and-upload button on the upper left corner of the Arduino IDE, and wait for the program to compile and upload to your device.

4. Ambient Light Sensor Data Display

This program reads the light intensity data from the Hat DLight sensor and displays it on the StickC-Plus2 screen and the computer’s serial monitor:

On This Page