We read temperature sensors to know the current temperature around us. This helps us control devices or show temperature on a screen.
Reading temperature sensor (LM35, TMP36) in Arduino
Start learning this pattern below
Jump into concepts and practice - no test required
int sensorPin = A0; float voltage, temperatureC; void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(sensorPin); voltage = sensorValue * (5.0 / 1023.0); temperatureC = voltage * 100.0; // For LM35 Serial.print("Temperature: "); Serial.print(temperatureC); Serial.println(" °C"); delay(1000); }
Use analogRead() to get sensor value from analog pin.
Convert sensor value to voltage, then to temperature in Celsius.
int sensorPin = A0; float voltage, temperatureC; void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(sensorPin); voltage = sensorValue * (5.0 / 1023.0); temperatureC = voltage * 100.0; // LM35 sensor Serial.print("Temp: "); Serial.print(temperatureC); Serial.println(" C"); delay(1000); }
int sensorPin = A0; float voltage, temperatureC; void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(sensorPin); voltage = sensorValue * (5.0 / 1023.0); temperatureC = (voltage - 0.5) * 100.0; // TMP36 sensor Serial.print("Temp: "); Serial.print(temperatureC); Serial.println(" C"); delay(1000); }
This program reads the LM35 temperature sensor connected to analog pin A0. It converts the sensor reading to voltage, then to temperature in Celsius. The temperature is printed to the Serial Monitor every second.
int sensorPin = A0; float voltage, temperatureC; void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(sensorPin); voltage = sensorValue * (5.0 / 1023.0); temperatureC = voltage * 100.0; // LM35 sensor Serial.print("Temperature: "); Serial.print(temperatureC); Serial.println(" °C"); delay(1000); }
Make sure your Arduino board uses 5V reference voltage for correct calculation.
For TMP36, subtract 0.5V from voltage before multiplying by 100.
Use Serial Monitor in Arduino IDE to see printed temperature values.
Use analogRead() to get sensor data from LM35 or TMP36.
Convert analog value to voltage, then to temperature in Celsius.
Print temperature to Serial Monitor to see results.
Practice
analogRead() function do when reading from an LM35 temperature sensor?Solution
Step 1: Understand analogRead() function
TheanalogRead()function reads the voltage level on an analog pin and returns a number between 0 and 1023 representing that voltage.Step 2: Relate to LM35 sensor output
The LM35 outputs an analog voltage proportional to temperature, soanalogRead()reads this voltage level.Final Answer:
It reads the voltage level from the sensor's analog output pin. -> Option DQuick Check:
analogRead() reads voltage level = A [OK]
- Thinking analogRead() converts voltage to temperature
- Assuming analogRead() sends commands to sensor
- Confusing analogRead() with digitalRead()
Solution
Step 1: Understand analog to voltage conversion
The analog reading ranges from 0 to 1023 for 0 to 5 volts. To get voltage, multiply reading by (5.0 / 1023.0).Step 2: Check each option
float voltage = analogRead(sensorPin) * (5.0 / 1023.0); correctly applies the formula. Others either divide incorrectly or multiply by wrong factors.Final Answer:
float voltage = analogRead(sensorPin) * (5.0 / 1023.0); -> Option CQuick Check:
Voltage = reading * (5/1023) [OK]
- Dividing by 5 instead of multiplying
- Using 1024 instead of 1023 in denominator
- Multiplying by 5 without dividing by 1023
int sensorPin = A0; int reading = 250; float voltage = reading * (5.0 / 1023.0); float temperatureC = voltage * 100; Serial.println(temperatureC);
Solution
Step 1: Calculate voltage from reading
voltage = 250 * (5.0 / 1023.0) ≈ 1.22 volts.Step 2: Calculate temperature in Celsius
temperatureC = 1.22 * 100 ≈ 122 °C. Serial.println displays approximately 122.19, closest to 122.0.Final Answer:
122.0 -> Option AQuick Check:
Voltage ≈1.22V, Temp = voltage*100 = 122 [OK]
- Forgetting to multiply voltage by 100
- Using wrong analog to voltage conversion
- Confusing TMP36 formula with LM35
int sensorPin = A0; int reading = analogRead(sensorPin); float voltage = reading / 1023 * 5.0; float temperatureC = (voltage - 0.5) * 100; Serial.println(temperatureC);
Solution
Step 1: Analyze voltage calculation
reading / 1023 * 5.0uses left-to-right precedence: firstreading / 1023(int / int = integer division, truncates), then * 5.0, yielding wrong voltage.Step 2: Rule out other options
A: Formula (voltage - 0.5)*100 correct for TMP36. B: Pin declaration int is fine. D: Serial.println prints floats fine.Final Answer:
The voltage calculation divides before multiplying, causing integer division error. -> Option AQuick Check:
Use float divisor to avoid integer division [OK]
- Using integer 1023 instead of float 1023.0
- Misunderstanding operator precedence
- Thinking Serial.println() can't print floats
Solution
Step 1: Confirm TMP36 formula
Temperature = (voltage - 0.5) * 100.Step 2: Check voltage conversion
Correct: reading * (5.0 / 1023.0). Avoid integer division like reading / 1023 * 5 (truncates) or 5 / 1023 (zero).Step 3: Verify loop structure
A1 pin, analogRead, Serial.println, delay(1000) must all align with correct math.Final Answer:
int sensorPin = A1; void loop() { int reading = analogRead(sensorPin); float voltage = reading * (5.0 / 1023.0); float temperatureC = (voltage - 0.5) * 100; Serial.println(temperatureC); delay(1000); } -> Option BQuick Check:
TMP36 temp = (voltage - 0.5)*100 with float math [OK]
- Using LM35 formula for TMP36 sensor
- Integer division in voltage calculation
- Using integer math like 5 / 1023 resulting in zero
