DHT22 problem with code

  • Only read the DHT22 once every 2.5 or more seconds.

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
    Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

It was a good idea to check it this way, saved a lot of time, I didn't think to skip the whole arduino cloud widgets thing. Thank you :slight_smile:

Good move. :+1:

Look in Files> Examples in the IDE and use an example that comes with the Library.

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

1 Like
#include "DHT.h"

#define DHTPIN D4     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHT22 test!"));
  dht.begin();
}

void loop() {
  delay(20000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.println(F(" C "));
  delay(20000);
 
}

Result:

��f��fx�� � f� ��xff���f x ~�� �fx x x �� ��f��fx�� � f� ��Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!

#include <dht.h>
#define dataPin 4 // Defines pin number to which the sensor is connected

dht DHT; // Creats a DHT object

void setup() 
{
	Serial.begin(9600);
}
void loop() 
{
	//Uncomment whatever type you're using!
	int readData = DHT.read22(dataPin); // DHT22/AM2302
	//int readData = DHT.read11(dataPin); // DHT11

	float t = DHT.temperature; // Gets the values of the temperature
	float h = DHT.humidity; // Gets the values of the humidity

	// Printing the results on the serial monitor
	Serial.print("Temperature = ");
	Serial.print(t);
	Serial.print(" ");
	Serial.print((char)176);//shows degrees character
	Serial.print("C | ");
	Serial.print((t * 9.0) / 5.0 + 32.0);//print the temperature in Fahrenheit
	Serial.print(" ");
	Serial.print((char)176);//shows degrees character
	Serial.println("F ");
	Serial.print("Humidity = ");
	Serial.print(h);
	Serial.println(" % ");
	Serial.println("");

	delay(2000); // Delays 2 secods
}

dht DHT; // Creats a DHT object?

I should just copy and paste or.. ??

Im sorry This is my third project. It's like I only understand 10% of what I'm doing :upside_down_face:

  • If you don’t have that library, install it.

  • Paste example into a new IDE sketch.

  • Your sensor might be damaged . . .

1 Like

Thanks to everyone for your time and help. :heart:

I ordered another sensor 2 hours ago :grin:.
Friday night or Saturday - next fight. :upside_down_face:

I'll let you know when I check the new sensor

  • When you get a new sensor do this.

Edit

  • Oh, I see you are using a module.
1 Like

I use nail polish. Srsly.

Works great on tools, too (identify the size - red, green, blue, black, yellow).

1 Like

Yes, but thank you, I will use it in other things :slightly_smiling_face:

So yes, my sensor was damaged. :woman_facepalming:t2:

I checked new sensor with the library code and also my code.
Both codes work when I open "serial monitor".

Dashboard on arduino cloud doesn't work.
I will work on it tomorrow.

Maybe I should change something in "Variables"?

Restart cloud session or restart Arduino or restart computer.

If anyone needs it, fixed code and variables

 /*
Arduino IoT Cloud Variables description
  The following variables are automatically generated and updated when changes are made to the Thing

  float voltage;
  CloudLight led;
  CloudTemperatureSensor temp;
  int bat_percentage;
  CloudRelativeHumidity humi;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"

#include "DHT.h"
#define DHTTYPE DHT22     // DHT 22
#define DHTPIN D4 //DHT22 Pin D4(GPIO 2)
#define led D2
DHT dht(DHTPIN, DHTTYPE);
int analogInPin  = A0;    // Analog input pin
int sensorValue; 
float calibration = 0.40; // Check Battery voltage using multimeter & add/subtract the value
void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  dht.begin();

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(2000); 
pinMode(led, OUTPUT);
  // Defined in thingProperties.h
  initProperties();
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}
void loop() {
  ArduinoCloud.update();

  
 sensorValue = analogRead(analogInPin);
  voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration); //multiply by two as voltage divider network is 100K & 100K Resistor
 
  bat_percentage = mapfloat(voltage, 3.2, 4.2, 0, 100); //2.8V as Battery Cut off Voltage & 4.2V as Maximum Voltage
 
  if (bat_percentage >= 100)
  {
    bat_percentage = 100;
  }
  if (bat_percentage <= 0)
  {
    bat_percentage = 1;
  }
 
  Serial.print("Analog Value = ");
  Serial.print(sensorValue);
  Serial.print("t Output Voltage = ");
  Serial.print(voltage);
  Serial.print("t Battery Percentage = ");
  Serial.println(bat_percentage);



  humi = dht.readHumidity();
  temp = dht.readTemperature();

 if (isnan(humi) || isnan(temp)) {
  Serial.println("Failed to read from DHT sensor!");
  }else{
  
  Serial.print("Humidity ");
  Serial.println(humi);
  Serial.print("Temperature ");
  Serial.println(temp);
 }
  delay(4000);   

 
  
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}



/*
  Since Led is READ_WRITE variable, onLedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedChange()  {
  // Add your code here to act upon Led change
  if(led)
  {
    digitalWrite(led,HIGH);
    Serial.println("ON");
  }
  else
  {
    digitalWrite(led,LOW);
    Serial.println("OFF");
    delay(200);
    pinMode(led, INPUT);
  }
}

    

Thank you :heart::heart: