mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-17 18:52:07 +02:00
use "arduino-sht" library for sht3x and sht4x
This commit is contained in:
@ -0,0 +1,62 @@
|
||||
#include <Wire.h>
|
||||
#include "SHTSensor.h"
|
||||
|
||||
// Note that all i2c devices sharing one bus must have distinct addresses. Thus
|
||||
// this example only works with sensors that have distinct i2c addresses (e.g.
|
||||
// SHT3x and SHT3x_alt, or SHT3x and SHTC3).
|
||||
// Make sure not to use auto-detection as it will only pick up one sensor.
|
||||
|
||||
// Sensor with normal i2c address
|
||||
// Sensor 1 with address pin pulled to GND
|
||||
SHTSensor sht1(SHTSensor::SHT3X);
|
||||
|
||||
// Sensor with alternative i2c address
|
||||
// Sensor 2 with address pin pulled to Vdd
|
||||
SHTSensor sht2(SHTSensor::SHT3X_ALT);
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Wire.begin();
|
||||
Serial.begin(9600);
|
||||
delay(1000); // let serial console settle
|
||||
|
||||
// init on a specific sensor type (i.e. without auto detecting),
|
||||
// does not check if the sensor is responding and will thus always succeed.
|
||||
|
||||
// initialize sensor with normal i2c-address
|
||||
sht1.init();
|
||||
|
||||
// initialize sensor with alternative i2c-address
|
||||
sht2.init();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
// read from first sensor
|
||||
if (sht1.readSample()) {
|
||||
Serial.print("SHT1 :\n");
|
||||
Serial.print(" RH: ");
|
||||
Serial.print(sht1.getHumidity(), 2);
|
||||
Serial.print("\n");
|
||||
Serial.print(" T: ");
|
||||
Serial.print(sht1.getTemperature(), 2);
|
||||
Serial.print("\n");
|
||||
} else {
|
||||
Serial.print("Sensor 1: Error in readSample()\n");
|
||||
}
|
||||
|
||||
// read from second sensor
|
||||
if (sht2.readSample()) {
|
||||
Serial.print("SHT2:\n");
|
||||
Serial.print(" RH: ");
|
||||
Serial.print(sht2.getHumidity(), 2);
|
||||
Serial.print("\n");
|
||||
Serial.print(" T: ");
|
||||
Serial.print(sht2.getTemperature(), 2);
|
||||
Serial.print("\n");
|
||||
} else {
|
||||
Serial.print("Sensor 2: Error in readSample()\n");
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
#include <Wire.h>
|
||||
|
||||
#include "SHTSensor.h"
|
||||
|
||||
SHTSensor sht;
|
||||
// To use a specific sensor instead of probing the bus use this command:
|
||||
// SHTSensor sht(SHTSensor::SHT3X);
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
|
||||
Wire.begin();
|
||||
Serial.begin(9600);
|
||||
delay(1000); // let serial console settle
|
||||
|
||||
if (sht.init()) {
|
||||
Serial.print("init(): success\n");
|
||||
} else {
|
||||
Serial.print("init(): failed\n");
|
||||
}
|
||||
sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM); // only supported by SHT3x
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
if (sht.readSample()) {
|
||||
Serial.print("SHT:\n");
|
||||
Serial.print(" RH: ");
|
||||
Serial.print(sht.getHumidity(), 2);
|
||||
Serial.print("\n");
|
||||
Serial.print(" T: ");
|
||||
Serial.print(sht.getTemperature(), 2);
|
||||
Serial.print("\n");
|
||||
} else {
|
||||
Serial.print("Error in readSample()\n");
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
}
|
27
src/library/arduino-sht/examples/sht3xanalog/sht3xanalog.ino
Normal file
27
src/library/arduino-sht/examples/sht3xanalog/sht3xanalog.ino
Normal file
@ -0,0 +1,27 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#include "SHTSensor.h"
|
||||
|
||||
SHT3xAnalogSensor sht3xAnalog(A0, A1);
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
|
||||
Wire.begin();
|
||||
Serial.begin(9600);
|
||||
|
||||
delay(1000); // let serial console settle
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.print("SHT3x Analog:\n");
|
||||
Serial.print(" RH: ");
|
||||
Serial.print(sht3xAnalog.readHumidity(), 2);
|
||||
Serial.print("\n");
|
||||
Serial.print(" T: ");
|
||||
Serial.print(sht3xAnalog.readTemperature(), 2);
|
||||
Serial.print("\n");
|
||||
|
||||
delay(1000);
|
||||
}
|
Reference in New Issue
Block a user