Skip to content

Commit 1c0ffeb

Browse files
pbjpkasEeeeBin
authored andcommitted
Fixed: DHT12 incorrect value when temperature is negative. (#200)
* Fixed incorrect value when temperature is negative. Corrected calculation by referring to the bit indicating whether the temperature is positive or negative (byte address 0x03, bit 8). * add new-line in end-of-file.
1 parent c88cf33 commit 1c0ffeb

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

examples/Modules/DHT12/DHT12.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,22 @@ float DHT12::readTemperature(uint8_t scale)
3434
float resultado=0;
3535
uint8_t error=read();
3636
if (error!=0) return (float)error/100;
37+
38+
resultado=datos[2]+(float)(datos[3]&0x7f)/10;
39+
if(datos[3]&0x80)
40+
{
41+
resultado = -resultado;
42+
}
43+
3744
if (scale==0) scale=_scale;
3845
switch(scale) {
3946
case CELSIUS:
40-
resultado=(datos[2]+(float)datos[3]/10);
4147
break;
4248
case FAHRENHEIT:
43-
resultado=((datos[2]+(float)datos[3]/10)*1.8+32);
49+
resultado=resultado*1.8+32;
4450
break;
4551
case KELVIN:
46-
resultado=(datos[2]+(float)datos[3]/10)+273.15;
52+
resultado=resultado+273.15;
4753
break;
4854
};
4955
return resultado;

0 commit comments

Comments
 (0)