Need help dht error

Hi i took the advise of some people from here and started my code again
This is an egg hatcher machine
Its still very early in development that's why its missing most of its void loop(). I started writing the loop() and i began with the most obvious component the dht22 sensor.
My question is why is my LCD screen showing the temperature and humidity at 0%?
At some point before that when i first tried it on the serial monitor it said (nan) on both values but i figured that it was a library / coding error.
Now i cant figure this out its not a coding problem because the code is verified through the ide.
Its not a wiring problem either
temp hum 0
Here is my code below:

//Libraries
#include <Wire.h>
//This library allows you to communicate with I2C/TWI devices.
#include <LiquidCrystal_I2C.h>
//This library allows you to communicate with your LiquidCrystalDevice
#include <dht.h>
//This library allows you to communicate with your DHT22 sensor
#include <Servo.h>
//This library allows you to communicate with your servo motor

//Variables
float hum;  //Stores   humidity value
float temp; //Stores temperature value

//Constants
#define DHTPIN 3 //The pin 3 is used for DHT22
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define DHTTYPE DHT22 //The type of DHT sensor we are using  
dht DHT; //Initialize the DHT sensor
#define RELAYPIN 10 //The pin 10 is used for the Relay
#define SERVOMOTOR 9 //The pin 9 is used for the servo motor 
void setup() {
  Serial.begin(9600); //Allows us to vie our temp and hum values from the serial Monitor
  lcd.begin(16, 2); // This function initiates our lcd 16 is the number of columns and 2 is the number of rows.
  lcd.init(); //This function initializes the interface to the LCD
  lcd.clear(); // This function clears the LCD screen and positions the cursor in the upper-left corner
  lcd.backlight(); //This function turns on the LCD backlight
  pinMode(RELAYPIN, OUTPUT); //Declaring the relay pin as an output
  pinMode(SERVOMOTOR, OUTPUT); //Declaring the servomotor pin as an output
}
void loop() {
  byte CelsiusSymbol[] = {
    0b11100,
    0b10100,
    0b11100,
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00000
  };
  int chk = DHT.read22(DHTPIN);
  //Read data and store it to variables hum and temp
  hum = DHT.humidity;
  temp = DHT.temperature;
  //Print temp and humidity values to LCD
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temp);
  lcd.print('c');
  lcd.createChar(0, CelsiusSymbol ); // create a new custom character
  lcd.setCursor(11, 0); // move cursor to (11, 0)
  lcd.write((byte)0);  // print the custom char at (11, 0)
  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(hum);
  lcd.print("%");
  delay(2000); //Delay 2 sec between temperature/humidity check.
}




Always show us a good schematic of your proposed circuit. Show us a good image of your ‘actual’ wiring.
Give links to components.


Try the examples that come with the library.

I tried your code on my hardware, Uno, DHT22, I2C enabled 16 x 2 character LCD. As you can see in the photo, the code seems to be working just fine.

So your wiring and/or hardware is suspect. A schematic, like requested by @LarryD and photos that clearly show the wiring would be helpful.

26.4°C

:scream:

image

@kyrme - Do the relay and servo work?

How did the original code work without dht.begin()? ... aah... you fixdit! : )

Like I said, it works fine.
That library has no begin() function. Check it out >> GitHub - RobTillaart/DHTlib: Arduino library for DHT temperature and humidity sensor. AVR optimized

1 Like

You are magic... srsly... it works. I have been trying many incorrect DHT/dht/et c. libraries.

It is tricky to get random code off the net to work sometimes especially if there are many linbraries with similar names and you have to find the right one. I think it is better to get a library first and use the example code than to get the code and try to match the library.

1 Like

heart

Hi
Is there a website that can help me design my schematic?

I had a separate code on the servo(not written here) and its working great. The relay i just have it connected to the arduino as i still have to get the heat lamp and the base

A pencil and paper is fine.

+1 for hand drawn, photographed and posred.

This page may be of interest >> How to make a schematic you can post.

1 Like

I added some pictures
I will draw the digram when i have more time

So i have a 4.7k resistor so the resistance is not the problem. I should try to connect the dht sensor directly to the breadboard like in the picture and see what happens

Ready for the relay and motor!

So i found out what was wrong.
It was a wiring issue.
The dht sensor gave 0 readings when i had it connected with jumper wires ( jumper wires - Breadboard - jumper wires) but it finally gave me readings i connected the sensor directly to the breadboard.

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