
Arduino 上手教程
环境配置: 参考Arduino IDE上手教程完成 IDE 安装,并根据实际使用的开发板安装对应的板管理,与需要的驱动库。
使用到的驱动库:
使用到的硬件产品:


#include <M5Unified.h>
#include <M5_DLight.h>
M5_DLight sensor;
uint16_t lux; // store sensor reading
void setup() {
// initializatize 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 direction of the 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);
}进入下载模式:不同的 Stick 设备进行程序烧录前需要安装对应的驱动程序,不同的主控设备使用的驱动与安装步骤可能有所不同。详情可参考Arduino IDE上手教程页面底部的设备程序下载教程列表,查看具体设备对应的操作方式。
选中设备端口,点击 Arduino IDE 左上角编译上传按钮,等待程序完成编译并上传至设备。
该程序将检测 Hat DLight 的光检测传感器的数据,并在 StickC-Plus2 的屏幕和电脑的串口监视器打印数据:
