Hello. I'm a student who is new to arduino. I've been working on this sketch for a bit, but it won't work! The sketch should program my setup with a dht11 temperature and humidity sensor, along with an lcd, to display the temperature and humidity readings on the lcd in degrees fahrenheit.According to the code, if the sensor doesn't give a reading that makes sense, my lcd will display "failed to read dht" this is all that happens. I've tried fixing multiple things, looking over the code, and replacing my dht, but nothing seems to work. Do you think it's my setup, or my dht, or my code, or something else entirely? Please help!
Here's my code, please let me know if you see any errors:
```
#include <LiquidCrystal.h>
#include <dht.h> //this calls the DHT library
#define DHTPIN 13 // Pin connected to DHT
LiquidCrystal lcd(7,8,9,10,11,12)
#define DHTTYPE DHT11 // Define DHT type
;DHT dht(DHTPIN,DHTTYPE); //command to DHT library
void setup() {
Serial.begin (9600);
dht.begin(); //start the sensor
lcd.begin(16,2); // LCD screen is 16 lines by two columns
}
void loop() {
delay(2000);
float h = dht.readHumidity(); // value for humidity
float t = dht.readTemperature(); // value for temperature
t = t * 9 / 5 + 32; // Change reading from Celsius to Fahrenheit
if (isnan(t) || isnan(h)) { // Check that DHT sensor is working
lcd.setCursor(0,0);
lcd.print("Failed to Read from DHT");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("%/t");
Serial.print("Temperature: ");
Serial.print(t);
}
else { // Otherwise show the readings on the screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("f");
}
}
```
That makes sense. Also for the
float h = dht.readHumidity(); // value for humidity
float t = dht.readTemperature(); // value for temperature
I don't think I should move it because it's telling the code what t and h are equal to
I'm not sure. I'm trying to upload it, but there is an error "DHT does not name a type"
here: ;DHT dht(DHTPIN,DHTTYPE); //command to DHT library
do I need to put it as ;dht DHT(DHTPIN,DHTTYPE); instead?
So I can do that and I will try, but this is an example I edited just a bit.
// Example testing sketch for various DHT humidity/temperature
sensors. Written by ladyada, public domain. Copied and edited by milliemaven2014, May 2023
Also, do you have any ideas/suggestions for my "dht does not name a type" problem I commented about
I'm trying to upload it, but there is an error "DHT does not name a type" here: ;DHT dht(DHTPIN,DHTTYPE); //command to DHT library do I need to put it as ;dht DHT(DHTPIN,DHTTYPE); instead?
/* Example testing sketch for various DHT humidity/temperature
sensors. Written by ladyada, public domain. */
#include <LiquidCrystal.h>
#include "DHT.h" // Call the DHT library
#define DHTPIN 8 // Pin connected to DHT
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTTYPE DHT11 // Define the type of DHT module
DHT dht(DHTPIN, DHTTYPE); // Command to the DHT.h library
void setup() {
dht.begin(); // Start the sensor
lcd.begin(16, 2); // LCD screen is 16 characters by 2 lines
}
void loop() {
float h = dht.readHumidity(); // Value for humidity
float t = dht.readTemperature(); // Value for temperature
t = t * 9 / 5 + 32; // Change reading from Celsius to Fahrenheit
if (isnan(t) || isnan(h)) { // Check that DHT sensor is working
lcd.setCursor(0, 0);
lcd.print("Failed to read from DHT"); // If DHT is not working,
// display this
} else { // Otherwise show the readings on the screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("f");
}
}
This is the correct way to declare your DHT.
Change the one in your edited one to that.
Check your wiring for the DHT11
Note is that the same way you are declaring it?
Ok, I fixed the declaring and such problem and have checked my wiring. The wiring seems like everything is in place. Here's an pdf outline of my wiring setup on my breadboard: