Arduino 使用教程
库安装
- 启动 Arduino IDE
- 打开库管理器:
Sketch➔Include Library➔Manage Libraries... - 在
Filter your search...字段中搜索Sensirion I2C SHT3X库,然后点击install按钮进行安装。
接线图
(此处可插入接线图图片)
Example
单次测量温湿度模式
单次测量模式:控制板发送一次采集命令,传感器采集一次数据。此模式可根据需要读取数据,功耗较低。
cpp
#include <Arduino.h>
#include <SensirionI2cSht3x.h>
#include <Wire.h>
SensirionI2cSht3x sensor;
static char errorMessage[64];
static int16_t error;
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(100);
}
Wire.begin();
sensor.begin(Wire, SHT30_I2C_ADDR_44);
sensor.stopMeasurement();
delay(1);
sensor.softReset();
delay(100);
uint16_t aStatusRegister = 0u;
error = sensor.readStatusRegister(aStatusRegister);
if (error != NO_ERROR) {
Serial.print("Error trying to execute readStatusRegister(): ");
errorToString(error, errorMessage, sizeof errorMessage);
Serial.println(errorMessage);
return;
}
Serial.print("aStatusRegister: ");
Serial.print(aStatusRegister);
Serial.println();
}
void loop() {
float aTemperature = 0.0;
float aHumidity = 0.0;
error = sensor.measureSingleShot(REPEATABILITY_MEDIUM, false, aTemperature,
aHumidity);
if (error != NO_ERROR) {
Serial.print("Error trying to execute measureSingleShot(): ");
errorToString(error, errorMessage, sizeof errorMessage);
Serial.println(errorMessage);
return;
}
Serial.print("aTemperature: ");
Serial.print(aTemperature);
Serial.print("\t");
Serial.print("aHumidity: ");
Serial.print(aHumidity);
Serial.println();
}结果:串口打印出获取到的温湿度数据。
周期测量温湿度模式
周期测量模式:传感器按照设定采集频率自动采集数据。
cpp
#include <Arduino.h>
#include <SensirionI2cSht3x.h>
#include <Wire.h>
SensirionI2cSht3x sensor;
static char errorMessage[64];
static int16_t error;
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(100);
}
Wire.begin();
sensor.begin(Wire, SHT30_I2C_ADDR_44);
sensor.stopMeasurement();
delay(1);
sensor.softReset();
delay(100);
uint16_t aStatusRegister = 0u;
error = sensor.readStatusRegister(aStatusRegister);
if (error != NO_ERROR) {
Serial.print("Error trying to execute readStatusRegister(): ");
errorToString(error, errorMessage, sizeof errorMessage);
Serial.println(errorMessage);
return;
}
Serial.print("aStatusRegister: ");
Serial.print(aStatusRegister);
Serial.println();
error = sensor.startPeriodicMeasurement(REPEATABILITY_MEDIUM,
MPS_ONE_PER_SECOND);
if (error != NO_ERROR) {
Serial.print("Error trying to execute startPeriodicMeasurement(): ");
errorToString(error, errorMessage, sizeof errorMessage);
Serial.println(errorMessage);
return;
}
}
void loop() {
float aTemperature = 0.0;
float aHumidity = 0.0;
error = sensor.blockingReadMeasurement(aTemperature, aHumidity);
if (error != NO_ERROR) {
Serial.print("Error trying to execute blockingReadMeasurement(): ");
errorToString(error, errorMessage, sizeof errorMessage);
Serial.println(errorMessage);
return;
}
Serial.print("aTemperature: ");
Serial.print(aTemperature);
Serial.print("\t");
Serial.print("aHumidity: ");
Serial.print(aHumidity);
Serial.println();
}结果:串口前10S打印周期测量模式下获取的温湿度数据,10S后退出周期测量模式,进入单次测量模式,打印单次测量模式下获取的温湿度数据。