Adafruit SHT40 basic driver

Dependents:   SHT40-example

Basic library for Adafruit SHT40 Working on PSoC6 Pioneer dev board CY8CKIT-062-WIFI-BT

Committer:
reedas
Date:
Mon Feb 07 15:08:07 2022 +0000
Revision:
0:c46b3e0a8c2b
Child:
1:c6381a3f2d9a
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
reedas 0:c46b3e0a8c2b 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 0:c46b3e0a8c2b 85