请参考/***************************************************
This is an example for the SHT31-D Humidity & Temp Sensor
Designed specifically to work with the SHT31-D sensor from Adafruit
These sensors use I2C to communicate, 2 pins are required to
interface
Hardware connections for Arduino Nano:
VDD to 3.3V DC
SDA to A4
SCL to A5
GND to common groud
****************************************************/
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include "LiquidCrystal_I2C.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif
void setup() {
//#ifndef ESP8266
// while (!Serial); // will pause Zero, Leonardo, etc until serial console opens
//#endif
Serial.begin(9600);
Serial.println("SHT31 test");
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println("Couldn't find SHT31");
while (1)
delay(1);
}
}
void loop() {
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (! isnan(t)) { // check if 'is not a number'
Serial.print("Temp *C = "); Serial.println(t);
} else {
Serial.println("Failed to read temperature");
}
if (! isnan(h)) { // check if 'is not a number'
Serial.print("Hum. % = "); Serial.println(h);
} else {
Serial.println("Failed to read humidity");
}
Serial.println();
delay(5000);
}
|