Temperature Humidity Sensor

Hello All,
I am at my wits end trying to use the sensor. No code I find runs without this frequent error:

DHT does not display a type

I am new to Arduino and running version Arduino 1.8.19 IDE.

I have installed libraries.

Below is latest code I have attempted to use. Again, it throws the error above:

#include "DHT.h"

#define DHTPIN 2 // Digital pin connected to the DHT sensor

#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));

dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("C "));
Serial.print(f);
Serial.print(F("F Heat index: "));
Serial.print(hic);
Serial.print(F("C "));
Serial.print(hif);
Serial.println(F("F"));
}

Please edit your post to add code tags.

If the library is properly installed, test the sensor with one of the library examples.
File>Examples>Examples from custom libraries

Instead of
#include "DHT.h"
try
#include <DHT.h>

Sorry about that:

#define DHTPIN 7 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));

dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("C "));
Serial.print(f);
Serial.print(F("F Heat index: "));
Serial.print(hic);
Serial.print(F("C "));
Serial.print(hif);
Serial.println(F("F"));
}

For informed help, please read and follow the directions in the "How to get the best out of the forum" post, linked at the head of every forum category.

jremington Happy now?

#define DHTPIN 7 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F(" Humidity: "));
  Serial.print(h);
  Serial.print(F("% Temperature: "));
  Serial.print(t);
  Serial.print(F("C "));
  Serial.print(f);
  Serial.print(F("F Heat index: "));
  Serial.print(hic);
  Serial.print(F("C "));
  Serial.print(hif);
  Serial.println(F("F"));
}

Hi, @ronpenrose
Welcome to the forum.

If you read that error and what is displayed before it, that should give you information as to where in your code problem is.
Also the line may be highlighted in your code.

The IDE has a "copy error messages" command at the bottom of the screen after each compile scan.
Copy and post the error messages in a new post, please.

Thanks... Tom... :smiley: :+1: :coffee: :australia:

you need to include the DHT header file at the start of the program

#include <DHT.h>

#define DHTPIN 7 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11

We need to know what Arduino board (or other brand, such as ESP32) you are using and exactly what other hardware you are connecting it to. ..
If you are using something that is a non-Arduino product then it helps to have a link to its technical data sheet or a link to where you bought it.

guess what - from

*Thanks all for your help. I apologize for not following the guidelines. I'm new here and going through learning curve so thanks for your patience :grinning:. Below is hardware information, complete code, and error message:

I am hoping when I submit this, the code tags will display correctly!

Computer: Arduino IDE on Windows 10.
Board: Arduino Mega 2560
Processor: ATMega2560
Port: COM4 (Arduino Mega or Mega2560
Programmer: ARVIS mkll
Sensor: DHT11 Temperature and Humidity Sensor Module (included in Ellegoo kit)
Installed Libraries: DHT and DHT20

Here is complete code:

#include <DHT.h>

#define DHTPIN 7     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F(" Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("C "));
  Serial.print(f);
  Serial.print(F("F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("C "));
  Serial.print(hif);
  Serial.println(F("F"));
}

Here is ERROR MSG:

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware -hardware C:\Users\Owner\Documents\ArduinoData\packages -hardware C:\Users\Owner\Documents\Arduino 1.8.19\hardware -tools C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\tools-builder -tools C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr -tools C:\Users\Owner\Documents\ArduinoData\packages -built-in-libraries C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\libraries -libraries C:\Users\Owner\Documents\Arduino 1.8.19\libraries -fqbn=arduino:avr:mega:cpu=atmega2560 -vid-pid=2341_0042 -ide-version=10819 -build-path C:\Users\Owner\AppData\Local\Temp\arduino_build_571960 -warnings=none -build-cache C:\Users\Owner\AppData\Local\Temp\arduino_cache_994987 -prefs=build.warn_data_percentage=75 -verbose C:\Users\Owner\Documents\Arduino 1.8.19_Projects\LCD Hello World\HelloWorld\HelloWorld.ino
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\arduino-builder -compile -logger=machine -hardware C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware -hardware C:\Users\Owner\Documents\ArduinoData\packages -hardware C:\Users\Owner\Documents\Arduino 1.8.19\hardware -tools C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\tools-builder -tools C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr -tools C:\Users\Owner\Documents\ArduinoData\packages -built-in-libraries C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\libraries -libraries C:\Users\Owner\Documents\Arduino 1.8.19\libraries -fqbn=arduino:avr:mega:cpu=atmega2560 -vid-pid=2341_0042 -ide-version=10819 -build-path C:\Users\Owner\AppData\Local\Temp\arduino_build_571960 -warnings=none -build-cache C:\Users\Owner\AppData\Local\Temp\arduino_cache_994987 -prefs=build.warn_data_percentage=75 -verbose C:\Users\Owner\Documents\Arduino 1.8.19_Projects\LCD Hello World\HelloWorld\HelloWorld.ino
Using board 'mega' from platform in folder: C:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr
Detecting libraries used...
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\cores\arduino" "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\variants\mega" "C:\Users\Owner\AppData\Local\Temp\arduino_build_571960\sketch\HelloWorld.ino.cpp" -o nul
Alternatives for DHT.h: [DHT@1.4.4]
ResolveLibrary(DHT.h)
-> candidates: [DHT@1.4.4]
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\cores\arduino" "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\variants\mega" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT" "C:\Users\Owner\AppData\Local\Temp\arduino_build_571960\sketch\HelloWorld.ino.cpp" -o nul
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\cores\arduino" "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\variants\mega" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT" "C:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT\DHT.cpp" -o nul
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\cores\arduino" "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\variants\mega" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT" "C:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT\DHT_U.cpp" -o nul
Alternatives for Adafruit_Sensor.h: [Adafruit_Unified_Sensor@1.1.6 Adafruit_Sensor-master@1.1.6]
ResolveLibrary(Adafruit_Sensor.h)
-> candidates: [Adafruit_Unified_Sensor@1.1.6 Adafruit_Sensor-master@1.1.6]
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\cores\arduino" "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\variants\mega" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\Adafruit_Sensor-master" "C:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT\DHT_U.cpp" -o nul
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\cores\arduino" "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\variants\mega" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\Adafruit_Sensor-master" "C:\Users\Owner\Documents\Arduino 1.8.19\libraries\Adafruit_Sensor-master\Adafruit_Sensor.cpp" -o nul
Generating function prototypes...
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\cores\arduino" "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\variants\mega" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\Adafruit_Sensor-master" "C:\Users\Owner\AppData\Local\Temp\arduino_build_571960\sketch\HelloWorld.ino.cpp" -o "C:\Users\Owner\AppData\Local\Temp\arduino_build_571960\preproc\ctags_target_for_gcc_minus_e.cpp"
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\tools-builder\ctags\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\Owner\AppData\Local\Temp\arduino_build_571960\preproc\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\cores\arduino" "-IC:\Users\Owner\Documents\Arduino 1.8.19\hardware\arduino\avr\variants\mega" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT" "-IC:\Users\Owner\Documents\Arduino 1.8.19\libraries\Adafruit_Sensor-master" "C:\Users\Owner\AppData\Local\Temp\arduino_build_571960\sketch\HelloWorld.ino.cpp" -o "C:\Users\Owner\AppData\Local\Temp\arduino_build_571960\sketch\HelloWorld.ino.cpp.o"
HelloWorld:6:1: error: 'DHT' does not name a type
DHT dht(DHTPIN, DHTTYPE);
^~~
C:\Users\Owner\Documents\Arduino 1.8.19_Projects\LCD Hello World\HelloWorld\HelloWorld.ino: In function 'void setup()':
HelloWorld:12:6: error: expected unqualified-id before '.' token
dht.begin();
^
C:\Users\Owner\Documents\Arduino 1.8.19_Projects\LCD Hello World\HelloWorld\HelloWorld.ino: In function 'void loop()':
HelloWorld:21:16: error: expected primary-expression before '.' token
float h = dht.readHumidity();
^
HelloWorld:23:16: error: expected primary-expression before '.' token
float t = dht.readTemperature();
^
HelloWorld:25:16: error: expected primary-expression before '.' token
float f = dht.readTemperature(true);
^
HelloWorld:34:18: error: expected primary-expression before '.' token
float hif = dht.computeHeatIndex(f, h);
^
HelloWorld:36:18: error: expected primary-expression before '.' token
float hic = dht.computeHeatIndex(t, h, false);
^
Multiple libraries were found for "Adafruit_Sensor.h"
Used: C:\Users\Owner\Documents\Arduino 1.8.19\libraries\Adafruit_Sensor-master
Not used: C:\Users\Owner\Documents\Arduino 1.8.19\libraries\Adafruit_Unified_Sensor
Using library DHT at version 1.4.4 in folder: C:\Users\Owner\Documents\Arduino 1.8.19\libraries\DHT
Using library Adafruit_Sensor-master at version 1.1.6 in folder: C:\Users\Owner\Documents\Arduino 1.8.19\libraries\Adafruit_Sensor-master
exit status 1
'DHT' does not name a type

Suspicious. Why two DHT libraries? Your error seems to indicate that it might be accessing the wrong one. Pick one and delete the other.

I only have the DHT libraray installed (directory called DHT_sensor_library)
your code of post #9 compiles and links OK

Problem solved. I deleted all DHT libraries and reinstalled. When I included the library it added DHT_U.h which I did not have in previous code. It is working fine now. Thank you all very much. I'm certain you will be hearing from me again. Next time will do homework better before posting.

1 Like

Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.