SHT31-D and an MKR1000 - Temp and RH off the same I2C device

Hello!

I have an Arduino project that was on a Mega, and I’m moving onto an MKR1000. The sensor I’m using (an SHT31-D Relative Humidity and Temperature Sensor on an Adafruit breakout board, link here) has a simple example sketch, shown below:

#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

Adafruit_SHT31 sht31 = Adafruit_SHT31();

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB
#endif

void setup() {
#ifndef ESP8266
  while (!Serial);     // will pause Zero, Leonardo, etc until serial console opens
#endif
  Serial.begin(9600);
  Serial.println("SHT31 test");
  if (! sht31.begin(0x44)) {   // Set to 0x45 for alternate i2c addr
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }
}


void loop() {
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();

  if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.println(t);
  } else { 
    Serial.println("Failed to read temperature");
  }
  
  if (! isnan(h)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h);
  } else { 
    Serial.println("Failed to read humidity");
  }
  Serial.println();
  delay(1000);
}

I want to add separate displays for both T and RH. I’ve seen in the forum that I2C communication works, but I’m not sure how to isolate 2 streams of data from the same device. And how much of this code must be included alongside Blynk.run()? I’m not sure how much code is written by the app and how much groundwork needs to be done in the IDE (for example, if I need to define t and h as in the above sketch before connecting to Blynk)

Actually this is not directly related with Blynk.
You can use multiple i2c devices on the same bus but you must adjust the device addresses so they are not collide. Most devices has pins or at least some joints that you can cut or solder, so that you can change i2c address. So you can connect two screen. You can print your t and rh to single screen also. 1602 lcd is more than enough.
To Blinkfy your project is another subject but it is not so difficult when you start.

Look virtual pin examples for starting point.

Thank you for your reply! I’m sorry I wasn’t clear at first though - I meant two displays on the Blynk control panel from the same single sensor, not physical LCD displays.

Can I assign 2 addresses to this single sensor then - one for Temp and one for RH - and would that help?

I’m reading through virtual pins now, and I can see roughly how I2C would work on them. I’m still wondering how I can differentiate between the two streams of data coming down the same pipe with virtual pins.

Sorry I’m not understanding just yet

Of course you can with virtual pins.
For example create a subroutine called UPDATE. Set temp to V1 and set relative humidity to V2 in this subroutine. Call this subroutine every 10 sec with simple timer library and on Blynk app side set these virtual pins to display widgets.

1 Like

Ah, okay. Thank you, I was confused!