Arduino Project: Arduino and LM35 Sensor

Home / Arduino Project: Arduino and LM35 Sensor

Arduino Project: Arduino and LM35 Sensor

December 11, 2015 | Article | No Comments

LM35 is an integrated circuit sensor that can be used to measure temperature with an electrical output proportional to the temperature in Celcius. Compared to thermistor, this component give more accurate result and generate higher voltage than thermocouples which may not require output amplifier.

LM35

The scale factor for this sensor is .01V/oC and doesn’t require any external calibration or trimming and maintains an accuracy of +/-0.4 oC at room temperature and +/-0.8 oC over range of 0oC to 100 oC. It has three leg: VCC, V-out, GND. The VCC is input voltage to supply the IC. The V-out is the pin which output the result temperature reading. The GND is ground.

LM35Detail

Objective

  • Measure surrounding temperature.
  • Display the measured temperature to serial communication

Prerequisite

  • none

Requirement

Following is all materials I use for this tutorial.

  • Arduino Uno R3
  • LM35

You can change the Arduino board to higher specification if you like.

You also need an USB A to B cable for upload our program to our Arduino.

Circuit

Here is the circuit image, created using Fritzing.

SimpleTemperatureSensor

Make sure you have the pin and leg configured properly. We will connect V-out leg to pin Analog 0 or A0. Next we can sense the temperature by read the value of this pin.

Code

Let’s see what code we use for this project and discuss it later.

const unsigned int TEMP_SENSOR_PIN = 0;   // We mean analog 0
const float SUPPLY_VOLTAGE = 5.0;         // If you use 3.3V, write it here.
const unsigned int BAUD_RATE = 9600;

const float get_temperature() {
  const int sensor_voltage = analogRead(TEMP_SENSOR_PIN);
  const float voltage = sensor_voltage * SUPPLY_VOLTAGE / 1024;
  return (voltage * 100);
}

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

void loop() {
  Serial.print(get_temperature());
  Serial.println(" C");
  delay(1000);
}

We define the pin for reading as TEMP_PIN and assign this constant by 0. Remember that our reading will be in analog, so we will use analogRead() function instead of digitalRead() function. We use 5.0V from Arduino and write it as constant SUPPLY_VOLTAGE. If you use different voltage, write it there.

Result

Open serial communication on Arduino IDE. You will see something similar to this:

LM35SerialCommResult

Why it Works

The heart of this project is get_temperature().

const float get_temperature() {
  const int sensor_voltage = analogRead(TEMP_SENSOR_PIN);
  const float voltage = sensor_voltage * SUPPLY_VOLTAGE / 1024;
  return (voltage * 100);
}

First, we read the output of sensor. The value range from 0 to 1023, which is a scale actually. We multiply the output with our SUPPLY_VOLTAGE, we got the exact value which has V unit. Division with 1024 is applied as the highest output is 1024 value. Because the precision is 10 mV/oC, we should convert the Volt unit to mV unit which we next multiply it by 1000. But wait, we then should divide it with 10 the scale, so we can get the shortcut: just multiply the voltage with 100. And that’s the recipe.

You can revise the code to make compact code by using magic number.

const float get_temperature() {
  return (analogRead(TEMP_SENSOR_PIN) * 0.48828125);
}

Where did we get this magic number? You should figure it by yourself 😉

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial