Categories

Follow

ESP8266 SHT30 shield I2C code example

This example uses the same Wemos SHt30 shield or one of the variants on the internet but tis time we don’t use a library, no difificult to do this and very handy for learning just in case you ever encounter an I2C device with no library and you may have to write your own.

The key is the datasheet for the device, in there you will find the I2C address(es) and any commands or configuration that you may need, the SHT30 is quite an easy one. You can actually look at the code in the libraries you download (or on github) as well.

Code

#include <Wire.h>
 
// WEMOS SHT30 I2C address is 0x45
#define Addr 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 measurement command
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(500);
 
  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);
 
  // Read 6 bytes of data
  // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity 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
  float cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45;
  float fTemp = (cTemp * 1.8) + 32;
  float humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0);
  // Output data to serial monitor
  Serial.print("Relative Humidity : ");
  Serial.print(humidity);
  Serial.println(" %RH");
  Serial.print("Temperature in Celsius : ");
  Serial.print(cTemp);
  Serial.println(" C");
  Serial.print("Temperature in Fahrenheit : ");
  Serial.print(fTemp);
  Serial.println(" F");
  delay(500);
}

 

Output

Open the serial monitor

Temperature in Celsius : 30.02 C
Temperature in Fahrenheit : 86.03 F
Relative Humidity : 56.31 %RH
Temperature in Celsius : 30.56 C
Temperature in Fahrenheit : 87.02 F
Relative Humidity : 60.39 %RH
Temperature in Celsius : 30.98 C
Temperature in Fahrenheit : 87.77 F
Relative Humidity : 60.43 %RH
Temperature in Celsius : 30.55 C
Temperature in Fahrenheit : 86.99 F
Relative Humidity : 54.71 %RH
Temperature in Celsius : 30.28 C
Temperature in Fahrenheit : 86.51 F
Relative Humidity : 49.55 %RH

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

*