Interfacing of Temperature Sensor with Arduino

This article is going to teach you about the temperature sensor and how to interface it with Arduino. Through this article, you are going to learn about how to read Analog value in Arduino and along with some useful function you may need.

Temperature Sensor

The temperature sensor we are using is LM35. The voltage output is directly proportional to the temperature around it. It can measure more accurately than a thermistor. It also posses low self-heating and does not cause more than 0.1 degree Celsius. The operating temperature range is -55 to 150-degree Celsius.

The Project

In this project, we are interfacing the temperature sensor with Arduino and controlling the brightness of an LED according to the reading of the temperature.

Interface Temperature Senor with Arduino

So, we have to interface it, big deal? Actually no. The interfacing is very easy with Arduino. I hope you already know how to control input/ output pin of Arduino. Arduino-UNO has 6 ADC channel, that means 6 numbers of ADC devices can be interfaced with it. Below is the circuit diagram.

Programming

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

void loop() {
 int value= analogRead(A0);
  Serial.println(value);
}

The code in the setup-block initializing the ADC as well as Serial communication. For more information about the serial, read this article. We have done this to so that we can see the data in the terminal window. In the loop section, we are continuously reading the value from the sensor and printing it to the terminal. Now click on Tools–> Serial Monitor and in the bottom right corner, change the baud rate to 9600. You should start seeing the values taken by the sensor to the terminal. But remember, our project was to glow LED based on the value. For that, we have to do certain changes in the program. Have a look-

void setup() {
  Serial.begin(9600);
  pinMode(2,OUTPUT);

}

void loop() {
 int val=analogRead(A0);
 Serial.print("Raw value-");
 Serial.println(val,DEC);
 val = map(val,0,1023,0,255);
 Serial.print("Value after processed -");
 Serial.println(val,DEC);
 analogWrite(2,val);
 delay(1000);
 
}

Also, you may have to notice the “MAP” function. It is there because the register of ADC is actually 10 bit. So, it is impossible to store that value in character. What this map function does is it maps the value between 0 to 1023 to 0 to 255 linearly. I know that you are thinking that I can store this value in “int”. You are correct and I can do that if I just have to interface, but I have to control the brightness of the LED and the maximum analog value I can write to analogWrite is 255.

Further Calibration

So, you may be thinking that the value of temperature is not correct. Yes, you are right. It is incorrect because we still need to calibrate it against the standard value. See, if you have to make a thermometer, you need a correct value of temperature. But if you don’t need that(like in the above case) you can be satisfied with these values as well and can still do your job. For example, you have to make a device that switches off the air-conditioning system if the temperature falls from certain voltage. Now, in this scenario, we know that the value is going to decrease as the temperature decreases. So, we just have to know the value at the threshold temperature and now just introduce a simple if condition. That’s it for this article, I hope this is useful to you. If you have any question or suggestion please let me know in the comment.

Recommended Post

ADC-https://avrgeeks.com/adc-in-atmega-16/ Serial-Communication-https://avrgeeks.com/communication-in-microcontroller/

Prakhar Bhatt

Prakhar is a recently graduated Engineer from Noida. As a student of Electronics, he has spent four years of his college life pondering over micro-processers and micro-controllers and bagged two research papers from projects in the same domain. Simply a geek.

Latest posts by Prakhar Bhatt (see all)

Share This