DHT11 and LED-indicators

Hi Guys,

For now, I’m almost done with having my DHT11 being monitored. The serial monitor is doing just fine and I can read values for temperature and humidity. I’m trying to have a LED either to be HIGH or LOW if the right temperature is read. Every time I run my code the LED is either HIGH or LOW but isn’t changing when the read values are changing. The first try was to have the LED1 be HIGH if 20 was reached and kept, and be LOW if the temperature is anything between. The code is under here.

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// 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 <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 3 // 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 the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11

// See guide for details on sensor wiring and usage:
// Overview | DHT11, DHT22 and AM2302 Sensors | Adafruit Learning System

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

const int LED1=2;
const int LED2=5;

void setup() {
pinMode (LED1,OUTPUT);
pinMode (LED2,OUTPUT);
digitalWrite (LED1,LOW);
digitalWrite (LED2,LOW);

Serial.begin(9600);
// Initialize device.
dht.begin();
Serial.println(F(“DHTxx Unified Sensor Example”));
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println(F("------------------------------------"));
Serial.println(F(“Temperature Sensor”));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F(“°C”));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F(“°C”));
Serial.print (F(“Resolution: “)); Serial.print(sensor.resolution); Serial.println(F(“°C”));
Serial.println(F(”------------------------------------”));
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println(F(“Humidity Sensor”));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F(“Unique ID: “)); Serial.println(sensor.sensor_id);
Serial.print (F(“Max Value: “)); Serial.print(sensor.max_value); Serial.println(F(”%”));
Serial.print (F(“Min Value: “)); Serial.print(sensor.min_value); Serial.println(F(”%”));
Serial.print (F(“Resolution: “)); Serial.print(sensor.resolution); Serial.println(F(”%”));
Serial.println(F(”------------------------------------”));
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
}

void loop() {
// Delay between measurements.
delay(1000);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F(“Error reading temperature!”));
}
else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F(“°C”));

}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F(“Error reading humidity!”));
}
else {
Serial.print(F(“Humidity: “));
Serial.print(event.relative_humidity);
Serial.println(F(”%”));
}

{

dht.temperature().getEvent(&event);
if (event.temperature=20)
digitalWrite (LED1, HIGH);
else (LED1, LOW);
}

}

Best Frederik Sølver

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can I suggest you read Humidy and Temperature at the BEGINNING of the loop and store it in varaibles.
Then use the variables to do your displaying and comparisons.

I'm not sure what this is supposed to do?

 if (event.temperature=20)
  digitalWrite (LED1, HIGH);
  else (LED1, LOW);
  }

This might help;

https://www.arduino.cc/reference/en/language/structure/control-structure/else/

Thanks.. Tom... :slight_smile:

If You use Autoformat in the IDE and post the code using cide tags it will be better. Now it is unteadable.
Read the first topics like "How to use Forum", "How to attache code, pics etc."