pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Hat NCIR Arduino Tutorial

1. Preparation

2. Example Program

Example Description
Hat NCIR is a single-point infrared temperature measurement sensor compatible with the StickC series. It features a built-in infrared sensor MLX90614, with a temperature measurement range of -70°C ~ +380°C and a field of view of 90°. This example uses the StickC-Plus2 to drive and control the Hat NCIR through the I2C interface and display sensor data on both the screen of the StickC-Plus2 and the serial monitor on the computer.

Complete Code

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 43 44 45 46 47 48
#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();
}

3. Compile and Upload

  1. 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.

  2. 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.

4. Infrared Temperature Sensor Data Display

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:

On This Page