At a glance Led temperature gauge DHT11 project

Hi All,
I'm still quite new to coding so please bare with me and thanks in advance for reading this far . My latest project is to build a of thermometer that lights up different LEDs depending on what temperature range they have been assigned.
Temp is read from the DHT11 sensor. I'm using an UNO that I will later replace with just the AtMega328 to make the project smaller.
I have copied the code from the DHT11 examples library to get the sensor up and running. It works great and is quite accurate +- 1 degree. My coding problem is that the LED that should be on, flash, it does not stay on as desired. I have searched the forum for solutions but yet to no avail, Alas here I am. Infact I did manage to get them to stay on but they would not turn Off. Can anyone let me know if I'm on the right track with my if and else statements. Cheers.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#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);
#define HOT 12
#define warm 11
#define nice 10
#define cold 9
#define freezing 8

float HOTTemp = 34;      //Red
float warmTemp = 33;     //Orange
float niceTemp = 27;     //Green
float coldTemp = 20;     //Blue
float freezingTemp = 5;  //White
int dt = 100;

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();
  pinMode(HOT, OUTPUT);
  pinMode(warm, OUTPUT);
  pinMode(nice, OUTPUT);
  pinMode(cold, OUTPUT);
  pinMode(freezing, OUTPUT);

  
}

void loop() {
  // Wait a few seconds between measurements.
  delay(1000);

  // 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 hic (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 hic (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"));

  if (hic < freezingTemp ) {
    digitalWrite(freezing, HIGH);
    delay(dt);
    // digitalWrite(freezing,LOW);
  } else (hic > freezingTemp);
  {
    digitalWrite(freezing, LOW);
    delay(dt);
  }

  //cold 18
  if (hic < coldTemp && hic >= freezingTemp) {
    digitalWrite(cold, HIGH);
    delay(dt);
    // digitalWrite(cold,LOW);
  } else (hic > coldTemp && hic <= freezingTemp);
  {
    digitalWrite(cold, LOW);
    delay(dt);
  }

  //nice 27
  if (hic < niceTemp && hic >= coldTemp) {
    digitalWrite(nice, HIGH);
    delay(dt);
    // digitalWrite(nice,LOW);
  } else (hic > niceTemp && hic <= coldTemp);
  {
    digitalWrite(nice, LOW);
    delay(dt);
  }
  //warm 33
  if (hic < warmTemp && hic >= niceTemp) {
    digitalWrite(warm, HIGH);
    delay(dt);
    // digitalWrite(warm,LOW);
  } else (hic > warmTemp && hic <= niceTemp);
  {
    digitalWrite(warm, LOW);
    delay(dt);
  }

  //HOT 34
  if (hic >= HOTTemp) {
    digitalWrite(HOT, HIGH);
    delay(dt);
    // digitalWrite(HOT,LOW);
  } else (hic < HOTTemp);
  {
    digitalWrite(HOT, LOW);
    delay(dt);
  }

So as you can see, I'm not even close to being a good coder but I am to get better. I also just really enjoy the whole process of buiding electronic projects.

You're missing the if after else. It will never go into the braces below. It should be

else if (hic > freezingTemp);
  {
    digitalWrite(freezing, LOW);
    delay(dt);
  }

However, the else condition isn't needed. It could be

else 
  {
    digitalWrite(freezing, LOW);
    delay(dt);
  }

Why are you comparing to hic and not t?

Hi thanks for your reply, I will edit the code as you suggest.
I'm using hic as it was just in the code from the library, should I also change all instances to t?

Hi Karma,
Thanks, heaps the code now works and LED stays on for the duration of that temp range, I was over thinking it, something so simple. Thanks for teaching me.
You've helped me greatly, now when my wife says its too cold, I can point to the thermometer and say actually green LED says its "nice" . It should make for some happy banter between us.
Thanks again :slight_smile:
Here's the corrected code if it helps anyone.

if (hic < freezingTemp ) {
    digitalWrite(freezing, HIGH);
    delay(dt);
    // digitalWrite(freezing,LOW);
  } else 
  {
    digitalWrite(freezing, LOW);
    delay(dt);
  }

  //cold 18
  if (hic < coldTemp && hic >= freezingTemp) {
    digitalWrite(cold, HIGH);
    delay(dt);
    // digitalWrite(cold,LOW);
  } else
  //  (hic > coldTemp && hic <= freezingTemp);
  {
    digitalWrite(cold, LOW);
    delay(dt);
  }

  //nice 27
  if (hic < niceTemp && hic >= coldTemp) {
    digitalWrite(nice, HIGH);
    delay(dt);
    // digitalWrite(nice,LOW);
  } else
  //  (hic > niceTemp && hic <= coldTemp);
  {
    digitalWrite(nice, LOW);
    delay(dt);
  }
  //warm 33
  if (hic < warmTemp && hic >= niceTemp) {
    digitalWrite(warm, HIGH);
    delay(dt);
    // digitalWrite(warm,LOW);
  } else
  //  (hic > warmTemp && hic <= niceTemp);
  {
    digitalWrite(warm, LOW);
    delay(dt);
  }

  //HOT 34
  if (hic > HOTTemp) {
    digitalWrite(HOT, HIGH);
    delay(dt);
    // digitalWrite(HOT,LOW);
  }
   else
    // (hic < HOTTemp);
  {
    digitalWrite(HOT, LOW);
    delay(dt);
  }
  }
  

You also have an extra ';' in there that terminates the else clause (end of first line) so the code between the brackets will always get executed.

Good catch!

I've been looking for a simple project for students. I'm going to use this with a TMP36.
Thanks

Thanks Faraday for the pick up.
I watched a video on how to use libraries in Arduino, it was really good. I decided to clean up the code (please let me know if I could improve on it). so here is what I think is an improved version.

//Quick glance thermometer
#include "DHT.h"  //include libary

#define Type DHT11  //Tell the libary what type of sensor you are using.

int sensorPin = 2;        //assign pin for sensor DHT11
DHT HT(sensorPin, Type);  //create the object
int HOT = 12;             //assign output pins for each LED
int warm = 11;
int nice = 10;
int cold = 9;
int freezing = 8;
int dt = 1000;
// Assign temperatures to each LED
float HOTTemp = 33;      //Red LED
float warmTemp = 33;     //Orange LED
float niceTemp = 27;     //Green LED
float coldTemp = 20;     //Blue LED
float freezingTemp = 5;  //White LED
float humidity;
float tempC;
float tempF;


void setup() {
  Serial.begin(9600);  // start serial monitor
  HT.begin();          // start DHT11 sensor
  delay(dt);

  pinMode(HOT, OUTPUT);  //Define pins to be inputs or outputs.
  pinMode(warm, OUTPUT);
  pinMode(nice, OUTPUT);
  pinMode(cold, OUTPUT);
  pinMode(freezing, OUTPUT);
}

void loop() {
  humidity = HT.readHumidity();  // Tell the DHT11 what to read
  tempC = HT.readTemperature();
  tempF = HT.readTemperature(false);  // change to true if you also want F

  Serial.print("Humidity: ");  //Print to serial or other monitor
  Serial.print(humidity);
  Serial.println("% ");
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.println(" C ");
  //  Serial.print(tempF); //Uncomment if you want to print F
  //  Serial.println(" F ");
  delay(dt);


  //freezing < 5
  if (tempC < freezingTemp) {  //Turn ON White LED if tempC is below 5
    digitalWrite(freezing, HIGH);
    delay(dt);
  } else {
    digitalWrite(freezing, LOW);  //if not below 5 turn OFF
    delay(dt);
  }

  //cold < 18
  if (tempC < coldTemp && tempC >= freezingTemp) {  //Turn ON Blue LED if tempC between 5-18 degrees
    digitalWrite(cold, HIGH);
    delay(dt);
  } else {
    digitalWrite(cold, LOW);  //if not between 5-18 degrees turn OFF
    delay(dt);
  }

  //nice < 27
  if (tempC < niceTemp && tempC >= coldTemp) {
    digitalWrite(nice, HIGH);
    delay(dt);
  } else {
    digitalWrite(nice, LOW);
    delay(dt);
  }

  //warm < 33
  if (tempC < warmTemp && tempC >= niceTemp) {
    digitalWrite(warm, HIGH);
    delay(dt);
  } else {
    digitalWrite(warm, LOW);
    delay(dt);
  }

  //HOT > 33
  if (tempC > HOTTemp) {
    digitalWrite(HOT, HIGH);
    delay(dt);
  } else {
    digitalWrite(HOT, LOW);
    delay(dt);
  }
}

Also here is the wiring layout. Please not the DHT11 I used was only 3pin, so I left the forth pin unconnected in the layout below. Yours maybe different so you will need to check.

Thanks for sharing.
I don't know if this was directed to me. I plan on using a small analog temp sensor TMP36 and a single RGB LED. It will be similar to yours but different.

We might reduce it down to an ATtiny85 to demonstrate how to program them as well. We'll see.

Hey Karma,
Wow! That sounds cool. I did think of just using the one RGB LED but decided to stick with simple first.
As far as the sensor goes, I actually tried using the LM335Z but had no luck with the output results. I Googled the heck out of it, different formulas, tried adding in a resistor but the numbers just didn't work. I believe the raw data should be in Kelvin but wasn't accurate. Again, no doubt it was something simple. Anyway, I would be really keen to follow your project, will you be posting it? Also I have just purchased an pro micro atmega32u4 thought I might give it ago rather the the gigantic ATmega328 (haha), that I have used in a couple of other projects. So really keen to see how you would use the ATtiny85.
Thanks Karma

I've been mentoring a few 11 year olds but they are back in school now. Our electron herding will have to take a back seat to their school studies. I don't know if/when they'll have time to play Nano anytime soon.

I'll post the resulting code here if/when it becomes reality

That's a different animal. The TMP36, TMP37 is easier to work with but probably not as accurate in the end. The DHTs are by far the easiest to implement.

Ahhh ok, I'll get hold of a TMP36 as They are so compact.
Hey hopefully the Nano Gods will gift you all with an abundance of free time :slight_smile:
I just got the Pro Micro working, so onto the build . I'll also post some finished pics of the project here.

Just a slight nit

All of those variables are constants which should never change so it is best to declare them as such:

const int sensorPin = 2;

The compiler is smart enough to realize this an optimize away the variable.

Also, all of your variables are declared at the global scope but none of them need to be. They should be declared inside loop(). Best practice to limit variable scope as much as possible. You can even declare them as you use them for the first time.

void loop() {
  float humidity = HT.readHumidity();  // Tell the DHT11 what to read
  float tempC = HT.readTemperature();
  float tempF = HT.readTemperature(false);  // change to true if you also want F
  ...

Hey Faraday,
Ahhh ok . I've seen this done in many sketches, thanks for the tip, I'll try it out on my next attempt at coding. Really appreciate your input.

Ok folks, I've finished the project. It came up pretty alright, well from the outside anyway. So here are some pics. I went with the Pro Mini and a $5 garden solar light, with only slight modifications, for the encloser.



Well Done. Nice packaging

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