The output from dht sensor is always 0, no matter humidity or temperature. Can someone help me, thanks.
The cpp and header is from this website.
[DHT - Temp and Humidity sensor device driver. | Mbed]
Hello,
usually is good to share also MbedOs version.
However, I tried that library with MbedOS 5.15.8 on Nucleo-F767ZI and output is OK
Temperature in Kelvin: 300.15, Celcius: 27.00, Farenheit 80.60
Humidity is 47.00, Dewpoint: 14.75, Dewpoint fast: 14.71
But you miss important part, you have to call readData()
function. Without it you will read 0 every time because the rest of functions only calculate a conversion of measured data.
#include "mbed.h"
#include "DHT.h"
DHT sensor(D4, DHT22);
int main()
{
int error = 0;
float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
while(1) {
thread_sleep_for(2000);
error = sensor.readData();
if (0 == error) {
c = sensor.ReadTemperature(CELCIUS);
f = sensor.ReadTemperature(FARENHEIT);
k = sensor.ReadTemperature(KELVIN);
h = sensor.ReadHumidity();
dp = sensor.CalcdewPoint(c, h);
dpf = sensor.CalcdewPointFast(c, h);
printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf);
} else {
printf("Error: %d\n", error);
}
}
}
For MbedOS6+ DHT - Fork of DHT component - https://os.mbed.com/teams… | Mbed also OK, but for that you also need to change printf lib in mbed_app.json
{
"target_overrides": {
"*": {
"target.printf_lib": "std"
}
}
}
BR, Jan
Thanks for your reply, I just change all include mbed.json file. The version is mbed-os 6.13.0. But it shows “Error:1”. You can ignore other codes.
type or paste code here#include "mbed.h"
#include "Joystick.h"
#include "N5110.h"
#include "DHT.h"
//#include "ManMode.h"
// main() runs in its own thread in the OS
N5110 lcd(PC_7, PA_9, PB_10, PB_5, PB_3, PA_10);
Joystick joystick(PC_3, PC_2);
DHT sensor(PA_1,DHT22);
//void displayTMP();
void main_menu();
void select_term1();
void displayMain_menu();
// Manegement mode
int main_Man();
void menu_Man();
void select_term2();
void display_Manmode();
void set_triggerTMP();
void set_triggerHum();
int main_op();
int main()
{
joystick.init();
lcd.init(LPH7366_1); //initialise for LPH7366-1 LCD (Options are LPH7366_1 and LPH7366_6)
lcd.setContrast(0.55); //set contrast to 55%
lcd.setBrightness(0.5);
lcd.clear();
//oid main_menu();
//while (true) {
//main_menu();
main_op() ;
//}
}
void main_menu(){
displayMain_menu();
//select_term1();
}
void displayMain_menu(){
lcd.printString(" Select Mode ", 0, 1);
lcd.printString(" Manage Mode ", 0, 3);
lcd.printString("Operation Mode ", 0, 4);
lcd.refresh();
}
/*void select_term1(){
if (joystick.get_direction() == N){
lcd.printString(" in Operation Mode ", 0, 2);
printf(" Direction: N\n");
main_Man();
}
}*/
int main_op(){
int error =0;
float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
while(1) {
thread_sleep_for(2000);
error = sensor.readData();
if (0 == error) {
c = sensor.ReadTemperature(CELCIUS);
f = sensor.ReadTemperature(FARENHEIT);
k = sensor.ReadTemperature(KELVIN);
h = sensor.ReadHumidity();
dp = sensor.CalcdewPoint(c, h);
dpf = sensor.CalcdewPointFast(c, h);
printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf);
} else {
printf("Error: %d\n", error);
}
}
}