Introduction
In this experiment, you’ll learn how to measure temperature using the LM35 temperature sensor and an Arduino. The LM35 outputs a voltage directly proportional to the temperature in Celsius, making it easy to use with an Arduino's analog input.
Components Needed
Circuit Setup
- Place the LM35 sensor on the Arduino breadboard shield. It has three pins: VCC, Output, and GND (from left to right, facing the flat side).
- Connect the VCC pin of the LM35 to the 5V pin on the Arduino R4 Minima.
- Connect the GND pin of the LM35 to the GND pin on the Arduino.
- Connect the Output pin of the LM35 to the Arduino's analog input pin A0 using jumper cables.
Code for Temperature Measurement
Upload the following code to your Arduino to measure and display the temperature in Celsius on the Serial Monitor:
// LM35 temperature sensor connected to analog pin A0
const int sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
// Read the analog input from the LM35 sensor
int sensorValue = analogRead(sensorPin);
// Convert the analog value to voltage (0 - 5V)
float voltage = sensorValue * (5.0 / 1023.0);
// Convert voltage to temperature in Celsius
float temperatureC = voltage * 100.0;
// Print temperature to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
delay(1000); // Wait 1 second between readings
}
Explanation
The LM35 outputs 10mV per degree Celsius. By reading the analog value and converting it to a voltage, we can calculate the temperature as follows:
- First, the analog reading (0–1023) is converted to a voltage between 0 and 5V.
- The voltage is then multiplied by 100 to convert it to Celsius, since each 0.01V (10mV) represents 1°C.
This code displays the temperature on the Serial Monitor in Celsius.
Troubleshooting
- If the temperature reading is incorrect, check the wiring to ensure the LM35 sensor is correctly connected to the 5V, GND, and analog input pins.
- Ensure that the Serial Monitor is set to the correct baud rate (9600).
- If the temperature fluctuates, make sure the connections are secure and stable.