This sensor is not used in the TWELITE PAL series. For usage examples, refer to:
Processing Flow
Wire.begin()
: Initialize the bus.setup()
: Initialize the sensor.begin()
: Start the sensor- Wait for a few milliseconds
.available()
becomestrue
.get_temp(), .get_humid()
: Read values
Required Procedures for Operation
Wire Bus
Before calling the setup()
method, initialize the Wire with Wire.begin()
.
Procedure After Waking from Sleep
Ensure the Wire bus is active just before entering sleep (Wire will be automatically recovered after waking).
Code Example
##include <TWELITE>
##include <SNS_SHT3X>
SNS_SHT3X sns_sht3x; // Declare the object
Include #include <SNS_SHT3X>
and declare an object of the SNS_SHT3X
class.
Initialization
void setup() {
Wire.begin();
sns_sht3x.setup();
}
Starting Sensor Reading
void loop() {
if(eState == E_STATE::INIT) {
sns_sht3x.begin();
eState = E_STATE::CAPTURE;
}
}
To begin reading sensor values, call .begin()
. It takes a few milliseconds to complete.
※ The above loop()
assumes a state-machine design using the eState
variable. (Reference)
Waiting for Sensor Reading
void loop() {
if(eState == E_STATE::CAPTURE) {
if (sns_sht3x.available()) {
// Sensor values are ready
}
}
}
Check .available()
to determine whether the sensor values are ready.
Reading Sensor Values
void loop() {
if(eState == E_STATE::CAPTURE) {
if (sns_sht3x.available()) {
Serial << crlf << "SHT3X:"
<< " T=" << sns_sht3x.get_temp() << 'C'
<< " H=" << sns_sht3x.get_humid() << '%';
}
}
}
Once the sensor values are ready, you can read them.
.get_temp()
and .get_humid()
use floating point operations. You can also retrieve values as integers scaled by 100.
auto temp = div100(sns_sht3x.get_temp_cent());
auto humd = div100(sns_sht3x.get_humid_per_dmil);
Serial << crlf << "SHT3X:"
<< format(" T=%c%d.%02d", temp.neg ? '-' : ' ', temp.quo, temp.rem)
<< format(" H=%c%d.%02d", humd.neg ? '-' : ' ', humd.quo, humd.rem);
In this example, div100()
is used to split the x100 values into integer and fractional parts.
Methods
get_temp()
, get_temp_cent()
double get_temp()
int16_t get_temp_cent()
Reads the temperature. get_temp()
returns °C, while get_temp_cent()
returns the value scaled by 100 as an integer.
On error, it returns a value between -32760 and -32768.
get_humid()
, get_humid_per_dmil()
double get_humid()
int16_t get_humid_per_dmil()
Reads the humidity. get_humid()
returns %RH, while get_humid_per_dmil()
returns the value scaled by 100 as an integer.
On error, it returns a value between -32760 and -32768.
Common Methods
setup()
void setup(uint32_t arg1 = 0UL)
Allocates memory and initializes the sensor.
You can specify the I2C address in the lower 8 bits of arg1
. If unspecified, it defaults to 0.
##include <SNS_SHT3X>
SNS_SHT3X sns_sht3x;
bool b_found_sht3x = false;
void setup() {
sns_sht3x.setup();
if (!sns_sht3x.probe()) {
delayMicroseconds(100); // just in case, wait for devices to listen further I2C comm.
sns_sht3x.setup(0x45); // alternative ID
if (sns_sht3x.probe()) b_found_sht3x = true;
} else {
b_found_sht3x = true;
}
}
In this example, it first tries to initialize with the default I2C ID. If no response, it retries with address 0x45
.
begin()
, end()
void begin()
void end()
Begins sensor reading. It takes a few milliseconds before values become ready, during which .available()
returns false.
end()
is not supported.
process_ev()
void process_ev(uint32_t arg1, uint32_t arg2 = 0)
For sensors that require waiting, provide E_EVENT_TICK_TIMER
or E_EVENT_START_UP
in arg1
to notify the passage of time. If enough time has elapsed after calling this method, .available()
will return true and the sensor value can be read.
available()
bool available()
Returns true
when the sensor is ready to be read.
probe()
bool probe()
Returns true
if the sensor is connected.
sns_stat()
uint32_t sns_stat()
Stores various information about the sensor device.
- The stored value is undefined for this device.
sns_opt()
uint32_t& sns_opt()
Returns the value passed to setup(uint32_t arg1)
.
- The lower 8 bits store the specified I2C device address.