DHT11 Temp/humidity sketch trouble

What to do to get this to work?

I`[code]
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors
// - Adafruit Unified Sensor Lib: GitHub - adafruit/Adafruit_Sensor: Common sensor library

#include "DHT.h"

#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

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

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

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

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
[/code]strong text`

Why?
What is wrong?

Please remember to use code tags when posting code

Why? It does not do anything.
What is wrong? I have no idea.
Is not the sketch showing //code at the top and bottom?

Truth is or was it was failing to upload. One thing had to be done was to "uncomment the type of sensor" whatever that means. I noticed DHT22 did not have // in front so I put // in front of DHT22 and removed // from in front of DHT11.

The big thing was additional libraries were needed to be added to make the sketch work. Once these were added it uploaded and ran superbly!

I guess I am making progress...

And it isn't failing now?

Do you see this printed in the serial monitor?

You have to uncomment the sensor that you have. So take away the // from the DHT11 and comment the other ones. Try that and see if it works
Also if you put ~~~ before the code starts, and after the last line the code will show up on the screen in the proper way

I was trying to use the same code, and it failed to compile, because it was looking for DHT.h, but I could not find it in any library search.

I am good at following directions but only IF I understand what is being said or what is written! "Uncomment" made no sense to me BUT it did sink in that DHT22 appeared to be different from the other two. So I made DHT11 different. I guess that is equivalent to "selecting" the existing sensor to a normal person or a dinosaur like me!

Next post involving sketches I will try to get the proper presentation....

Yes commenting means adding the // before the line and uncommenting means removing the //.
As you can see throughout the code there are a lot of lines that have the // at the start. Those lines will not be read as part of the program. they are just information statements. If you uncomment one of them the code will not compile because it won't understand that line as proper coding.

FYI: You can edit your initial post with your code listed above, and add the 3 ~~~ at the beginning and end, and it will correct the coding display, so that someone can copy and run it on their computer, to maybe make any corrections needed

NOOOOOO...
Please post your working code in a new post.

Never go back and up date info that you have been asking help on, it upsets the flow of the thread and makes it confusing to other people that might need this thread to fix a similar problem.

Tom... :grinning: :+1: :coffee: :australia:

Ah, Ok thanks for that. I'll keep that in mind. :grinning_face_with_smiling_eyes:

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