Compilation error: 'DHT20' was not declared in this scope

Hello. I am not sure what did I do wrong that result in this error.

My sensor is DHT20 - AHT20 Pin Module - I2C Temperature and Humidity Sensor : ID 5183 : $4.50 : Adafruit Industries, Unique & fun DIY electronics and kits

> #include <DHT.h>
> #include "Adafruit_seesaw.h"
> Adafruit_seesaw ss;
> 
> #include <SPI.h>
> #include <Wire.h>
> #include <RTClib.h>
> #include <SD.h>
> 
> #define DHTTYPE DHT20
> #define ECHO_TO_SERIAL 1 //Sends datalogging to serial if 1, nothing if 0
> #define LOG_INTERVAL 360000 //milliseconds between entries (6 minutes = 360000)
> 
> DHT dht(DHTTYPE);
> RTC_DS1307 rtc;
> 
> const int sunlightPin = A2;
> const int chipSelect = 10;
> const int LEDPinGreen = 6;
> const int LEDPinRed = 7;
> const int solenoidPin = 3;
> const int wateringTime = 600000; //Set the watering time (10 min for a start)
> const float wateringThreshold = 25; //Value below which the garden gets watered
> 
> float soilTemp = 0; //Scaled value of soil temp (degrees F)
> float soilMoisture = 0; //Scaled value of volumetric water content in soil (percent)
> float humidity = 0; //Relative humidity (%)
> float airTemp = 0; //Air temp (degrees F)
> float heatIndex = 0; //Heat index (degrees F)
> float sunlight = 0; //Sunlight illumination in lux
> bool watering = false;
> bool wateredToday = false;
> DateTime now;
> File logfile;
> 
> /*
> Soil Moisture Reference
> 
> Air = 0%
> Really dry soil = 10%
> Probably as low as you'd want = 20%
> Well watered = 50%
> Cup of water = 100%
> */
> 
> void error(char *str)
> {
>   Serial.print("error: ");
>   Serial.println(str);
>   
>   // red LED indicates error
>   digitalWrite(LEDPinRed, HIGH);
>   
>   while(1);
> }
> 
> void setup() {
>   
>   //Initialize serial connection
>   Serial.begin(9600); //Just for testing
>   Serial.println("Initializing SD card...");
>   
>   
>   pinMode(chipSelect, OUTPUT); //Pin for writing to SD card
>   pinMode(LEDPinGreen, OUTPUT); //LED green pint
>   pinMode(LEDPinRed, OUTPUT); //LED red pin
>   pinMode(solenoidPin, OUTPUT); //solenoid pin
>   digitalWrite(solenoidPin, LOW); //Make sure the valve is off
>   analogReference(EXTERNAL); //Sets the max voltage from analog inputs to whatever is connected to the Aref pin (should be 3.3v)
>   
>   //Establish connection with DHT sensor
>   dht.begin();
>   
>   //Establish connection with real time clock
>   Wire.begin();
>   if (!rtc.begin()) {
>     logfile.println("RTC failed");
> #if ECHO_TO_SERIAL
>     Serial.println("RTC failed");
> #endif  //ECHO_TO_SERIAL
>   }
>   
>   //Set the time and date on the real time clock if necessary
>   if (! rtc.isrunning()) {
>     // following line sets the RTC to the date & time this sketch was compiled
>     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
>   }
>   
>   //Check if SD card is present and can be initialized
>   if (!SD.begin(chipSelect)) {
>     error("Card failed, or not present");        
>   }
>   
>   Serial.println("Card initialized.");
>   
>   // create a new file
>   char filename[] = "LOGGER00.CSV";
>   for (uint8_t i = 0; i < 100; i++) {
>     filename[6] = i/10 + '0';
>     filename[7] = i%10 + '0';
>     if (! SD.exists(filename)) {
>       // only open a new file if it doesn't exist
>       logfile = SD.open(filename, FILE_WRITE); 
>       break;  // leave the loop!
>     }
>   }
>   
>   if (! logfile) {
>     error("couldnt create file");
>   }
>   
>   Serial.print("Logging to: ");
>   Serial.println(filename);
>   
>   
>   logfile.println("Unix Time (s),Date,Soil Temp (F),Air Temp (F),Soil Moisture Content (%),Relative Humidity (%),Heat Index (F),Sunlight Illumination (lux),Watering?");   //HEADER 
> #if ECHO_TO_SERIAL
>   Serial.println("Unix Time (s),Date,Soil Temp (F),Air Temp (F),Soil Moisture Content (%),Relative Humidity (%),Heat Index (F),Sunlight Illumination (lux),Watering?");
> #endif ECHO_TO_SERIAL// attempt to write out the header to the file
> 
>   now = rtc.now();
>     
> }
> 
> void loop() {
>     
>   //delay software
>   delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
>   
>   //Three blinks means start of new cycle
>   digitalWrite(LEDPinGreen, HIGH);
>   delay(150);
>   digitalWrite(LEDPinGreen, LOW);
>   delay(150);
>   digitalWrite(LEDPinGreen, HIGH);
>   delay(150);
>   digitalWrite(LEDPinGreen, LOW);
>   delay(150);
>   digitalWrite(LEDPinGreen, HIGH);
>   delay(150);
>   digitalWrite(LEDPinGreen, LOW);
>   
>   //Reset wateredToday variable if it's a new day
>   if (!(now.day()==rtc.now().day())) {
>     wateredToday = false;
>   }
>   
>   now = rtc.now();
>   
>   // log time
>   logfile.print(now.unixtime()); // seconds since 2000
>   logfile.print(",");
>   logfile.print(now.year(), DEC);
>   logfile.print("/");
>   logfile.print(now.month(), DEC);
>   logfile.print("/");
>   logfile.print(now.day(), DEC);
>   logfile.print(" ");
>   logfile.print(now.hour(), DEC);
>   logfile.print(":");
>   logfile.print(now.minute(), DEC);
>   logfile.print(":");
>   logfile.print(now.second(), DEC);
>   logfile.print(",");
>  #if ECHO_TO_SERIAL
>   Serial.print(now.unixtime()); // seconds since 2000
>   Serial.print(",");
>   Serial.print(now.year(), DEC);
>   Serial.print("/");
>   Serial.print(now.month(), DEC);
>   Serial.print("/");
>   Serial.print(now.day(), DEC);
>   Serial.print(" ");
>   Serial.print(now.hour(), DEC);
>   Serial.print(":");
>   Serial.print(now.minute(), DEC);
>   Serial.print(":");
>   Serial.print(now.second(), DEC);
>   Serial.print(",");
> #endif //ECHO_TO_SERIAL
>   
>   //Collect Variables
>   soilTemp = ss.getTemp();
>   delay(20);
>   
>   soilMoisture = ss.touchRead(0)*(0.1538)-53.846;
>   delay(20);
>     
>   humidity = dht.readHumidity();
>   delay(20);
>   
>   airTemp = dht.readTemperature(true);
>   delay(20);
>   
>   heatIndex = dht.computeHeatIndex(airTemp,humidity);
>   
>   //This is a rough conversion that I tried to calibrate using a flashlight of a "known" brightness
>   sunlight = pow(((((150 * 3.3)/(analogRead(sunlightPin)*(3.3/1024))) - 150) / 70000),-1.25);
>   delay(20);
>   
>   //Log variables
>   logfile.print(soilTemp);
>   logfile.print(",");
>   logfile.print(airTemp);
>   logfile.print(",");
>   logfile.print(soilMoisture);
>   logfile.print(",");
>   logfile.print(humidity);
>   logfile.print(",");
>   logfile.print(heatIndex);
>   logfile.print(",");
>   logfile.print(sunlight);
>   logfile.print(",");
> #if ECHO_TO_SERIAL
>   Serial.print(soilTemp);
>   Serial.print(",");
>   Serial.print(airTemp);
>   Serial.print(",");
>   Serial.print(soilMoisture);
>   Serial.print(",");
>   Serial.print(humidity);
>   Serial.print(",");
>   Serial.print(heatIndex);
>   Serial.print(",");
>   Serial.print(sunlight);
>   Serial.print(",");
> #endif
>   
>   if ((soilMoisture < wateringThreshold) && (now.hour() > 19) && (now.hour() < 22) && (wateredToday = false)) {
>     //water the garden
>     digitalWrite(solenoidPin, HIGH);
>     delay(wateringTime);
>     digitalWrite(solenoidPin, LOW);
>   
>     //record that we're watering
>     logfile.print("TRUE");
> #if ECHO_TO_SERIAL
>     Serial.print("TRUE");
> #endif
>   
>     wateredToday = true;
>   }
>   else {
>     logfile.print("FALSE");
> #if ECHO_TO_SERIAL
>     Serial.print("FALSE");
> #endif
>   }
>   
>   logfile.println();
> #if ECHO_TO_SERIAL
>   Serial.println();
> #endif
>   delay(50);
>   
>   //Write to SD card
>   logfile.flush();
>   delay(5000);
> }
> Using library Grove_Temperature_And_Humidity_Sensor at version 1.0.1 in folder: C:\Arduino\libraries\Grove_Temperature_And_Humidity_Sensor 
> Using library Wire at version 1.0 in folder: C:\Users\keyge\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire 
> Using library Adafruit_seesaw_Library at version 1.6.2 in folder: C:\Arduino\libraries\Adafruit_seesaw_Library 
> Using library Adafruit_BusIO at version 1.12.0 in folder: C:\Arduino\libraries\Adafruit_BusIO 
> Using library SPI at version 1.0 in folder: C:\Users\keyge\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\SPI 
> Using library RTClib at version 2.0.3 in folder: C:\Arduino\libraries\RTClib 
> Using library SD at version 1.2.4 in folder: C:\Arduino\libraries\SD 
> exit status 1
> 
> Compilation error: 'DHT20' was not declared in this scope

Maybe you used the wrong library in the code you didn't post

There are 2 libraries I have tried.

  • Using library DHT_sensor_library at version 1.4.4 in folder: C:\Arduino\libraries\DHT_sensor_library
  • Using library Grove_Temperature_And_Humidity_Sensor at version 1.0.1 in folder: C:\Arduino\libraries\Grove_Temperature_And_Humidity_Sensor

See reply #2

#include <DHT.h>

#include "Adafruit_seesaw.h"

Adafruit_seesaw ss;

#include <SPI.h>

#include <Wire.h>

#include <RTClib.h>

#include <SD.h>

#define DHTTYPE DHT20

#define ECHO_TO_SERIAL 1 //Sends datalogging to serial if 1, nothing if 0

#define LOG_INTERVAL 360000 //milliseconds between entries (6 minutes = 360000)

DHT dht(DHTTYPE);

RTC_DS1307 rtc;

Welcome to my ignore list.
See reply #2

Sorry that I didn't post the whole code. I have now added everything to the post. I am just genuinely confused because you mentioned library so only reply with library code first. Again, I'm sorry if it upset you.

It is generally best if you read this before posting:

Some seasoned members get a bit annoyed at having to ask the same questions over and over again and requesting the same information that people have failed to provide. It is difficult to help people that don’t provide the correct information and the above guide is conveniently and prominently posted at the top of every section for precisely this reason. Read it, have a think and formulate your question while providing all the appropriate information and you will likely get the support you need

It's fine now I can answer my own post. DHT20 is not the same as DHT11 or DHT22 when it comes to coding as the DHT.h doesn't have DHT20 (it cant be declared hence that error) since DHT20 is actually uses standard I2C interface. Also Adafruit DHT20 is AHT20 in disguised so I tried this GitHub - adafruit/Adafruit_AHTX0: Arduino library for AHT10 and AHT20 sensors! and it makes sense.

Still have to say that the reply here is rude. I tried my best to figured out my mistakes base on reply2, look closely at library manager , edit and add more info as much as possible to the point that whole code is there. Yet I get look down and get lectured that I need to reformulate my question even though at the time that's the best I could do out of frustration and not to mention all the details of error/code/library/output/sensor I provided and I get treated like that.

I will recommend all my friends and relative to stay away from this forum from now on.