Creating a temp controled LED

hi there. i am trying to make a code that when the tempature turns to 70 degres farenhieght it will turn on an led. this is my code

#include <DHT.h>

// Define DHT sensor type and pin
#define DHTPIN 2 // Pin where the data pin of the DHT sensor is connected
#define DHTTYPE DHT11 // DHT11 or DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
// Initialize the built-in LED as an output
pinMode(LED_BUILTIN, OUTPUT);

// Initialize the DHT sensor
dht.begin();

// Start serial communication for debugging
Serial.begin(9600);
}

void loop() {
// Read the temperature in Fahrenheit
float temperature = dht.readTemperature(true);

// Check if the reading is valid
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Print temperature to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °F");

// If the temperature is above 68°F, make the LED blink
if (temperature > 68) {
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on
delay(300); // Wait for 300 ms
digitalWrite(LED_BUILTIN, LOW); // Turn the LED off
delay(300); // Wait for 300 ms
} else {
digitalWrite(LED_BUILTIN, LOW); // Ensure the LED is off if temperature <= 68
}
}
can somebody help? i just started and am 9 so can you create a diagram? thank you.

1 Like
  • How about you make an attempt at drawing a diagram with a pencil and paper; show it to us.

  • Show us a good image of your wiring.

is there an app ?

You should read the pinned post re 'How to get the best from the forum'. Also learn to use Tools/Auto Format before posting code.

Pencil. Paper. Cellphone camera.

a7

1 Like

Format your code.

Got it. Thank you for the tip. I will do some more exploring on the forum

Copy that. Thank you

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