Skip to content

Commit 9350310

Browse files
committed
0.5.1 SHT2x
1 parent 5821bfa commit 9350310

File tree

26 files changed

+723
-375
lines changed

26 files changed

+723
-375
lines changed

libraries/SHT2x/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.5.1] - 2025-05-27
10+
- PR #32 - update readme.md, comments (Kudos to morfeus02)
11+
- add 2 examples
12+
- minor edits.
13+
914
## [0.5.0] - 2023-12-07
1015
- update API, begin()
1116
- update readme.md

libraries/SHT2x/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021-2024 Rob Tillaart
3+
Copyright (c) 2021-2025 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

libraries/SHT2x/README.md

Lines changed: 191 additions & 132 deletions
Large diffs are not rendered by default.

libraries/SHT2x/SHT2x.cpp

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT2x.cpp
33
// AUTHOR: Rob Tillaart, Viktor Balint, JensB
4-
// VERSION: 0.5.0
4+
// VERSION: 0.5.1
55
// DATE: 2023-11-25
66
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
77
// URL: https://github.com/RobTillaart/SHT2x
@@ -164,19 +164,23 @@ bool SHT2x::readTemperature()
164164
if (crc8(buffer, 2) != buffer[2])
165165
{
166166
_error = SHT2x_ERR_CRC_TEMP;
167-
// return false; // do not fail yet
167+
// Allow reading the value even if CRC fails, as _error is flagged.
168+
// The user should call getError() to check the status.
169+
// return false;
168170
}
169171
_rawTemperature = buffer[0] << 8;
170172
_rawTemperature += buffer[1];
171-
_rawTemperature &= 0xFFFC;
173+
_rawTemperature &= 0xFFFC; // Clear status bits (last two bits)
172174

173-
// clear requestType
175+
// clear requestType, marking this async operation as complete
174176
_requestType = SHT2x_REQ_NONE;
175177

176-
_status = buffer[1] & 0x03;
177-
if (_status == 0xFF) // TODO != 0x01 (need HW to test)
178+
_status = buffer[1] & 0x03; // Extract status bits
179+
// After a temperature read, the status bits should indicate "temperature reading" (0x01).
180+
// If not, it implies a read error or unexpected sensor state.
181+
if (_status != SHT2x_STATUS_TEMPERATURE)
178182
{
179-
_error = SHT2x_ERR_READBYTES;
183+
_error = SHT2x_ERR_READBYTES; // Or a more specific error e.g. SHT2x_ERR_UNEXPECTED_STATUS
180184
return false;
181185
}
182186
return true;
@@ -194,24 +198,28 @@ bool SHT2x::readHumidity()
194198
if (crc8(buffer, 2) != buffer[2])
195199
{
196200
_error = SHT2x_ERR_CRC_HUM;
197-
// return false; // do not fail yet
201+
// Allow reading the value even if CRC fails, as _error is flagged.
202+
// The user should call getError() to check the status.
203+
// return false;
198204
}
199205
_rawHumidity = buffer[0] << 8;
200206
_rawHumidity += buffer[1];
201-
_rawHumidity &= 0xFFFC;
207+
_rawHumidity &= 0xFFFC; // Clear status bits (last two bits)
202208

203-
// clear requestType
209+
// clear requestType, marking this async operation as complete
204210
_requestType = SHT2x_REQ_NONE;
205211

206-
_status = buffer[1] & 0x03;
207-
if (_status == 0xFF) // TODO != 0x02 (need HW to test)
212+
_status = buffer[1] & 0x03; // Extract status bits
213+
// After a humidity read, the status bits should indicate "humidity reading" (0x02).
214+
// If not, it implies a read error or unexpected sensor state.
215+
if (_status != SHT2x_STATUS_HUMIDITY)
208216
{
209-
_error = SHT2x_ERR_READBYTES;
217+
_error = SHT2x_ERR_READBYTES; // Or a more specific error e.g. SHT2x_ERR_UNEXPECTED_STATUS
210218
return false;
211219
}
212220

213-
_error = SHT2x_OK;
214-
_lastRead = millis();
221+
_error = SHT2x_OK; // Mark as OK if all checks passed for this specific read
222+
_lastRead = millis(); // Record time of successful synchronous style read completion
215223
return true;
216224
}
217225

@@ -412,7 +420,7 @@ bool SHT2x::setHeaterLevel(uint8_t level)
412420
heaterReg |= level;
413421
if (writeCmd(0x51, heaterReg) == false)
414422
{
415-
_error = -1;
423+
_error = SHT2x_ERR_WRITECMD; // Use defined error code for write command failure
416424
return false;
417425
}
418426
return true;
@@ -563,18 +571,25 @@ bool SHT2x::batteryOK()
563571
//
564572
uint8_t SHT2x::crc8(const uint8_t *data, uint8_t len)
565573
{
566-
// CRC-8 formula from page 14 of SHT spec pdf
567-
// Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf
568-
const uint8_t POLY = 0x31;
574+
// CRC-8 formula from page 14 of SHT2x datasheet.
575+
// Document: "Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf"
576+
// Polynomial: x^8 + x^5 + x^4 + 1 (0x131 -> 0x31, MSB is implicit)
577+
const uint8_t CRC_POLYNOMIAL = 0x31;
569578
uint8_t crc = 0x00;
570579

571-
for (uint8_t j = len; j; --j)
580+
for (uint8_t byteIndex = 0; byteIndex < len; ++byteIndex)
572581
{
573-
crc ^= *data++;
574-
575-
for (uint8_t i = 8; i; --i)
582+
crc ^= data[byteIndex]; // XOR byte into CRC
583+
for (uint8_t bit = 8; bit > 0; --bit)
576584
{
577-
crc = (crc & 0x80) ? (crc << 1) ^ POLY : (crc << 1);
585+
if (crc & 0x80) // If MSB is set
586+
{
587+
crc = (crc << 1) ^ CRC_POLYNOMIAL;
588+
}
589+
else
590+
{
591+
crc = (crc << 1);
592+
}
578593
}
579594
}
580595
return crc;

0 commit comments

Comments
 (0)