
Arduino Quick Start
Environment configuration: Refer to Arduino IDE Getting Started Tutorial to complete IDE installation, and install the appropriate board manager and required driver libraries based on the development board you are using.
Libraries used:
Hardware used:


#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);
}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.
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.
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:
