🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    CoreS3 & NCIR 2 UNIT not reading sensor correctly

    Arduino
    2
    3
    287
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      reptilepvp
      last edited by

      I'm using Arduino IDE 2.3.4 & having the issue with the unit reading the temperature data. If I use UIFLOW the sensor works perfect but whenever I try to get it to read thru IDE. It constantly just reads 2784 degree's. If I use the user demos . It picks up the i2c addy as the default 0x5A. Please help if possible, this is my code .

      #include <M5CoreS3.h>
      #include <M5GFX.h>
      #include <M5UNIT_NCIR2.h>
      #include <M5Unified.h>
      #include <Preferences.h>
      #include <Wire.h>

      // Objects
      M5GFX display;
      M5UNIT_NCIR2 ncir2;
      Preferences preferences;

      int16_t temperature;
      int16_t temperature_soc;

      uint16_t low_alarm_freq, high_alarm_freq, low_alarm_interval,
      high_alarm_interval, buzz_freq;
      uint8_t low_alarm_duty, high_alarm_duty, duty;
      int16_t low_alarm_temp, high_alarm_temp;

      // Global Variables
      float emissivity = 0.95;
      float tempAlert = 30.0;
      int currentPage = 0;
      bool needRedraw = true; // Flag to control screen redraw

      #define M5UNIT_NCIR2_DEFAULT_ADDR 0x5A

      void setup() {
      auto cfg = M5.config();
      M5.begin(cfg);
      Serial.begin(115200); // Start serial communication
      ncir2.begin(&Wire,1, 2, M5UNIT_NCIR2_DEFAULT_ADDR);

      // Load settings from preferences
      preferences.begin("settings", true);
      emissivity = preferences.getFloat("emissivity", 0.95);
      tempAlert = preferences.getFloat("tempAlert", 30.0);
      preferences.end();

      M5.Display.fillScreen(TFT_BLACK);
      }

      void loop() {
      M5.update();

      // Update the display only if necessary
      if (needRedraw) {
      if (currentPage == 0) {
      drawMainMenu();
      } else if (currentPage == 1) {
      drawSettingsPage();
      }
      needRedraw = false; // Reset redraw flag
      }

      // Handle touch input
      if (currentPage == 0) {
      handleMainMenuTouch();
      } else if (currentPage == 1) {
      handleSettingsTouch();
      }
      }

      // Main Menu
      void drawMainMenu() {
      M5.Display.fillScreen(TFT_BLACK);
      M5.Display.setTextSize(2);
      M5.Display.setCursor(10, 10);
      M5.Display.print("NCIR2 Temp Monitor");

      float temperature = ncir2.getTempValue(); // Now this line will work
      Serial.printf("Temperature Read: %.2f C\n", temperature); // Debug print
      M5.Display.setCursor(10, 50);
      M5.Display.printf("Temp: %.2f C", temperature);

      // Draw Settings button
      M5.Display.fillRect(10, 100, 200, 50, TFT_BLUE);
      M5.Display.setCursor(30, 120);
      M5.Display.setTextColor(TFT_WHITE);
      M5.Display.print("Settings");
      }

      void handleMainMenuTouch() {
      auto detail = M5.Touch.getDetail();
      if (detail.wasPressed()) {
      int x = detail.x;
      int y = detail.y;

      // Check if "Settings" button is pressed
      if (x >= 10 && x <= 210 && y >= 100 && y <= 150) {
        currentPage = 1;  // Navigate to Settings Page
        needRedraw = true; // Set redraw flag
      }
      

      }
      }

      void drawSettingsPage() {
      M5.Display.fillScreen(TFT_BLACK);
      M5.Display.setTextSize(2);
      M5.Display.setCursor(10, 10);
      M5.Display.print("Settings");

      M5.Display.setCursor(10, 50);
      M5.Display.printf("Emissivity: %.2f", emissivity);
      M5.Display.setCursor(10, 80);
      M5.Display.printf("Alert Temp: %.2f C", tempAlert);

      // Draw adjustment buttons
      M5.Display.fillRect(10, 120, 80, 50, TFT_GREEN);
      M5.Display.setCursor(20, 140);
      M5.Display.setTextColor(TFT_WHITE);
      M5.Display.print("Emissivity +");

      M5.Display.fillRect(100, 120, 80, 50, TFT_RED);
      M5.Display.setCursor(110, 140);
      M5.Display.setTextColor(TFT_WHITE);
      M5.Display.print("Emissivity -");

      M5.Display.fillRect(10, 180, 80, 50, TFT_GREEN);
      M5.Display.setCursor(20, 200);
      M5.Display.setTextColor(TFT_WHITE);
      M5.Display.print("Temp +");

      M5.Display.fillRect(100, 180, 80, 50, TFT_RED);
      M5.Display.setCursor(110, 200);
      M5.Display.setTextColor(TFT_WHITE);
      M5.Display.print("Temp -");

      // Draw "Back" button
      M5.Display.fillRect(10, 250, 200, 50, TFT_BLUE);
      M5.Display.setCursor(30, 270);
      M5.Display.setTextColor(TFT_WHITE);
      M5.Display.print("Back");
      }

      void handleSettingsTouch() {
      auto detail = M5.Touch.getDetail();
      if (detail.wasPressed()) {
      int x = detail.x;
      int y = detail.y;

      // Check if "Back" button is pressed
      if (x >= 10 && x <= 210 && y >= 250 && y <= 300) {
        currentPage = 0;  // Navigate back to Main Menu
        needRedraw = true; // Set redraw flag
        return;
      }
      
      // Check if "+" buttons are pressed to adjust values
      if (x >= 10 && x <= 90 && y >= 120 && y <= 170) {
        emissivity += 0.01;  
        saveSettings();
        needRedraw = true; // Set redraw flag
      }
      
      if (x >= 100 && x <= 180 && y >= 120 && y <= 170) {
        emissivity -= 0.01;  
        saveSettings();
        needRedraw = true; // Set redraw flag
      }
      
      if (x >= 10 && x <= 90 && y >= 180 && y <= 230) {
        tempAlert += 1.0;  
        saveSettings();
        needRedraw = true; // Set redraw flag
      }
      
      if (x >= 100 && x <= 180 && y >= 180 && y <= 230) {
        tempAlert -= 1.0;  
        saveSettings();
        needRedraw = true; // Set redraw flag
      }
      

      }
      }

      void saveSettings() {
      preferences.begin("settings", false);
      preferences.putFloat("emissivity", emissivity);
      preferences.putFloat("tempAlert", tempAlert);
      preferences.end();
      }

      1 Reply Last reply Reply Quote 0
      • kurikoK
        kuriko
        last edited by

        @reptilepvp
        I don't quite understand, according to the document, the I2C address of NCIR2 is 0x5A
        Maybe you should try leaving only the code for reading the temperature?

        Good morning, and welcome to the Black Mesa Transit System.

        R 1 Reply Last reply Reply Quote 0
        • R
          reptilepvp @kuriko
          last edited by

          @kuriko i wanted the code to read in Fahrenheit . But also the issue is that it’s the reading the i2c connection but in UIFlow it does.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post