Adafruit SHT40 basic driver
Basic library for Adafruit SHT40 Working on PSoC6 Pioneer dev board CY8CKIT-062-WIFI-BT
SHT40.h@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 | 0:c46b3e0a8c2b | 1 | /*! |
reedas | 0:c46b3e0a8c2b | 2 | * @file Adafruit_SHT4x.cpp |
reedas | 0:c46b3e0a8c2b | 3 | * |
reedas | 0:c46b3e0a8c2b | 4 | * @mainpage Adafruit SHT4x Digital Humidity & Temp Sensor |
reedas | 0:c46b3e0a8c2b | 5 | * |
reedas | 0:c46b3e0a8c2b | 6 | * @section intro_sec Introduction |
reedas | 0:c46b3e0a8c2b | 7 | * |
reedas | 0:c46b3e0a8c2b | 8 | * This is a library for the SHT4x Digital Humidity & Temp Sensor |
reedas | 0:c46b3e0a8c2b | 9 | * |
reedas | 0:c46b3e0a8c2b | 10 | * Designed specifically to work with the SHT4x Digital sensor from Adafruit |
reedas | 0:c46b3e0a8c2b | 11 | * |
reedas | 0:c46b3e0a8c2b | 12 | * Pick one up today in the adafruit shop! |
reedas | 0:c46b3e0a8c2b | 13 | * ------> https://www.adafruit.com/product/4885 |
reedas | 0:c46b3e0a8c2b | 14 | * |
reedas | 0:c46b3e0a8c2b | 15 | * These sensors use I2C to communicate, 2 pins are required to interface |
reedas | 0:c46b3e0a8c2b | 16 | * |
reedas | 0:c46b3e0a8c2b | 17 | * Adafruit invests time and resources providing this open source code, |
reedas | 0:c46b3e0a8c2b | 18 | * please support Adafruit andopen-source hardware by purchasing products |
reedas | 0:c46b3e0a8c2b | 19 | * from Adafruit! |
reedas | 0:c46b3e0a8c2b | 20 | * |
reedas | 0:c46b3e0a8c2b | 21 | * @section author Author |
reedas | 0:c46b3e0a8c2b | 22 | * |
reedas | 0:c46b3e0a8c2b | 23 | * Limor Fried/Ladyada (Adafruit Industries). |
reedas | 0:c46b3e0a8c2b | 24 | * |
reedas | 0:c46b3e0a8c2b | 25 | * @section license License |
reedas | 0:c46b3e0a8c2b | 26 | * |
reedas | 0:c46b3e0a8c2b | 27 | * BSD license, all text above must be included in any redistribution |
reedas | 0:c46b3e0a8c2b | 28 | * |
reedas | 0:c46b3e0a8c2b | 29 | * @reworked by Andrew Reed areed@cityplym.ac.uk |
reedas | 0:c46b3e0a8c2b | 30 | * |
reedas | 0:c46b3e0a8c2b | 31 | * simple driver hacked together by hardware engineer, definite scope for |
reedas | 0:c46b3e0a8c2b | 32 | * improvement by someone who knows what they are doing. |
reedas | 0:c46b3e0a8c2b | 33 | * |
reedas | 0:c46b3e0a8c2b | 34 | * @section DESCRIPTION |
reedas | 0:c46b3e0a8c2b | 35 | * |
reedas | 0:c46b3e0a8c2b | 36 | * SHT40 i2c Humidity and Temperature sensor. |
reedas | 0:c46b3e0a8c2b | 37 | * |
reedas | 0:c46b3e0a8c2b | 38 | */ |
reedas | 0:c46b3e0a8c2b | 39 | |
reedas | 0:c46b3e0a8c2b | 40 | #ifndef SHT40_H |
reedas | 0:c46b3e0a8c2b | 41 | #define SHT40_H |
reedas | 0:c46b3e0a8c2b | 42 | |
reedas | 0:c46b3e0a8c2b | 43 | /** |
reedas | 0:c46b3e0a8c2b | 44 | * Includes |
reedas | 0:c46b3e0a8c2b | 45 | */ |
reedas | 0:c46b3e0a8c2b | 46 | #include "mbed.h" |
reedas | 0:c46b3e0a8c2b | 47 | |
reedas | 0:c46b3e0a8c2b | 48 | /** |
reedas | 0:c46b3e0a8c2b | 49 | * Defines |
reedas | 0:c46b3e0a8c2b | 50 | */ |
reedas | 0:c46b3e0a8c2b | 51 | // Acquired from adafruit arduino SHT4x.h |
reedas | 0:c46b3e0a8c2b | 52 | |
reedas | 0:c46b3e0a8c2b | 53 | #define SHT40_I2C_ADDRESS 0x44 |
reedas | 0:c46b3e0a8c2b | 54 | #define SHT4x_NOHEAT_HIGHPRECISION 0xFD |
reedas | 0:c46b3e0a8c2b | 55 | #define SHT4x_NOHEAT_MEDPRECISION 0xF6 |
reedas | 0:c46b3e0a8c2b | 56 | #define SHT4x_NOHEAT_LOWPRECISION 0xE0 |
reedas | 0:c46b3e0a8c2b | 57 | #define SHT4x_HIGHHEAT_1S 0x39 |
reedas | 0:c46b3e0a8c2b | 58 | #define SHT4x_HIGHHEAT_100MS 0x32 |
reedas | 0:c46b3e0a8c2b | 59 | #define SHT4x_MEDHEAT_1S 0x2F |
reedas | 0:c46b3e0a8c2b | 60 | #define SHT4x_MEDHEAT_100MS 0x24 |
reedas | 0:c46b3e0a8c2b | 61 | #define SHT4x_LOWHEAT_1S 0x1E |
reedas | 0:c46b3e0a8c2b | 62 | #define SHT4x_LOWHEAT_100MS 0x15 |
reedas | 0:c46b3e0a8c2b | 63 | #define SHT4x_READSERIAL 0x89 |
reedas | 0:c46b3e0a8c2b | 64 | #define SHT4x_SOFTRESET 0x94 |
reedas | 0:c46b3e0a8c2b | 65 | #define I2C_SPEED_STANDARD 100000 |
reedas | 0:c46b3e0a8c2b | 66 | #define I2C_SPEED_FAST 400000 |
reedas | 0:c46b3e0a8c2b | 67 | |
reedas | 0:c46b3e0a8c2b | 68 | /** |
reedas | 0:c46b3e0a8c2b | 69 | * 2 byte packet is sent to device as instruction |
reedas | 0:c46b3e0a8c2b | 70 | * 6 byte packet is received from device |
reedas | 0:c46b3e0a8c2b | 71 | * received data includes checksum in byte 3 for bytes 1 and 2 |
reedas | 0:c46b3e0a8c2b | 72 | * and byte 6 for bytes 4 and 5. |
reedas | 0:c46b3e0a8c2b | 73 | */ |
reedas | 0:c46b3e0a8c2b | 74 | static uint8_t crc8(const uint8_t *data, int len); |
reedas | 0:c46b3e0a8c2b | 75 | |
reedas | 0:c46b3e0a8c2b | 76 | /** |
reedas | 0:c46b3e0a8c2b | 77 | * Adafruit SHT40 i2c digital humidity and temperature sensor. |
reedas | 0:c46b3e0a8c2b | 78 | */ |
reedas | 0:c46b3e0a8c2b | 79 | class SHT40 { |
reedas | 0:c46b3e0a8c2b | 80 | |
reedas | 0:c46b3e0a8c2b | 81 | public: |
reedas | 0:c46b3e0a8c2b | 82 | |
reedas | 0:c46b3e0a8c2b | 83 | /** |
reedas | 0:c46b3e0a8c2b | 84 | * Constructor. |
reedas | 0:c46b3e0a8c2b | 85 | * |
reedas | 0:c46b3e0a8c2b | 86 | * @param sda mbed pin to use for SDA line of I2C interface. |
reedas | 0:c46b3e0a8c2b | 87 | * @param scl mbed pin to use for SCL line of I2C interface. |
reedas | 0:c46b3e0a8c2b | 88 | */ |
reedas | 0:c46b3e0a8c2b | 89 | SHT40(PinName sda, PinName scl); |
reedas | 0:c46b3e0a8c2b | 90 | |
reedas | 0:c46b3e0a8c2b | 91 | |
reedas | 0:c46b3e0a8c2b | 92 | //Reads the temperature, input void, outputs an int in celcius. |
reedas | 0:c46b3e0a8c2b | 93 | int tempC(void); |
reedas | 0:c46b3e0a8c2b | 94 | |
reedas | 0:c46b3e0a8c2b | 95 | //Reads the relative humidity, input void, outputs and int. |
reedas | 0:c46b3e0a8c2b | 96 | int relHumid(void); |
reedas | 1:c6381a3f2d9a | 97 | //Reads the temperature, input void, outputs an int in celcius. |
reedas | 1:c6381a3f2d9a | 98 | float tempCF(void); |
reedas | 1:c6381a3f2d9a | 99 | |
reedas | 1:c6381a3f2d9a | 100 | //Reads the relative humidity, input void, outputs and int. |
reedas | 1:c6381a3f2d9a | 101 | float relHumidF(void); |
reedas | 0:c46b3e0a8c2b | 102 | |
reedas | 0:c46b3e0a8c2b | 103 | /* @ToDo populate a struct that returns temp and humidity in one call |
reedas | 0:c46b3e0a8c2b | 104 | * struct tempHumid{ |
reedas | 0:c46b3e0a8c2b | 105 | * int tempC; // or maybe a float? |
reedas | 0:c46b3e0a8c2b | 106 | * int relHumid; |
reedas | 0:c46b3e0a8c2b | 107 | * } |
reedas | 0:c46b3e0a8c2b | 108 | */ |
reedas | 0:c46b3e0a8c2b | 109 | |
reedas | 0:c46b3e0a8c2b | 110 | |
reedas | 0:c46b3e0a8c2b | 111 | private: |
reedas | 0:c46b3e0a8c2b | 112 | |
reedas | 0:c46b3e0a8c2b | 113 | I2C* i2c_; |
reedas | 0:c46b3e0a8c2b | 114 | |
reedas | 0:c46b3e0a8c2b | 115 | }; |
reedas | 0:c46b3e0a8c2b | 116 | |
reedas | 0:c46b3e0a8c2b | 117 | #endif /* SHT40_H */ |