So I have a basic code which I am using to attempt to learn how to program with. I am just trying to do a simple alarm, doesn't matter which I choose temperature or humidity here , so in my example ,just wanting to turn on a red led to indicate a high value. I have declared the red and green led's in my code and am reading temps and humidity just fine but am unable to get the led's to operate. Seems pretty simple, just use if statement but for some reason I am unable to see what I have done wrong here. my if statement is as follows: if(h<60){digitalWrite(redLed,HIGH)} and so on. Thanks , has to be something simple I am missing .
#include "DHT.h"
#define DHTPIN 4 // 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!
int redLed=D5; // Red led indicates a high temperature.
int greenLed=D0 ; // Green led indicates a normal temperature
#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 4 (on the right) of the sensor to GROUND
// 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!"));
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
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);
if (h<=60){
digitalWrite (redLed,HIGH);
digitalWrite(greenLed,LOW);
}
// 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"));
}
Did you try some simple example sketch to verify that the LEDs work by themselves? Is the serial output of values okay?
Yes I am getting readings from the serial monitor , it displays the current temperature as well as the humidity. I have jumper voltage to each led's to verify that they are actually working. I am using a ESP32 WeMos D1 R1 on a Windows 10 Pro computer and Arduino 1.8.13 software. Thanks
Larryfos:
Yes I am getting readings from the serial monitor , it displays the current temperature as well as the humidity. I have jumper voltage to each led's to verify that they are actually working. I am using a ESP32 WeMos D1 R1 on a Windows 10 Pro computer and Arduino 1.8.13 software. Thanks
That's a little compromised. It would be more conclusive to drive the LEDs from the pins so you can be sure they are connected and to the right pins.
I hope you never jumpered a live output pin to 3.3V or 5V. Do you have current limiting resistors on those LEDs? another thing to try is
// if (h<=60){
if (1){
digitalWrite (redLed,HIGH);
digitalWrite(greenLed,LOW);
}
Notice
float h = dht.readHumidity();
that h is declared as a float and can hold a number such as 10.5.
Notice that
if (h<=60)
where h, a float that can contain a 10.5 is being compared to a 60 which is an integer. I know it's a stretch but, in this case, comparing a float to an integer is like comparing apples to whales.
Try
if (h<=60.0f)
Aarg;
You asked if I tested or knew that the Led's are working, i believe. I tested the Led's outside of the circuit and yes they have 22o ohm resistors on the ground leg and the pins are connected to the positive leg of the resistor. I initially thought that the Led's may have been suspect but they are fine. I tried and I still have no led output, I must be missing something simple, thanks .
if (1){
digitalWrite (redLed,HIGH);
digitalWrite(greenLed,LOW); tried
Idahowalker;
Thanks for your suggestion :
if (h<=60.0f)
Still not working, I will just start from the beginning and see if I am missing a simple step, redo the Hudimity code then add the led and see what happens.
Larryfos:
Aarg;
You asked if I tested or knew that the Led's are working, i believe. I tested the Led's outside of the circuit and yes they have 22o ohm resistors on the ground leg and the pins are connected to the positive leg of the resistor. I initially thought that the Led's may have been suspect but they are fine. I tried and I still have no led output, I must be missing something simple, thanks .
if (1){
digitalWrite (redLed,HIGH);
digitalWrite(greenLed,LOW); tried
If that doesn't work, you have a hardware problem, not a software problem. Please post a connection diagram, a.k.a. schematic and images of your hardware if you can manage it.
Resistors don't have positive legs.
So have you tried to use the example blink an led code and modify the code to blink the led thingy? Reduce to the most basic and simple, see if that works then add in complications. Cause what code you post and all the other explanations it should work but it does not. So time to back down to the basic blink that LED test.
int redLed=D5;
void setup() {
pinMode(redLed,OUTPUT);
}
void loop()
{
digitalWrite( redLed, HIGH):
delay( 500 );
digitalWrite( redLed, LOW);
delay( 500 );
}
Can you get the above code to blink the red led?