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

    Unable to read temperature value from SHT30

    General
    4
    11
    2.9k
    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.
    • S
      Surya
      last edited by

      I'm attempting to read temperature values from the SHT30 (Yanmis SHT30) sensor. However, I'm encountering an error mentioned below.
      0_1712233103960_763d9f73-e78a-42da-b5af-1c34b6a137ed-image.png

      Can someone kindly provide me with guidance or solutions to resolve the issue I'm facing?

      Here is my setup:
      0_1712233338034_8723dcd8-4006-4b91-8fe6-dadc0d4ebd4b-image.png

      Code:
      https://github.com/RobTillaart/SHT31/blob/master/examples/SHT31_rawValues/SHT31_rawValues.ino

      #include <M5AtomS3.h>
      #include <M5Unified.h>
      #include "Wire.h"
      #include "Wire.h"
      #include "SHT31.h"
      
      #define SHT31_ADDRESS   0x44
      
      uint32_t start;
      uint32_t stop;
      uint32_t cnt;
      
      SHT31 sht(SHT31_ADDRESS);  //  uses explicit address
      
      
      void setup()
      {
        Serial.begin(115200);
        Wire.begin();
        Wire.setClock(100000);
        sht.begin();
        uint16_t stat = sht.readStatus();
          
        sht.requestData();
        cnt = 0;
      }
      
      void loop()
      {
        uint16_t rawTemperature;
        uint16_t rawHumidity;
      
        if (sht.dataReady())
        {
          start = micros();
          bool success  = sht.readData();   //  default = true = fast
          stop = micros();
          sht.requestData();                //  request for next sample
      
          Serial.print("\t");
          Serial.print(stop - start);
          Serial.print("\t");
          if (success == false)
          {
            Serial.println("Failed read");
          }
          else
          {
            rawTemperature = sht.getRawTemperature();
            rawHumidity = sht.getRawHumidity();
            Serial.print(rawTemperature, HEX);
            Serial.print(" = ");
      
            //  This formula comes from page 14 of the SHT31 datasheet
            Serial.print(rawTemperature * (175.0 / 65535) - 45, 1); 
            Serial.print("°C\t");
            Serial.print(sht.getRawHumidity(), HEX);
            Serial.print(" = ");
            
            //  This formula comes from page 14 of the SHT31 datasheet
            Serial.print(rawHumidity * (100.0 / 65535), 1); 
            Serial.print("%\t");
            Serial.println(cnt);
            cnt = 0;
          }
        }
        cnt++;  //  simulate other activity
        delay(1000);
      }
      1 Reply Last reply Reply Quote 0
      • felmueF
        felmue
        last edited by

        Hello @Surya

        maybe checkout this example here. It uses an SHT30 as well.

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        1 Reply Last reply Reply Quote 1
        • teastainT
          teastain
          last edited by teastain

          @surya The alternate I2C address is 45, some suppliers set it to this by default.
          Alternately you could try an sketch called an I2C scanner to see what devices are hooked up.
          This little sketch works on my AtomS3:

          #include "M5UnitENV.h"
          SHT3X sht3x;
          
          void setup() {           //runs once on start up
            Serial.begin(115200);  //open the serial port for USB cable
            delay(500);
            Serial.println("in setup");
            delay(1000);  //wait for a second
          }
          
          void loop() {  
            if (sht3x.begin(&Wire, 0x44, 2, 1, 400000U)) {
               Serial.println("Address 44 detected");
            } else if (sht3x.begin(&Wire, 0x45, 2, 1, 400000U)) {
              Serial.println("Address 45 detected");
            } else {
              Serial.println("No ENV detected");
            }
          

          -Terry

          Cheers, Terry!

          100% M5Stack addict with several drawers full of product!

          1 Reply Last reply Reply Quote 1
          • S
            Surya
            last edited by

            Thank you, @felmue and @teastain, I will give it a try.

            1 Reply Last reply Reply Quote 0
            • S
              Surya
              last edited by

              The SHT30 perfectly worked with "M5UnitENV.h". Thanks again!

              Here I am encountering another challenge: I'm attempting to read temperature data from both the SHT30 and SHT40 simultaneously. However, both sensors share the same I2C address (0x44), leading to an error. I also experimented with changing the address to 0x45, but unfortunately, it didn't resolve the issue. Any advice is greatly appreciated.

              Here is my new setup:
              0_1712310307256_9d3090af-c3a3-4636-86ec-e0b98cbdef77-image.png

              Code:

              #include <M5AtomS3.h>
              #include <M5Unified.h>
              #include "Wire.h"
              #include "M5UnitENV.h"
              #include <SensirionI2CSht4x.h>
              
              SHT3X sht3x;
              SensirionI2CSht4x sht4x;
              
              float temperature, humidity;
              
              void setup()
              {
                Serial.begin(115200);
              
                if (!sht3x.begin(&Wire, SHT3X_I2C_ADDR, 2, 1, 400000U))
                {
                  Serial.println("Couldn't find SHT3X");
                  // while (1)
                  // {
                  //   Serial.println("Couldn't find SHT3X");
                  //   delay(1000);
                  // }
                }
                sht4x.begin(Wire, SHT40_I2C_ADDR_45);
              }
              
              void loop()
              {
                temperature = humidity = NULL;
                uint16_t error;
                char errorMessage[256];
                if (sht3x.update())
                {
                  Serial.println("-----SHT3X-----");
                  Serial.print("Temperature: ");
                  Serial.print(sht3x.cTemp);
                  Serial.println(" degrees C");
                  Serial.print("Humidity: ");
                  Serial.print(sht3x.humidity);
                  Serial.println("% rH");
                  Serial.println("-------------\r\n");
                }
              
                error = sht4x.measureHighPrecision(temperature, humidity);
                if (error)
                {
                  Serial.print("Error trying to execute measureHighPrecision(): ");
                  errorToString(error, errorMessage, 256);
                  Serial.println(errorMessage);
                }
                else
                {
                  Serial.println("-----SHT4X-----");
                  Serial.print("Temperature:");
                  Serial.print(temperature);
                  Serial.print("\t");
                  Serial.print("Humidity:");
                  Serial.println(humidity);
                  Serial.println("-------------\r\n");
                }
                delay(1000);
              }
              

              Thanks
              Surya

              ajb2k3A 1 Reply Last reply Reply Quote 0
              • ajb2k3A
                ajb2k3 @Surya
                last edited by

                @surya if you can’t physically change the addresses then you will need an I2C hub which overrides the I2C internal address

                UIFlow, so easy an adult can learn it!
                If I don't know it, be patient!
                I've ether not learned it or am too drunk to remember it!
                Author of the WIP UIFlow Handbook!
                M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

                1 Reply Last reply Reply Quote 1
                • S
                  Surya
                  last edited by

                  I'm just curious, Is there a way to accomplish this programmatically, without needing to physically change the addresses?

                  Thanks
                  Surya

                  ajb2k3A teastainT 2 Replies Last reply Reply Quote 0
                  • ajb2k3A
                    ajb2k3 @Surya
                    last edited by

                    @surya Generally not as often the soldering of the devices pins is what's used to set the address.

                    UIFlow, so easy an adult can learn it!
                    If I don't know it, be patient!
                    I've ether not learned it or am too drunk to remember it!
                    Author of the WIP UIFlow Handbook!
                    M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

                    1 Reply Last reply Reply Quote 0
                    • teastainT
                      teastain @Surya
                      last edited by

                      @surya You need a PaHub:
                      https://docs.m5stack.com/en/unit/pahub2

                      https://shop.m5stack.com/products/i2c-hub-1-to-6-expansion-unit-pca9548apw

                      They are easy to set up and work very well.

                      Cheers, Terry!

                      100% M5Stack addict with several drawers full of product!

                      1 Reply Last reply Reply Quote 0
                      • felmueF
                        felmue
                        last edited by

                        Hello @Surya

                        neither SHT30 nor SHT40 allow for an I2C address change programmatically. The I2C address of SHT30 can be changed via ADDR pin; SHT40 is sold in two variants with two I2C addresses. Please also see my post here.

                        Thanks
                        Felix

                        GPIO translation table M5Stack / M5Core2
                        Information about various M5Stack products.
                        Code examples

                        1 Reply Last reply Reply Quote 0
                        • S
                          Surya
                          last edited by

                          Okay, We're gonna buy PaHub to make it work.

                          Thank you all for your invaluable solutions and clarifications on my issue. Your help is greatly appreciated!

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