Arduino code for PaHuB2 + any I2C sensor



  • Dear all,

    I looking for the sample code reading the values from the I2C module (like ENV III) that is connected to the PaHUB2 socket 0 (PaHUB2 has I2C address 0x71).

    Thank you.



  • Dear all,
    I'm looking for the sample Arduino code to read the ENV III module that is connected to the PaHUB2 socket 0 (PaHUB2 has an I2C address 0x71).

    Thank you.



  • @ektan Sorry, this took me all day to write and de-bug.
    It is an ENVIII on position 0 and ENVII on position 1, as that is what I have!

    #include <M5Stack.h>  //use any controller by substituting the library
    #include "M5_ENV.h"
    SHT3X sht30;  // creates ONE instance!!!
    float tmp1 = 12.3;
    float hum1 = 0.0;
    float tmp2 = 12.3;
    float hum2 = 0.0;
    
    void setup() {
      M5.begin();
      M5.Power.begin();
      delay(100);
      Wire.begin();
      sht30.init();
      Wire.beginTransmission(0x70);  //(the PaHub)
      Wire.write(0x00);              //turns off all ports
      Wire.endTransmission();        // as it says!
      M5.Lcd.setTextSize(3);         //or lower for StickC
      M5.Lcd.setTextColor(RED);
      M5.Lcd.print("Setup  ");
      delay(1000);
    }
    void loop() {
      Wire.beginTransmission(0x70);  //(the PaHub)
      Wire.write(0x01);              //turns on port 0, ENV1
      Wire.endTransmission();
      if (sht30.get() == 0) {   // Obtain the data of shT30.  
        tmp1 = sht30.cTemp;     // Store the temperature obtained from shT30.
        hum1 = sht30.humidity;  // Store the humidity obtained from the SHT30.
                 } else {
        tmp1 = 0, hum1 = 0;
      }
      delay(100);
      Wire.beginTransmission(0x70);  //(the PaHub)
      Wire.write(0x02);              //reads the ENV at port 1
      Wire.endTransmission();
      if (sht30.get() == 0) {   // Obtain the data of shT30. 
        tmp2 = sht30.cTemp;     // Store the temperature obtained from shT30.
        hum2 = sht30.humidity;  // Store the humidity obtained from the SHT30.
        } else {
        tmp2 = 0, hum2 = 0;
      }
      delay(100);
      M5.Lcd.fillRect(0, 0, 320, 80, BLACK);
      M5.Lcd.setCursor(5, 5);
      M5.Lcd.print("Temp1=  ");
      M5.Lcd.setCursor(130, 5);
      M5.Lcd.print(tmp1, 1);  //one decimal place
      M5.Lcd.setCursor(5, 50);
      M5.Lcd.print("Temp2=  ");
      M5.Lcd.setCursor(130, 50);
      M5.Lcd.print(tmp2, 1);  //one decimal place
      delay(1000);
    }
    

    There is no PaHub library because the Wire C-code is in-line with the Arduino code.
    This is actually easier to understand and illustrates how the PaHub works.
    -Terry



  • @teastain

    Thank you so much for the help.
    It works perfectly...



  • @teastain In Arduino you can uise the PCA9548 library .
    Example : https://randomnerdtutorials.com/tca9548a-i2c-multiplexer-esp32-esp8266-arduino/