Skip to content

Commit 304b4b0

Browse files
committed
add driver for SHT20
1 parent 3d6ec11 commit 304b4b0

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
338338
_sht4x->configureDriver(msgDeviceInitReq);
339339
drivers.push_back(_sht4x);
340340
WS_DEBUG_PRINTLN("SHT4X Initialized Successfully!");
341+
} else if (strcmp("sht20", msgDeviceInitReq->i2c_device_name) == 0) {
342+
_sht20 = new WipperSnapper_I2C_Driver_SHT20(this->_i2c, i2cAddress);
343+
if (!_sht20->begin()) {
344+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize sht20!");
345+
_busStatusResponse =
346+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
347+
return false;
348+
}
349+
_sht20->configureDriver(msgDeviceInitReq);
350+
drivers.push_back(_sht20);
351+
WS_DEBUG_PRINTLN("SHT20 Initialized Successfully!");
341352
} else if (strcmp("sht3x", msgDeviceInitReq->i2c_device_name) == 0) {
342353
_sht3x = new WipperSnapper_I2C_Driver_SHT3X(this->_i2c, i2cAddress);
343354
if (!_sht3x->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "drivers/WipperSnapper_I2C_Driver_PM25.h"
3131
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
3232
#include "drivers/WipperSnapper_I2C_Driver_SCD40.h"
33+
#include "drivers/WipperSnapper_I2C_Driver_SHT20.h"
3334
#include "drivers/WipperSnapper_I2C_Driver_SHT3X.h"
3435
#include "drivers/WipperSnapper_I2C_Driver_SHT4X.h"
3536
#include "drivers/WipperSnapper_I2C_Driver_SHTC3.h"
@@ -95,6 +96,7 @@ class WipperSnapper_Component_I2C {
9596
WipperSnapper_I2C_Driver_SI7021 *_si7021 = nullptr;
9697
WipperSnapper_I2C_Driver_SHT4X *_sht4x = nullptr;
9798
WipperSnapper_I2C_Driver_SHT3X *_sht3x = nullptr;
99+
WipperSnapper_I2C_Driver_SHT20 *_sht20 = nullptr;
98100
WipperSnapper_I2C_Driver_SHTC3 *_shtc3 = nullptr;
99101
WipperSnapper_I2C_Driver_LC709203F *_lc = nullptr;
100102
WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor *_ss = nullptr;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_SHT20.h
3+
*
4+
* Device driver for the SHT20 Temperature and Humidity Sensor
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Adafruit Wippersnapper code Copyright (c) 2022 Adafruit Industries.
11+
* SHT20 Driver code Copyright (c) DFRobot 2022.
12+
*
13+
* MIT license, all text here must be included in any redistribution.
14+
*
15+
*/
16+
17+
#ifndef WipperSnapper_I2C_Driver_SHT20_H
18+
#define WipperSnapper_I2C_Driver_SHT20_H
19+
20+
#include "WipperSnapper_I2C_Driver.h"
21+
#include <DFRobot_SHT20.h>
22+
#include <Wire.h>
23+
24+
/**************************************************************************/
25+
/*!
26+
@brief Class that provides a driver interface for the SHT20 sensor.
27+
*/
28+
/**************************************************************************/
29+
class WipperSnapper_I2C_Driver_SHT20 : public WipperSnapper_I2C_Driver {
30+
31+
public:
32+
/*******************************************************************************/
33+
/*!
34+
@brief Constructor for a SHT20 sensor.
35+
@param i2c
36+
The I2C interface.
37+
@param sensorAddress
38+
7-bit device address.
39+
*/
40+
/*******************************************************************************/
41+
WipperSnapper_I2C_Driver_SHT20(TwoWire *i2c, uint16_t sensorAddress)
42+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
43+
_i2c = i2c;
44+
_sensorAddress = sensorAddress;
45+
}
46+
47+
/*******************************************************************************/
48+
/*!
49+
@brief Initializes the SHT20 sensor and begins I2C.
50+
@returns True if initialized successfully, False otherwise.
51+
*/
52+
/*******************************************************************************/
53+
bool begin() {
54+
DFRobot_SHT20 sht20(&Wire, SHT20_I2C_ADDR);
55+
56+
_SHT20 = new DFRobot_SHT20(_i2c, _sensorAddress);
57+
delay(100);
58+
return true;
59+
}
60+
61+
/*******************************************************************************/
62+
/*!
63+
@brief Gets the SHT20's current temperature.
64+
@param tempEvent
65+
Pointer to an Adafruit_Sensor event.
66+
@returns True if the temperature was obtained successfully, False
67+
otherwise.
68+
*/
69+
/*******************************************************************************/
70+
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
71+
// populate temp and humidity objects with fresh data
72+
tempEvent->temperature = _SHT20->readTemperature();
73+
// the driver returns a sketchy float if an error occurs, this could be caught
74+
// here with a sensible range check and return false if not.
75+
return true;
76+
}
77+
78+
/*******************************************************************************/
79+
/*!
80+
@brief Gets the SHT20's current relative humidity reading.
81+
@param humidEvent
82+
Pointer to an Adafruit_Sensor event.
83+
@returns True if the humidity was obtained successfully, False
84+
otherwise.
85+
*/
86+
/*******************************************************************************/
87+
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
88+
// populate temp and humidity objects with fresh data
89+
humidEvent->relative_humidity = _SHT20->readHumidity();
90+
// the driver returns a sketchy float if an error occurs, this could be caught
91+
// here with a sensible range check and return false if not.
92+
return true;
93+
}
94+
95+
protected:
96+
DFRobot_SHT20 *_SHT20; ///< SHT20 object
97+
};
98+
99+
#endif // WipperSnapper_I2C_Driver_SHT20

0 commit comments

Comments
 (0)