Adafruit SHT40 basic driver
Basic library for Adafruit SHT40 Working on PSoC6 Pioneer dev board CY8CKIT-062-WIFI-BT
SHT40.cpp@1:c6381a3f2d9a, 2022-02-14 (annotated)
- Committer:
- reedas
- Date:
- Mon Feb 14 23:53:34 2022 +0000
- Revision:
- 1:c6381a3f2d9a
- Parent:
- 0:c46b3e0a8c2b
added floating point return values;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
reedas | 1:c6381a3f2d9a | 1 | /** |
reedas | 0:c46b3e0a8c2b | 2 | * @author Andrew Reed |
reedas | 0:c46b3e0a8c2b | 3 | * |
reedas | 0:c46b3e0a8c2b | 4 | * Freeware -- |
reedas | 0:c46b3e0a8c2b | 5 | * simple driver hacked together by hardware engineer, definite scope for |
reedas | 0:c46b3e0a8c2b | 6 | * improvement by someone who knows waht they are doing. |
reedas | 0:c46b3e0a8c2b | 7 | * |
reedas | 0:c46b3e0a8c2b | 8 | * @section DESCRIPTION |
reedas | 0:c46b3e0a8c2b | 9 | * |
reedas | 0:c46b3e0a8c2b | 10 | * SHT40 i2c Humidity and Temperature sensor. |
reedas | 0:c46b3e0a8c2b | 11 | * |
reedas | 0:c46b3e0a8c2b | 12 | * Datasheet, specs, and information: |
reedas | 0:c46b3e0a8c2b | 13 | * |
reedas | 0:c46b3e0a8c2b | 14 | * https://www.adafruit.com/product/4885 |
reedas | 0:c46b3e0a8c2b | 15 | */ |
reedas | 0:c46b3e0a8c2b | 16 | |
reedas | 0:c46b3e0a8c2b | 17 | /** |
reedas | 0:c46b3e0a8c2b | 18 | * Includes |
reedas | 0:c46b3e0a8c2b | 19 | */ |
reedas | 0:c46b3e0a8c2b | 20 | #include "SHT40.h" |
reedas | 0:c46b3e0a8c2b | 21 | |
reedas | 0:c46b3e0a8c2b | 22 | SHT40::SHT40(PinName sda, PinName scl) { |
reedas | 0:c46b3e0a8c2b | 23 | |
reedas | 0:c46b3e0a8c2b | 24 | i2c_ = new I2C(sda, scl); |
reedas | 0:c46b3e0a8c2b | 25 | //400KHz, as specified by the datasheet. |
reedas | 0:c46b3e0a8c2b | 26 | i2c_->frequency(I2C_SPEED_FAST); |
reedas | 0:c46b3e0a8c2b | 27 | |
reedas | 0:c46b3e0a8c2b | 28 | |
reedas | 0:c46b3e0a8c2b | 29 | |
reedas | 0:c46b3e0a8c2b | 30 | } |
reedas | 0:c46b3e0a8c2b | 31 | |
reedas | 0:c46b3e0a8c2b | 32 | int SHT40::tempC(void) { |
reedas | 0:c46b3e0a8c2b | 33 | |
reedas | 0:c46b3e0a8c2b | 34 | char txBuff[2]; |
reedas | 0:c46b3e0a8c2b | 35 | char rxBuff[6]; |
reedas | 0:c46b3e0a8c2b | 36 | |
reedas | 0:c46b3e0a8c2b | 37 | txBuff[0] = SHT4x_NOHEAT_HIGHPRECISION; // Triggers a temperature measure by feeding correct opcode. |
reedas | 0:c46b3e0a8c2b | 38 | txBuff[1] = SHT4x_NOHEAT_HIGHPRECISION >> 8; |
reedas | 0:c46b3e0a8c2b | 39 | i2c_->write((SHT40_I2C_ADDRESS << 1) & 0xFE, txBuff, 2); |
reedas | 0:c46b3e0a8c2b | 40 | thread_sleep_for(50); // Per datasheet, wait long enough for device to sample temperature |
reedas | 0:c46b3e0a8c2b | 41 | |
reedas | 0:c46b3e0a8c2b | 42 | // Reads triggered measure |
reedas | 0:c46b3e0a8c2b | 43 | i2c_->read((SHT40_I2C_ADDRESS << 1) | 0x01, rxBuff, 6); |
reedas | 0:c46b3e0a8c2b | 44 | thread_sleep_for(1); |
reedas | 0:c46b3e0a8c2b | 45 | |
reedas | 0:c46b3e0a8c2b | 46 | // Algorithm from arduino sht4x.h to compute temperature. |
reedas | 0:c46b3e0a8c2b | 47 | unsigned int rawTemperature = ((unsigned int) rxBuff[0] << 8) | (unsigned int) rxBuff[1]; |
reedas | 0:c46b3e0a8c2b | 48 | rawTemperature &= 0xFFFC; |
reedas | 0:c46b3e0a8c2b | 49 | |
reedas | 0:c46b3e0a8c2b | 50 | float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536 |
reedas | 0:c46b3e0a8c2b | 51 | float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14 |
reedas | 0:c46b3e0a8c2b | 52 | |
reedas | 0:c46b3e0a8c2b | 53 | return (int)realTemperature; |
reedas | 0:c46b3e0a8c2b | 54 | |
reedas | 0:c46b3e0a8c2b | 55 | } |
reedas | 0:c46b3e0a8c2b | 56 | int SHT40::relHumid(void) { |
reedas | 0:c46b3e0a8c2b | 57 | |
reedas | 0:c46b3e0a8c2b | 58 | char txBuff[2]; |
reedas | 0:c46b3e0a8c2b | 59 | char rxBuff[6]; |
reedas | 0:c46b3e0a8c2b | 60 | |
reedas | 0:c46b3e0a8c2b | 61 | |
reedas | 0:c46b3e0a8c2b | 62 | txBuff[0] = SHT4x_NOHEAT_HIGHPRECISION; // send command. |
reedas | 0:c46b3e0a8c2b | 63 | txBuff[1] = SHT4x_NOHEAT_HIGHPRECISION >> 8; |
reedas | 0:c46b3e0a8c2b | 64 | i2c_->write((SHT40_I2C_ADDRESS << 1) & 0xFE, txBuff, 2); |
reedas | 0:c46b3e0a8c2b | 65 | thread_sleep_for(16); // delay to sample humidity |
reedas | 0:c46b3e0a8c2b | 66 | |
reedas | 0:c46b3e0a8c2b | 67 | // Reads measurement |
reedas | 0:c46b3e0a8c2b | 68 | i2c_->read((SHT40_I2C_ADDRESS << 1) | 0x01, rxBuff, 6); |
reedas | 0:c46b3e0a8c2b | 69 | thread_sleep_for(1); |
reedas | 0:c46b3e0a8c2b | 70 | |
reedas | 0:c46b3e0a8c2b | 71 | //Algorithm from sht4x.h. |
reedas | 0:c46b3e0a8c2b | 72 | unsigned int rawHumidity = ((unsigned int) rxBuff[3] << 8) | (unsigned int) rxBuff[4]; |
reedas | 0:c46b3e0a8c2b | 73 | |
reedas | 0:c46b3e0a8c2b | 74 | rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place |
reedas | 0:c46b3e0a8c2b | 75 | |
reedas | 0:c46b3e0a8c2b | 76 | //Convert to relative humidity |
reedas | 0:c46b3e0a8c2b | 77 | float RH = rawHumidity / (float)65536; // two bytes worth to float |
reedas | 0:c46b3e0a8c2b | 78 | float rh = -6 + (125 * RH); // adjustment values as per sht4x.h |
reedas | 0:c46b3e0a8c2b | 79 | rh = min(max(rh, (float)0.0), (float)100.0); |
reedas | 0:c46b3e0a8c2b | 80 | |
reedas | 0:c46b3e0a8c2b | 81 | |
reedas | 0:c46b3e0a8c2b | 82 | return (int)rh; |
reedas | 0:c46b3e0a8c2b | 83 | |
reedas | 0:c46b3e0a8c2b | 84 | } |
reedas | 1:c6381a3f2d9a | 85 | float SHT40::tempCF(void) { |
reedas | 0:c46b3e0a8c2b | 86 | |
reedas | 1:c6381a3f2d9a | 87 | char txBuff[2]; |
reedas | 1:c6381a3f2d9a | 88 | char rxBuff[6]; |
reedas | 1:c6381a3f2d9a | 89 | |
reedas | 1:c6381a3f2d9a | 90 | txBuff[0] = SHT4x_NOHEAT_HIGHPRECISION; // Triggers a temperature measure by feeding correct opcode. |
reedas | 1:c6381a3f2d9a | 91 | txBuff[1] = SHT4x_NOHEAT_HIGHPRECISION >> 8; |
reedas | 1:c6381a3f2d9a | 92 | i2c_->write((SHT40_I2C_ADDRESS << 1) & 0xFE, txBuff, 2); |
reedas | 1:c6381a3f2d9a | 93 | thread_sleep_for(50); // Per datasheet, wait long enough for device to sample temperature |
reedas | 1:c6381a3f2d9a | 94 | |
reedas | 1:c6381a3f2d9a | 95 | // Reads triggered measure |
reedas | 1:c6381a3f2d9a | 96 | i2c_->read((SHT40_I2C_ADDRESS << 1) | 0x01, rxBuff, 6); |
reedas | 1:c6381a3f2d9a | 97 | thread_sleep_for(1); |
reedas | 1:c6381a3f2d9a | 98 | |
reedas | 1:c6381a3f2d9a | 99 | // Algorithm from arduino sht4x.h to compute temperature. |
reedas | 1:c6381a3f2d9a | 100 | unsigned int rawTemperature = ((unsigned int) rxBuff[0] << 8) | (unsigned int) rxBuff[1]; |
reedas | 1:c6381a3f2d9a | 101 | rawTemperature &= 0xFFFC; |
reedas | 1:c6381a3f2d9a | 102 | |
reedas | 1:c6381a3f2d9a | 103 | float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536 |
reedas | 1:c6381a3f2d9a | 104 | float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14 |
reedas | 1:c6381a3f2d9a | 105 | |
reedas | 1:c6381a3f2d9a | 106 | return (float)realTemperature; |
reedas | 1:c6381a3f2d9a | 107 | |
reedas | 1:c6381a3f2d9a | 108 | } |
reedas | 1:c6381a3f2d9a | 109 | float SHT40::relHumidF(void) { |
reedas | 1:c6381a3f2d9a | 110 | |
reedas | 1:c6381a3f2d9a | 111 | char txBuff[2]; |
reedas | 1:c6381a3f2d9a | 112 | char rxBuff[6]; |
reedas | 1:c6381a3f2d9a | 113 | |
reedas | 1:c6381a3f2d9a | 114 | |
reedas | 1:c6381a3f2d9a | 115 | txBuff[0] = SHT4x_NOHEAT_HIGHPRECISION; // send command. |
reedas | 1:c6381a3f2d9a | 116 | txBuff[1] = SHT4x_NOHEAT_HIGHPRECISION >> 8; |
reedas | 1:c6381a3f2d9a | 117 | i2c_->write((SHT40_I2C_ADDRESS << 1) & 0xFE, txBuff, 2); |
reedas | 1:c6381a3f2d9a | 118 | thread_sleep_for(16); // delay to sample humidity |
reedas | 1:c6381a3f2d9a | 119 | |
reedas | 1:c6381a3f2d9a | 120 | // Reads measurement |
reedas | 1:c6381a3f2d9a | 121 | i2c_->read((SHT40_I2C_ADDRESS << 1) | 0x01, rxBuff, 6); |
reedas | 1:c6381a3f2d9a | 122 | thread_sleep_for(1); |
reedas | 1:c6381a3f2d9a | 123 | |
reedas | 1:c6381a3f2d9a | 124 | //Algorithm from sht4x.h. |
reedas | 1:c6381a3f2d9a | 125 | unsigned int rawHumidity = ((unsigned int) rxBuff[3] << 8) | (unsigned int) rxBuff[4]; |
reedas | 1:c6381a3f2d9a | 126 | |
reedas | 1:c6381a3f2d9a | 127 | rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place |
reedas | 1:c6381a3f2d9a | 128 | |
reedas | 1:c6381a3f2d9a | 129 | //Convert to relative humidity |
reedas | 1:c6381a3f2d9a | 130 | float RH = rawHumidity / (float)65536; // two bytes worth to float |
reedas | 1:c6381a3f2d9a | 131 | float rh = -6 + (125 * RH); // adjustment values as per sht4x.h |
reedas | 1:c6381a3f2d9a | 132 | rh = min(max(rh, (float)0.0), (float)100.0); |
reedas | 1:c6381a3f2d9a | 133 | |
reedas | 1:c6381a3f2d9a | 134 | |
reedas | 1:c6381a3f2d9a | 135 | return (float)rh; |
reedas | 1:c6381a3f2d9a | 136 | |
reedas | 1:c6381a3f2d9a | 137 | } |