Environment Setup: Refer to Arduino IDE Getting Started Tutorial to complete IDE installation, and install the corresponding board manager and required driver libraries according to the development board you are using.
Libraries used:
Hardware products used:


#include <M5Unified.h>
#include <Wire.h>
uint16_t result = 0;
float temperature = 0;
void setup() {
// Initialization
auto cfg = M5.config();
M5.begin(cfg);
Wire.begin(0, 26); // Hat NCIR (SDA:G0, SCL:G26)
Serial.begin(115200);
// setup display
M5.Display.setRotation(1); // Depends on the direction in which the StickC_Plus2 is held
M5.Display.setTextColor(WHITE);
M5.Display.setTextSize(3);
Serial.println("StickC-Plus2 with Hat NCIR is ready!");
}
void loop() {
Wire.beginTransmission(0x5A); // Send initial signal and I2C bus address
Wire.write(0x07); // Send data only once and automatically add one address
Wire.endTransmission(false); // Stop signal
Wire.requestFrom(0x5A, 2); // Request 2 consecutive data bytes from 0x5A
if (Wire.available() >= 2) {
result = Wire.read(); // Receive DATA
result |= Wire.read() << 8; // Receive DATA
temperature = result * 0.02 - 273.15; // Convert sensor reading to Celsius degrees
// display on StickC-Plus2's screen
M5.Display.fillScreen(BLACK);
M5.Display.setCursor(60, 53);
M5.Display.printf("%.2fC", temperature);
// display on serial terminal
Serial.printf("Temperature: %.2f °C\n", temperature);
}
else {
Serial.println("Error reading from sensor");
}
delay(500);
M5.update();
}Enter download mode: Before flashing programs to different Stick series devices, the corresponding driver must be installed. Drivers and installation procedures vary depending on the controller in use. For detailed instructions, refer to the Arduino IDE Getting Started Tutorial page, particularly the device download tutorial section at the bottom, and check the specific operation steps for each device.
Select the device port, then click the compile & upload button at the top left of Arduino IDE, and wait until the program is compiled and uploaded to the device.
This program detects data from the Hat NCIR single-point infrared temperature sensor and displays it on both the screen of StickC-Plus2 and the serial monitor on the computer:
