GY-63_MS5611/libraries/SHT2x/examples/SHT2x_nolib/SHT2x_nolib.ino

124 lines
2.0 KiB
Arduino
Raw Normal View History

2021-09-27 14:51:23 -04:00
//
// FILE: SHT2x_nolib.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test sensor without library
// URL: https://github.com/RobTillaart/SHT2x
#include "Wire.h"
2021-09-29 07:21:17 -04:00
uint8_t status = 0;
2021-09-27 14:51:23 -04:00
2021-11-17 12:59:50 -05:00
2021-09-27 14:51:23 -04:00
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Wire.begin();
Wire.setClock(100000);
testConnected();
}
void loop()
{
delay(1000);
float t = getTemperature();
2021-09-29 07:21:17 -04:00
Serial.print(t, 2);
Serial.print("\t");
Serial.println(status, HEX);
2021-09-27 14:51:23 -04:00
delay(1000);
float h = getHumidity();
2021-09-29 07:21:17 -04:00
Serial.print(h, 2);
Serial.print("\t");
Serial.println(status, HEX);
2021-09-27 14:51:23 -04:00
}
2021-09-29 07:21:17 -04:00
2021-09-27 14:51:23 -04:00
///////////////////////////////////////////////////
2021-09-29 07:21:17 -04:00
2021-09-27 14:51:23 -04:00
void testConnected()
{
Wire.beginTransmission(0x40);
int rv = Wire.endTransmission();
2021-09-29 07:21:17 -04:00
if (rv != 0) Serial.println("CONNECT FAILED");
2021-09-27 14:51:23 -04:00
else Serial.println("CONNECTED");
}
float getTemperature()
{
uint16_t raw = 0;
Wire.beginTransmission(0x40);
Wire.write(0xF3);
if (Wire.endTransmission() != 0)
{
Serial.println("ERR 1");
return 0.0;
}
delay(90);
Wire.requestFrom(0x40, 3);
uint32_t start = millis();
while (Wire.available() < 3)
{
if (millis() - start > 90)
{
Serial.println("ERR 2");
return 0.0;
}
yield();
}
raw = Wire.read() << 8;
raw += Wire.read();
2021-09-29 07:21:17 -04:00
status = raw & 0x0003;
2021-09-27 14:51:23 -04:00
raw &= 0xFFFC;
Serial.print("RAW TEM: ");
Serial.println(raw, HEX);
return -46.85 + (175.72 / 65536.0) * raw;
}
float getHumidity()
{
uint16_t raw = 0;
Wire.beginTransmission(0x40);
Wire.write(0xF5);
if (Wire.endTransmission() != 0)
{
Serial.println("ERR 11");
return 0.0;
}
delay(30);
Wire.requestFrom(0x40, 3);
uint32_t start = millis();
while (Wire.available() < 3)
{
if (millis() - start > 30)
{
Serial.println("ERR 12");
return 0.0;
}
yield();
}
raw = Wire.read() << 8;
raw += Wire.read();
2021-09-29 07:21:17 -04:00
status = raw & 0x0003;
2021-09-27 14:51:23 -04:00
raw &= 0xFFFC;
Serial.print("RAW HUM: ");
Serial.println(raw, HEX);
return -6.0 + (125.0 / 65536.0) * raw;
}
// -- END OF FILE --
2021-12-28 06:13:24 -05:00