|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Sensirion AG |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * * Neither the name of Sensirion AG nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +#include <Wire.h> |
| 33 | + |
| 34 | +// SHT4x with I2C address A (SHT4x-AD1B) = 0x44 |
| 35 | +// SHT40 with I2C address A (SHT40-BD1B) = 0x45 |
| 36 | +const int16_t SHT_ADDRESS = 0x44; |
| 37 | + |
| 38 | +void setup() { |
| 39 | + Serial.begin(115200); |
| 40 | + // wait for serial connection from PC |
| 41 | + // comment the following line if you'd like the output |
| 42 | + // without waiting for the interface being ready |
| 43 | + while(!Serial); |
| 44 | + |
| 45 | + // output format |
| 46 | + Serial.println("RelativeHumidity(percent)\tTemperature(degC)"); |
| 47 | + |
| 48 | + // init I2C |
| 49 | + Wire.begin(); |
| 50 | + |
| 51 | + // wait until sensors are ready, < 1 ms according to datasheet |
| 52 | + delay(1); |
| 53 | +} |
| 54 | + |
| 55 | +void loop() { |
| 56 | + float temperature, humidity; |
| 57 | + uint8_t data[6], counter; |
| 58 | + |
| 59 | + // start sht measurement in high prescision |
| 60 | + Wire.beginTransmission(SHT_ADDRESS); |
| 61 | + Wire.write(0xFD); |
| 62 | + Wire.endTransmission(); |
| 63 | + |
| 64 | + // wait for measurement has finished according to datasheet > 8.2 ms |
| 65 | + delay(9); |
| 66 | + |
| 67 | + // read measurement data sht: 2 bytes T, 1 byte CRC, 2 bytes RH, 1 byte CRC |
| 68 | + Wire.requestFrom(SHT_ADDRESS, 6); |
| 69 | + counter = 0; |
| 70 | + while (Wire.available()) { |
| 71 | + data[counter++] = Wire.read(); |
| 72 | + } |
| 73 | + |
| 74 | + // floating point conversion according to datasheet |
| 75 | + // convert T in degC |
| 76 | + temperature = -45 + 175 * (float)((uint16_t)data[0] << 8 | data[1]) / 65535; |
| 77 | + // convert RH in % |
| 78 | + humidity = -6 + 125 * (float)((uint16_t)data[3] << 8 | data[4]) / 65535; |
| 79 | + |
| 80 | + Serial.print(humidity); |
| 81 | + Serial.print("\t"); |
| 82 | + Serial.print(temperature); |
| 83 | + Serial.println(); |
| 84 | + |
| 85 | + // wait 1 s for next measurement |
| 86 | + delay(1000); |
| 87 | +} |
0 commit comments