Project Overview:
This project uses an Arduino to read temperature values and control a series of LEDs based on the temperature. The LEDs light up sequentially as the temperature increases, providing a visual indication of the temperature range.
How It Works:
A temperature sensor (e.g., LM35) is connected to the Arduino, which reads the temperature as an analog value. The Arduino then maps this value to a temperature in Celsius. Depending on the temperature, different combinations of LEDs are turned on to indicate the current temperature range.
Components List:
- Arduino Board (e.g., Uno, Nano, etc.)
- Temperature Sensor (e.g., LM35)
- 4 LEDs
- 220-ohm Resistors (for each LED)
- Jumper Wires
- Breadboard (optional)
- Power Supply (for Arduino)
Software List:
Arduino IDE (Integrated Development Environment)
Circuit Diagram and Connection:
Connect the temperature sensor's output pin to Analog Pin A0 on the Arduino.
Connect the anode (long leg) of each LED to Digital Pins 2 through 5 on the Arduino, and the cathode (short leg) to GND through a 220-ohm resistor.
Project Code:
int baselineTemp = 30;
int celsius;
void setup() {
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// Read and map the analog value from the temperature sensor to Celsius
celsius = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
// Control LEDs based on the temperature range
if (celsius < baselineTemp) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
} else if (celsius >= baselineTemp && celsius < baselineTemp + 10) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
} else if (celsius >= baselineTemp + 20 && celsius < baselineTemp + 30) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
} else if (celsius >= baselineTemp + 30 && celsius < baselineTemp + 40) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
} else if (celsius >= baselineTemp + 60) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}
delay(1000); // Wait for 1 second before the next reading
}
Explanation of the Code:
Variable Declaration:
int baselineTemp = 30; sets a baseline temperature of 30°C.
int celsius; is used to store the calculated temperature in Celsius.
Setup Function:
pinMode(A0, INPUT); sets the analog pin connected to the temperature sensor as an input.
pinMode(2, OUTPUT); through pinMode(5, OUTPUT); sets the LED pins as outputs.
Loop Function:
celsius = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125); reads the analog value from the temperature sensor and maps it to a temperature in Celsius.
The if statements control the LEDs based on the temperature range.
Test and Troubleshooting:
Testing: Upload the code to your Arduino. Adjust the temperature sensor (e.g., using a heat source) and observe the LEDs lighting up based on the temperature range.
Troubleshooting:
Ensure the temperature sensor is correctly connected to Analog Pin A0.
Verify that each LED is functioning properly by testing them individually.
Summary:
This project uses an Arduino and a temperature sensor to create a simple temperature indicator with LEDs. The project demonstrates basic analog reading, mapping values, and controlling LEDs based on conditional logic.
No comments