Arduino and two SHT31


#1

hi I am trying to read from two SHT31 sensors one is jumpered to 0x45 and the other 0x44
I am reading them and apparently i am overwriting the data, but I just cant figure out how or where to write the data,

I was told

" You are putting the 6 bytes of requested data from the two different sensors in the same array data[6] and the values from the second reading are overwriting the first.

You are only converting the data from the second reading given how you are executing the convert the data code only once per loop. "

but I dont understand how to do this.

do you have any ideas?

thank you

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// SHT31
// This code is designed to work with the SHT31_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SHT31_I2CS#tabs-0-product_tabset-2

#include <Wire.h>

// SHT31 I2C address is 0x44(68)
#define Addr 0x44
#define Addr2 0x45


void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);
  delay(300);
}

void loop()
{
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }


  
{
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr2, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
 
   }}




  
  // Convert the data
  int temp = (data[0] * 256) + data[1];
  float cTemp = -45.0 + (175.0 * temp / 65535.0);
  float fTemp = (cTemp * 1.8) + 32.0;
  float humidity = (100.0 * ((data[3] * 256.0) + data[4])) / 65535.0;

  // Output data to serial monitor
  Serial.print("Temp   :");
  Serial.print(cTemp);
  Serial.println(" C");
  Serial.print("Humidity :");
  Serial.print(humidity);
  Serial.println(" %");
  delay(500);
}




#2

This is not related to freecodecamp, but I will try my best to answer.
At the beggining of loop function you create new array called “data” that contains unsigned ints, which is OK. Problems happens after that, because you initialize new array with name “data” and that completely overwrites contents of first array. You need to have two differently named arrays, for example data1 and data2, and after that you need to calculate two different temperatures(temp1/temp2) and humidities(humidity1/humidity2). Now, based on positions of sensors you can average those two temperature readings or print them separately.


#3

this is C code not javascript to be clear

The basic problem is the code is not structured well - there is duplication of code blocks causing confusion

you should write a function like this

// uses global Wire - better to turn it into a parameter
void fill_data(unsigned data[], unsigned addr)
{
  Wire.beginTransmission(addr);
  Wire.write(0x2C);
  Wire.write(0x06);
  Wire.endTransmission();
  delay(300);
  Wire.beginTransmission(addr);
  Wire.endTransmission();
  Wire.requestFrom(addr, 6);
  if (Wire.available() == 6) {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }
}

then call the function

const unsigned addr1=0x44, addr2=0x45;
unsigned data1[6], data2[6];
fill_data(data1, addr1);
fill_data(data2, addr2);

#4

thank you,
but it says that this call is ambiguous now?


#5

Could you paste the complete error message? Also your complete code