Hi all,
I am interfacing the HTU21D temp & humidity sensor with a NodeMCU V3.
I uploaded my code and it seems to run fine. But when I reset my nodeMcu or I re-upload the same code(meaning: exactly the same code!) I get no readings at all. When I remove the power supply and put it back (I know its not beautiful, but for testing purposes) it starts working. I soft reset the device also in code.
I have tried the following solutions:
- Soft reset in code (no success)
- Decoupling capacitor between VCC and GND (100nF)
- Shorter wires and/or different wires
- I used the Sparkfun library https://github.com/sparkfun/SparkFun_HTU21D_Breakout_Arduino_Library
I cannot seem to figure it out.
For reference, here is my code:
/*
This program interfaces the HTU21 temp/humidity sensor via I2C
*/
#include <Wire.h>
#include <Math.h>
const int HTU21_ADDR = 0x40;
const int Trigger_Temp_Measurement = 0xE3;// hold master
const int Trigger_Hum_Measurement = 0xE5; // hold master
const int Trigger_Temp = 0xF3; //No Hold Master
const int Trigger_Hum = 0xF5; //No Hold Master
int16_t Temp, Hum;//variables to store temp and humidity
int8_t TmpCrc, HumCrC;
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(HTU21_ADDR);
Wire.write(0xFE);
Wire.endTransmission(true);
}
void loop() {// put your main code here, to run repeatedly:
TempReading();
HumReading();
}
void TempReading() {
Wire.beginTransmission(HTU21_ADDR);
Wire.write(Trigger_Temp_Measurement);
Wire.endTransmission();
Wire.requestFrom(HTU21_ADDR, 3);
if (Wire.available() <= 3) {
Temp = (Wire.read() << 8) | (Wire.read() );
TmpCrc = Wire.read();
}
float TempCalc = -46.85 + (175.72 * ((float)Temp / (pow(2, 16))));
Serial.print("Temperature \t");
Serial.print(TempCalc);
//Serial.println(TmpCrc, HEX);
}
void HumReading() {
Wire.beginTransmission(HTU21_ADDR);
Wire.write(Trigger_Hum_Measurement);
Wire.endTransmission();
Wire.requestFrom(HTU21_ADDR, 3);
if (Wire.available() <= 3) {
Hum = (Wire.read() << 8) | (Wire.read());
HumCrC = Wire.read();
}
float HumCalc = -6 + 125 * (Hum / (pow(2, 16)));
Serial.print("\t Humidity \t");
Serial.println(HumCalc);
//Serial.println(HumCrC, HEX);
}
Anyone has an idea what the problem could be? I am using the GY-21 breakout board from AZ delivery.
Thanks in advance!