Reading Temperature (And Humidity)
Reading Temperature (And Humidity)
– 11
Problem Statement:
Write an Arduino program that:
• Reads temperature data from a temperature sensor (e.g., LM35
or DHT11).
• Sends the temperature values to the Serial Monitor or the
computer.
Components Required:
• Arduino Board (UNO, Mega, etc.)
• Temperature Sensor (LM35 or DHT11)
• Breadboard
• Jumper Wires
• Arduino IDE
Circuit Connections:
#define TEMP_PIN A0
void setup() {
Serial.begin(9600);
Serial.println("Reading Temperature Sensor...");
}
void loop() {
int rawValue = analogRead(TEMP_PIN);
float voltage = rawValue * (5.0 / 1023.0);
float temperature = voltage * 100.0;
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000);
}
Arduino Code (for DHT11 Sensor):
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println("Reading Temperature and Humidity...");
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT11 sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000);
}
Output:
For LM35:
1. The program reads the temperature from the LM35 sensor every
second.
2. The Serial Monitor displays the temperature in the following
format:
o Temperature: 25.3 °C
o Temperature: 26.1 °C
o Temperature: 24.8 °C
For DHT11:
1. The program reads the temperature and humidity from the
DHT11 sensor every 2 seconds.
2. The Serial Monitor displays the values in the following format:
o Temperature: 27.5 °C
o Humidity: 55.2 %
Output:
1. When you turn the potentiometers, the color of the RGB LED
changes based on the combined Red, Green, and Blue intensity
values.
2. The Serial Monitor displays the current RGB values in the
format:
• Red: 120
• Green: 200
• Blue: 45
3. The LED color dynamically changes as you adjust the
potentiometers, creating a range of color combinations.