#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(9600);
if (!sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}
Serial.println("SHT31 found!");
Serial.println("Humidity and temperature test");
}
void loop() {
delay(2000);
float temp = sht31.readTemperature();
float humid = sht31.readHumidity();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" °C\t");
Serial.print("Humidity: ");
Serial.print(humid);
Serial.println(" %");
}
/*Before uploading the code, make sure you've installed the Adafruit_SHT31 library. You can do this through the Arduino IDE by navigating to Sketch > Include Library > Manage Libraries..., then searching for "Adafruit SHT31" and installing it.
In the code:
We include the Wire library for I2C communication and the Adafruit_SHT31 library.
We create an instance of the Adafruit_SHT31 class called sht31.
In the setup() function, we initialize serial communication and initialize the SHT31 sensor with its default I2C address (0x44). If the sensor is not found, the program will be stuck in an infinite loop printing an error message.
In the loop() function, we read temperature and humidity values from the sensor and print them to the Serial Monitor every 2 seconds.
Make sure you have your SHT31-D sensor connected properly to the Arduino Uno's I2C pins (A4 for SDA, A5 for SCL), and ensure the power supply is connected as per the sensor's specifications.
*/