Advertisement
microrobotics

SHT40

Mar 30th, 2023
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The SHT40 is a temperature and humidity sensor that communicates via the I2C interface. Here's an example code in Arduino that reads the temperature and humidity from the sensor:
  3.  
  4. Note: This code uses the SHT4x library for Arduino to communicate with the SHT40 temperature and humidity sensor. You'll need to install the library before running this code. Also, note that the I2C address of the sensor is fixed at 0x44, so you don't need to specify the address in the code. Finally, note that the sensor has a measurement range of -40 to 125°C for temperature and 0 to 100% for humidity, and provides readings with an accuracy of ±0.2°C for temperature and ±2%RH for humidity.
  5. */
  6.  
  7. #include <Wire.h>
  8. #include <SHT4x.h>
  9.  
  10. SHT4x sht40;
  11.  
  12. void setup() {
  13.   Serial.begin(9600);
  14.   Wire.begin(); // start the I2C interface
  15.   sht40.init();
  16. }
  17.  
  18. void loop() {
  19.   float temperature, humidity;
  20.  
  21.   if (sht40.read(&temperature, &humidity)) {
  22.     Serial.print("Temperature: ");
  23.     Serial.print(temperature, 1);
  24.     Serial.print(" °C, Humidity: ");
  25.     Serial.print(humidity, 1);
  26.     Serial.println(" %RH");
  27.   } else {
  28.     Serial.println("Error reading from SHT40");
  29.   }
  30.  
  31.   delay(1000);
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement