DHT11 Temperature Sensor, LED Alarm

Please don't judge I am new to this and I know this seems stupid and basic but I have been trying to fix this all morning with no luck.

I am trying to make an LED turn red when the temperature of a bedroom drops below 16 Celsius and above 25 Celsius but for some reason the LED stays green and wont turn red.

below is my full code.

#include <DHT.h>
#include <DHT_U.h>

#include <Adafruit_Sensor.h>

#define DHTPIN 2
#define DHTTYPE DHT11

#define GreenLed 3
#define RedLed 4

DHT dht (DHTPIN, DHTTYPE);

int HighTemp = 25;
int LowTemp = 16;

void setup() {
// put your setup code here, to run once:
pinMode(GreenLed, OUTPUT);
pinMode(RedLed, OUTPUT);

Serial.begin(9600);
Serial.println("Temp Sensor V1.0");

dht.begin();
}

void loop() {
// put your main code here, to run repeatedly:
delay(1000);

float humidity = dht.readHumidity();
float temp = dht.readTemperature();

if (isnan(humidity) || isnan (temp)) {
Serial.println("No DHT Sensor!");
return;
}

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temp);
Serial.println("°C");

if (temp > HighTemp && temp < LowTemp) {
digitalWrite(RedLed, HIGH);
digitalWrite(GreenLed, LOW);
}
else ( {
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
}
}

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

if (temp > HighTemp && temp < LowTemp)

How likely is it that the temperature will be above the high temperature and below the low temperature ?

Did you perhaps mean

if (temp > HighTemp || temp < LowTemp)

By the way, your code as posted does not compile and has an odd structure to it, particularly the else clause. Why does it start with a round bracket ?

1 Like

Hi HeliBob, Its only once every now and again the temperature can spike or fall.

Thanks for your edit to my code my green LED now turns off when needed but i still seem to have no response from the red led.

  if (temp > HighTemp || temp < LowTemp){
    digitalWrite(RedLed, HIGH);
    digitalWrite(GreenLed, LOW);
  }
  
  else {
    digitalWrite(GreenLed, HIGH);
    digitalWrite(RedLed, LOW);
    }

You have a hardware problem

How are the LEDs wired ?
What value of series resistor are you using ?
Have you got the read LED connected the right way
round ?
What happens if you interchange the pins used by the 2 LEDs ?

If you are using a 328 based board such as a Uno or Nano have you tried using LED_BUILTIN on pin 13 as one of the LEDs ?

So my Red LED is connected to pin 4 of the arduino uno using a 220ohm resistor on the ground side.

The Green LED is connected to pin 3 of the arduino using a 220ohm resistor on the ground side.

Everything is connected the right way round as far as I can tell.

I have tried using different pins on the arduino but still no luck.

I have used pin 13 for the onboard led and it works.

@UKHeliBob Seems to work now but my red LED seems really dim even with no resistors this is back on pin 4 of the uno.

Could this just be down to a bad LED

No resistor is asking for trouble - you may have damaged the LED or the Arduino pin. Put the resistor back and try a different pin. Switch out the LED too if you have spares.

i know i know haha the things i do for test purposes.

I am going to try different components now that i have the code working thanks.

Forget about code and test the hardware first

Wire the LED in series with its series resistor and connect it between 5V and GND on the Arduino

Does it light up ?

I managed to fix it. Once you fixed that snippet of code I changed out the red led to a different one and got the result I was looking for.

That's good news

Hardware substitution can be very helpful

Throw away the dodgy LED immediately !

Thanks mate, and I did it was just a generic LED from a kit not sure why but it was extremely dim.

Thanks for your help once again.

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