'DHT22' was not declared in this scope

I know this question comes up quite a lot both on here and on other forums, but the answers don’t seem to work for my situation.

The sketch is from random nerds website.

The error:
randomner_DHT:11:17: error: ‘DHT22’ was not declared in this scope
#define DHTTYPE DHT22 // DHT 22 (AM2302)
^
/home/rob/sketchbook/randomner_DHT/randomner_DHT.ino:15:17: note: in expansion of macro ‘DHTTYPE’
DHT dht(DHTPIN, DHTTYPE);
^
/home/rob/sketchbook/randomner_DHT/randomner_DHT.ino: In function ‘void setup()’:
randomner_DHT:21:7: error: ‘class DHT’ has no member named ‘begin’
dht.begin();
^
/home/rob/sketchbook/randomner_DHT/randomner_DHT.ino: In function ‘void loop()’:
randomner_DHT:30:17: error: ‘class DHT’ has no member named ‘readHumidity’
float h = dht.readHumidity();
^
randomner_DHT:32:17: error: ‘class DHT’ has no member named ‘readTemperature’
float t = dht.readTemperature();
^
randomner_DHT:34:17: error: ‘class DHT’ has no member named ‘readTemperature’
float f = dht.readTemperature(true);
^
randomner_DHT:44:18: error: ‘class DHT’ has no member named ‘computeHeatIndex’
float hi = dht.computeHeatIndex(f, h);
^
Using library Adafruit_Sensor at version 1.0.2 in folder: /home/rob/sketchbook/libraries/Adafruit_Sensor
Using library DHT in folder: /home/rob/sketchbook/libraries/DHT (legacy)
exit status 1
‘DHT22’ was not declared in this scope

#include <Adafruit_Sensor.h>

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include <DHT.h>
#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  Serial.println("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
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  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("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");
}
[code]

[/code]

There are far too many non-creative people in the world who have all developed DHT libraries with the same name. We don't know which bone-head created the library you are using and used the same name as dozens of other libraries. No fair asking questions about unknown libraries.

randomner_DHT:11:17: error: 'DHT22' was not declared in this scope
 #define DHTTYPE DHT22   // DHT 22  (AM2302)

is the error message but in your code:

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)

that line is commented out. I'm almost sure you didn't post the code you're actually working with.

These type of errors almost always show up if you didn't install the necessary library. Maybe you have installed a DHT library but there are about half a dozen different libraries with that name, so you should install exactly the same library as the original author did.

The posted code compiles for me. The DHT library that I have is available through the Library Manager. Go to your sketchbook, libraries folder and delete any dht libraries there. In the IDE go to Sketch, Include Libraries, and click Manage libraries. In the window that comes up, choose Sensors in the Topic drop down and enter dht in the filter box. Select and install the DHT sensor library by Adafruit. That should be the correct library for your code.

PaulS:
There are far too many non-creative people in the world who have all developed DHT libraries with the same name. We don't know which bone-head created the library you are using and used the same name as dozens of other libraries. No fair asking questions about unknown libraries.

There seems to be quite a few DHT sketches from different websites lie random nerd and circuitbasics etc where they fail to compie with the same error or expected primary-expression before '.' token.

Believe me, I have spent many hours googling these errors only to get nowhere.

pylon:

randomner_DHT:11:17: error: 'DHT22' was not declared in this scope

#define DHTTYPE DHT22  // DHT 22  (AM2302)




is the error message but in your code:



// Uncomment whatever type you're using!
//#define DHTTYPE DHT11  // DHT 11
#define DHTTYPE DHT22  // DHT 22  (AM2302)




that line is commented out. I'm almost sure you didn't post the code you're actually working with.

These type of errors almost always show up if you didn't install the necessary library. Maybe you have installed a DHT library but there are about half a dozen different libraries with that name, so you should install exactly the same library as the original author did.

I have corrected it now, as it should be DHT22 and not DHT11

Regarding the library, the author didn't say what library to use , so I am only guessing which one to use.

groundFungus:
The posted code compiles for me. The DHT library that I have is available through the Library Manager. Go to your sketchbook, libraries folder and delete any dht libraries there. In the IDE go to Sketch, Include Libraries, and click Manage libraries. In the window that comes up, choose Sensors in the Topic drop down and enter dht in the filter box. Select and install the DHT sensor library by Adafruit. That should be the correct library for your code.

groundFungus:
The posted code compiles for me. The DHT library that I have is available through the Library Manager. Go to your sketchbook, libraries folder and delete any dht libraries there. In the IDE go to Sketch, Include Libraries, and click Manage libraries. In the window that comes up, choose Sensors in the Topic drop down and enter dht in the filter box. Select and install the DHT sensor library by Adafruit. That should be the correct library for your code.

Marvellous, thank you.

It does now compile, but it's wierd that the Adafruit sensor was already included before when it would not compile. Is that one of life's little misterys.