How to read from two sensors sht21&sht31

Hi everybody,

I have Mega 2560 board and I can read data from sht31 with built in scetch SHT31test. Now i try to read two sht 21 & sht31 via i2c but can't find apropriate sketch. Please help me ,if someone knows how to do that .

I have Mega 2560 board and I can read data from sht31 with built in scetch SHT31test.

There is no built-in sketch with that name but the Adafruit library for the SHT31 comes with an example with that name.

Now i try to read two sht 21 & sht31 via i2c but can't find apropriate sketch.

You might use that library to read the SHT21. Fortunately for you is that both sensors have different I2C addresses so you can have them connected to the same Arduino without a need for a bus multiplexor.

Thank you for quick answers.Indeed that is true pylon, but my problem is how to read sht21 & sht31 via i2c. I can read either sht21 or sht 31 with theirs libraries ,but not both sensors.I have to make a sketch to read both of them.

Have a nice day! Nick

Here’s the answer of my question,compiling sht21 & sht 31 libraries result is the following sketch

#include <Sodaq_SHT2x.h>

#include <Wire.h>

#include <Adafruit_SHT31.h>
Adafruit_SHT31 sht31 = Adafruit_SHT31();

int address1 =64; //decimal address of sensor 1

int address2 = 68; //decimal address of sensor 2

void setup() {
Serial.begin(9600);
Wire.begin(); // create a wire object
sht31.begin(0x44);

}

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

Serial.print("Sensor 1: ");
Serial.print(t);
Serial.print("C | ");
Serial.print(“Sensor 1: “);
Serial.print(h);
Serial.println(”%”);
Serial.print("Sensor 2: “);
Serial.print(SHT2x.GetHumidity());
Serial.print(” Sensor 2: ");
Serial.println(SHT2x.GetTemperature());
delay(1000);
}

Hallo,

I have a spare sht31 ,and because this sensor have two address lines 0x44 & 0x45, it is possible to connect to others two in the previous example.Is anybody have some idea how to do that?

Nick

I have a spare sht31 ,and because this sensor have two address lines 0x44 & 0x45, it is possible to connect to others two in the previous example.Is anybody have some idea how to do that?

Yes, you can do that. Just pull pin 2 high and make another Adafruit_SHT31 object but initialize that with the address 0x45. Connect the rest of the pins exactly as you've done with the first SHT31.

If you don't use the chip but some breakout board you have to check the pull-up resistors. You should have the pull-ups only once per bus and not on every device otherwise the current might get to big and destroy your chips silently.

Thank you pylon,

My sht31 is chip on stick ,and to pull pin 2 high means to connect it with Vdd or pin 5.it s not easy but I have to try.

Nick

My sht31 is chip on stick

Then post a link to that board. If possible also post a link to it's schematics.

to pull pin 2 high means to connect it with Vdd or pin 5

Yes. If you use a board like the one from Adafruit, pin 2 is called ADR on the header.

Hello again,

I connected a new sht31 with address 0x45 on i2c bus with another one with 0x44 address.After adding a new lines in code above the readings from these two sensors change simultaneously when i touch only one of them.
Something is wrong but i dont know what is it? Here the new code

#include <Sodaq_SHT2x.h>

#include <Wire.h>

#include <Adafruit_SHT31.h>
Adafruit_SHT31 sht31 = Adafruit_SHT31();

int address1 =64; //decimal address of sensor 1
int address2 = 68; //decimal address of sensor 2
int address3 = 65; //decimal address of sensor 3
void setup() {
Serial.begin(9600);
Wire.begin(); // create a wire object
sht31.begin(0x44);
sht31.begin(0x45);
}

void loop() {
float t = sht31.readTemperature();
float h = sht31.readHumidity();
float d = sht31.readTemperature();
float e = sht31.readHumidity();
Serial.print("Sensor 1: ");
Serial.print(t);
Serial.print("C | ");
Serial.print(“Sensor 1: “);
Serial.print(h);
Serial.println(”%”);
Serial.print("Sensor 3: ");
Serial.print(d);
Serial.print("C | “);
Serial.print(“Sensor 3: “);
Serial.print(e);
Serial.print(”%”);
Serial.print(“Sensor 2: “);
Serial.print(SHT2x.GetHumidity());
Serial.print(”%”);
Serial.print(” Sensor 2: ");
Serial.print(SHT2x.GetTemperature());
Serial.println("C | ");

delay(1000);
}

I’m using a new variables d & e for readings from new sht31 (0x45)
Please,help me to fix the code so it can work correctly.

You cannot use the same object for both sensors. Create one object per sensor and assign each object a different address.

Hello,

I’m trying to solve how to connect 2x SHT31 (or even more), but with IP scanner they both have x44 address.

If i connect SHT21 (x40) and SHT(x44) in one code it works fine, but i’m not able to add 2nd SHT31

Can i somehow change the x44 address?

Thanks

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

Adafruit_SHT31 sht31 = Adafruit_SHT31();
SHT21 sht;

void setup() {
Wire.begin(); 
sht31.begin(0x44);
Serial.begin(9600);
Serial.println("Loading... ");

  while (!Serial)
    delay(10);     // will pause Zero, Leonardo, etc until serial console opens
}

void loop() {
float T2 = sht.getTemperature();       //To get the temperature and humidity values and store them in their respective variable
float H2 = sht.getHumidity();
float T3 = sht31.readTemperature();
float H3 = sht31.readHumidity();

  Serial.print("SHT21 Temperature is: ");
  Serial.print(T2);
  Serial.print(" °");
  // Serial.print((char)247);  //223 
  Serial.print("C");
  Serial.print(" | ");
  Serial.print("SHT21 Humidity is: ");
  Serial.print(H2);
  Serial.println(" %\t");
  Serial.print("SHT31 Temperature is: ");
  Serial.print(T3);
  Serial.print(" °");
  // Serial.print((char)176);
  Serial.print("C");
  Serial.print(" | ");
  Serial.print("SHT31 Humidity is: ");
  Serial.print(H3);
  Serial.println(" %\t");

delay(1000);

}

Can i somehow change the x44 address?

No, but you can use a bus multiplexer (p.e. PCA9548A).

thanks for advise.

So if i get it correctly

  1. Usually SHT31 is shipped with x44 address ? and if i get lucky i might get x45 or need to ask seller. No way to overwrite?

  2. Does the same apply for SHT21? its always x40 address?

  3. Without multiplexer I would be able to use one 1x SHT21 and 1x SHT31 on one board?

  4. Is there any other sensor (temp/hum) on the market (except DHT11 / 22) which i'll be able to use 3-4 pieces on one board without any additional board (eg. multiplexer) ?

Thanks

  1. Usually SHT31 is shipped with x44 address ? and if i get lucky i might get x45 or need to ask seller. No way to overwrite?

No, the SHT31 is shipped with both addresses 0x44 and 0x45 available, just select by connecting the address pin to either Vcc or GND. If you use the chip on a breakout board it depends on that board what address the chip got set or if you can select it yourself.

  1. Does the same apply for SHT21? its always x40 address?

No, not the same apply, the SHT21 has a fixed address.

  1. Without multiplexer I would be able to use one 1x SHT21 and 1x SHT31 on one board?

No, without a multiplexer you can use 2x SHT31 and 1x SHT21 on one board. But usually it doesn't make sense to have multiple humidity sensors on one board. If you need more than 2 SHT31 sensors on one board you may control the ADDR pin by a digital output of the processor to set the address of only one specific sensor to 0x45 (ADDR high), all others will listen to 0x44 (ADDR low).

  1. Is there any other sensor (temp/hum) on the market (except DHT11 / 22) which i'll be able to use 3-4 pieces on one board without any additional board (eg. multiplexer) ?

See answer to question 3.