0% found this document useful (0 votes)
11 views

Arduino Temp Sensor Code

The document is an Arduino sketch for managing multiple DHT11 temperature and humidity sensors. It initializes five sensors and corresponding LEDs, reads their data, and controls the LEDs based on defined thresholds. Additionally, it allows for dynamic threshold updates via serial input from the user.

Uploaded by

Ishan 8th B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Arduino Temp Sensor Code

The document is an Arduino sketch for managing multiple DHT11 temperature and humidity sensors. It initializes five sensors and corresponding LEDs, reads their data, and controls the LEDs based on defined thresholds. Additionally, it allows for dynamic threshold updates via serial input from the user.

Uploaded by

Ishan 8th B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <DHT.

h>
#include <DHT_U.h>

#define NUM_SENSORS 5 // Number of sensors


#define DHTTYPE DHT11

// Sensor pins
int dhtPins[NUM_SENSORS] = {2, 3, 4, 5, 6};

// LED pins
int ledPins[NUM_SENSORS] = {7, 8, 9, 10, 11};

// Thresholds
float lowThresholds[NUM_SENSORS] = {0, 0, 0, 0, 0};
float highThresholds[NUM_SENSORS] = {100, 100, 100, 100, 100};

// DHT objects
DHT dht[NUM_SENSORS] = {
DHT(dhtPins[0], DHTTYPE),
DHT(dhtPins[1], DHTTYPE),
DHT(dhtPins[2], DHTTYPE),
DHT(dhtPins[3], DHTTYPE),
DHT(dhtPins[4], DHTTYPE)
};

void setup() {
Serial.begin(9600);

// Initialize sensors and LEDs


for (int i = 0; i < NUM_SENSORS; i++) {
dht[i].begin();
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Make sure LEDs are initially off
}
}

void loop() {
String data = "";

// Read temperature and humidity, control LEDs


for (int i = 0; i < NUM_SENSORS; i++) {
float temp = dht[i].readTemperature();
float hum = dht[i].readHumidity();

if (isnan(temp) || isnan(hum)) {
temp = -1;
hum = -1;
}

// Add sensor data to the output string


data += String(temp) + "," + String(hum);
if (i < NUM_SENSORS - 1) data += ",";

// Control LEDs based on thresholds


if (temp > highThresholds[i] || temp < lowThresholds[i]) {
digitalWrite(ledPins[i], HIGH); // Turn on the LED if out of range
} else {
digitalWrite(ledPins[i], LOW); // Turn off the LED if within range
}
}

// Send data over Serial


Serial.println(data);

// Check for incoming threshold updates


if (Serial.available()) {
String input = Serial.readStringUntil('\n');
parseThresholds(input); // Update thresholds if data is received
}

delay(1000); // Update every second


}

void parseThresholds(String input) {


int index = 0;
int separatorCount = 0;
String value = "";

for (int i = 0; i < input.length(); i++) {


char c = input[i];
if (c == ',') {
updateThreshold(separatorCount, value.toFloat());
separatorCount++;
value = "";
} else {
value += c;
}
}

// Update the last threshold


updateThreshold(separatorCount, value.toFloat());
}

void updateThreshold(int count, float val) {


if (count % 2 == 0) {
lowThresholds[count / 2] = val; // Update low threshold
} else {
highThresholds[count / 2] = val; // Update high threshold
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy