In my previous article, I had explained how to
control your led using serial monitor and in this article, I'll show you the working with temperature sensor. After that, we will look into how to connect the LM35 Temperature Sensor to your Arduino board and how we can sketch the program for that and finally using the temperatures how we can control your LEDs. It's quiet an interesting topic.
Introduction
Before using the temperature sensor we want to know how could we convert the values between Celsius and Fahrenheit
°F = °C x 1.8 +32 //Fahrenheit
°C = (°F-32)/1.8 // Celsius
(or) we can use the simple trick for the conversion °C * 2 and °F + 30, using this trick we can find the value of Celsius and Fahrenheit
STEP1:
Requirements:
- Arduino Uno
- Bread board
- LM35 Temperature Sensor
- LED-2
- Some Wires
STEP2:
Connection:
Figure 1: LM35 Temperature Sensor
The Temperature Sensor has 3 pins:
- +Vc to 5v
- Vout to Analog-5
- Gnd to Gnd
- LED1 to 7
- LED2 to 6
After the completion of the connection, we want to sketch the following code to the Arduino board to identify the current temperature in the room.
STEP3:
Programming:
-
- int temp;
- int temppin = 5;
- int led1 = 7;
- int led2 = 6;
- void setup()
- {
- pinMode(led1, OUTPUT);
- pinMode(led2, OUTPUT);
- Serial.begin(9600);
- }
- void loop()
- {
- temp = analogRead(temppin);
- float t = (temp / 1024.0) * 5000;
- float celsius = t / 10;
- float farh = (celsius * 9) / 5 + 32;
-
- Serial.print("Temperature =");
- Serial.print(celsius);
- Serial.print("*C");
- Serial.println();
- delay(1000);
- if (celsius > 30)
- {
- digitalWrite(led1, HIGH);
- }
- else
- {
- digitalWrite(led2, LOW);
- }
- if (celsius < 30)
- {
- digitalWrite(led1, HIGH);
- }
- else
- {
- digitalWrite(led2, LOW);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
OUTPUT:
The following output of the room temperature is shown in the serial monitor screenshots below,
Explanation
Declare the variables first-named temp, temppin, led1, led2, then move on to the function mode
setup() and initialize the variables, pinModes, and
loop() function it is fully based upon your condition that how it wants to work. Inside the loop function, we have some values,
- temp=analogRead(temppin);
- float t=(temp/1024.0)*5000;
- float celsius=t/10;
- float farh=(celsius*9)/5+32;
And then we want to glow your LED-based up on the room temperature. For that here we are using IF conditional statement if(celsius>30), then led1 want to glow, else led2 want to glow. Based upon your own conditions the led will work automatically.
Conclusion
And finally, we conclude that we made Automated temperature based led on and off the system.