Bird
Raised Fist0
Arduinoprogramming~10 mins

Reading temperature sensor (LM35, TMP36) in Arduino - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Reading temperature sensor (LM35, TMP36)
Start
Setup pin modes
Loop start
Read analog value
Convert to voltage
Calculate temperature
Print temperature
Wait a moment
Repeat loop
The program sets up the sensor pin, then repeatedly reads the analog value, converts it to voltage, calculates temperature, prints it, and waits before repeating.
Execution Sample
Arduino
const int sensorPin = A0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  int reading = analogRead(sensorPin);
This code reads the analog value from the temperature sensor connected to pin A0.
Execution Table
StepActionVariableValueExplanation
1Setup serial communicationSerial9600 baudPrepare to send data to computer
2Read analog valuereading523Analog value from sensor (0-1023)
3Convert to voltagevoltage2.56Voltage = reading * (5.0 / 1023)
4Calculate temperaturetemperatureC256For LM35: temp = voltage * 100
5Print temperatureSerial outputTemperature: 256 CShow temperature on serial monitor
6Waitdelay1000 msPause 1 second before next reading
7Loop repeats--Go back to step 2
💡 Loop runs forever, continuously reading and printing temperature
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6
readingundefined523523523523523
voltageundefinedundefined2.562.562.562.56
temperatureCundefinedundefinedundefined256256256
Key Moments - 3 Insights
Why do we multiply the analog reading by (5.0 / 1023)?
Because the analogRead returns a number from 0 to 1023 representing 0 to 5 volts, so multiplying converts the reading back to voltage (see step 3 in execution_table).
How does the sensor voltage relate to temperature?
For LM35, each 0.01 volts equals 1 degree Celsius, so multiplying voltage by 100 gives temperature in Celsius (step 4).
Why do we use delay(1000) after printing?
To wait 1 second before reading again, so the output updates at a readable pace (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'reading' at step 2?
A25.6
B2.56
C523
D9600
💡 Hint
Check the 'Variable' and 'Value' columns at step 2 in execution_table.
At which step is the temperature calculated from voltage?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step where 'temperatureC' variable gets a value in execution_table.
If the sensor output voltage doubles, how does the temperature value change at step 4?
AIt doubles
BIt stays the same
CIt halves
DIt becomes zero
💡 Hint
Temperature is voltage multiplied by 100, so doubling voltage doubles temperature (see step 4).
Concept Snapshot
Arduino reads analog sensor value (0-1023)
Convert reading to voltage: voltage = reading * (5.0 / 1023)
Calculate temperature: tempC = voltage * 100 (for LM35)
Print temperature to Serial Monitor
Use delay to pause between readings
Loop repeats forever
Full Transcript
This Arduino program reads a temperature sensor like LM35 or TMP36 connected to analog pin A0. It starts by setting up serial communication at 9600 baud to send data to the computer. In the loop, it reads the analog value from the sensor, which is a number between 0 and 1023 representing voltage from 0 to 5 volts. The program converts this reading to voltage by multiplying by 5.0 divided by 1023. Then it calculates the temperature in Celsius by multiplying the voltage by 100 (because LM35 outputs 10mV per degree Celsius). The temperature is printed to the serial monitor. The program waits one second before repeating the process, so the temperature updates every second. This loop runs forever, continuously showing the current temperature.

Practice

(1/5)
1. What does the analogRead() function do when reading from an LM35 temperature sensor?
easy
A. It sets the sensor's output voltage to a fixed value.
B. It converts the temperature directly to Celsius.
C. It sends data to the sensor to start measuring temperature.
D. It reads the voltage level from the sensor's analog output pin.

Solution

  1. Step 1: Understand analogRead() function

    The analogRead() function reads the voltage level on an analog pin and returns a number between 0 and 1023 representing that voltage.
  2. Step 2: Relate to LM35 sensor output

    The LM35 outputs an analog voltage proportional to temperature, so analogRead() reads this voltage level.
  3. Final Answer:

    It reads the voltage level from the sensor's analog output pin. -> Option D
  4. Quick Check:

    analogRead() reads voltage level = A [OK]
Hint: Remember: analogRead() reads voltage, not temperature directly [OK]
Common Mistakes:
  • Thinking analogRead() converts voltage to temperature
  • Assuming analogRead() sends commands to sensor
  • Confusing analogRead() with digitalRead()
2. Which of the following is the correct way to convert the analog reading from an LM35 sensor to voltage in Arduino code?
easy
A. float voltage = analogRead(sensorPin) / 5.0;
B. float voltage = analogRead(sensorPin) * 1023.0 / 5.0;
C. float voltage = analogRead(sensorPin) * (5.0 / 1023.0);
D. float voltage = analogRead(sensorPin) * 5.0;

Solution

  1. 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).
  2. 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.
  3. Final Answer:

    float voltage = analogRead(sensorPin) * (5.0 / 1023.0); -> Option C
  4. Quick Check:

    Voltage = reading * (5/1023) [OK]
Hint: Use (5.0 / 1023.0) to convert analog reading to voltage [OK]
Common Mistakes:
  • Dividing by 5 instead of multiplying
  • Using 1024 instead of 1023 in denominator
  • Multiplying by 5 without dividing by 1023
3. What will be the output on the Serial Monitor if the following Arduino code reads an analog value of 250 from an LM35 sensor?
int sensorPin = A0;
int reading = 250;
float voltage = reading * (5.0 / 1023.0);
float temperatureC = voltage * 100;
Serial.println(temperatureC);
medium
A. 122.0
B. 12.2
C. 0.25
D. 1.22

Solution

  1. Step 1: Calculate voltage from reading

    voltage = 250 * (5.0 / 1023.0) ≈ 1.22 volts.
  2. Step 2: Calculate temperature in Celsius

    temperatureC = 1.22 * 100 ≈ 122 °C. Serial.println displays approximately 122.19, closest to 122.0.
  3. Final Answer:

    122.0 -> Option A
  4. Quick Check:

    Voltage ≈1.22V, Temp = voltage*100 = 122 [OK]
Hint: Multiply voltage by 100 to get Celsius for LM35 [OK]
Common Mistakes:
  • Forgetting to multiply voltage by 100
  • Using wrong analog to voltage conversion
  • Confusing TMP36 formula with LM35
4. Identify the error in this Arduino code snippet for reading TMP36 temperature sensor:
int sensorPin = A0;
int reading = analogRead(sensorPin);
float voltage = reading / 1023 * 5.0;
float temperatureC = (voltage - 0.5) * 100;
Serial.println(temperatureC);
medium
A. The voltage calculation divides before multiplying, causing integer division error.
B. The sensorPin should be declared as float, not int.
C. The temperature formula is incorrect for TMP36 sensor.
D. Serial.println() cannot print float values.

Solution

  1. Step 1: Analyze voltage calculation

    reading / 1023 * 5.0 uses left-to-right precedence: first reading / 1023 (int / int = integer division, truncates), then * 5.0, yielding wrong voltage.
  2. 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.
  3. Final Answer:

    The voltage calculation divides before multiplying, causing integer division error. -> Option A
  4. Quick Check:

    Use float divisor to avoid integer division [OK]
Hint: Use float numbers in division to avoid integer division [OK]
Common Mistakes:
  • Using integer 1023 instead of float 1023.0
  • Misunderstanding operator precedence
  • Thinking Serial.println() can't print floats
5. You want to read temperature from a TMP36 sensor connected to analog pin A1 and print the temperature in Celsius every second. Which Arduino code snippet correctly implements this?
hard
A. int sensorPin = A1; void loop() { int reading = analogRead(sensorPin); float voltage = reading / 1023 * 5; float temperatureC = voltage * 100; Serial.println(temperatureC); delay(1000); }
B. 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); }
C. int sensorPin = A1; void loop() { int reading = analogRead(sensorPin); float voltage = reading * (5 / 1023); float temperatureC = (voltage - 0.5) * 100; Serial.println(temperatureC); delay(1000); }
D. int sensorPin = A1; void loop() { int reading = analogRead(sensorPin); float voltage = reading * (5.0 / 1023.0); float temperatureC = voltage * 100; Serial.println(temperatureC); delay(1000); }

Solution

  1. Step 1: Confirm TMP36 formula

    Temperature = (voltage - 0.5) * 100.
  2. Step 2: Check voltage conversion

    Correct: reading * (5.0 / 1023.0). Avoid integer division like reading / 1023 * 5 (truncates) or 5 / 1023 (zero).
  3. Step 3: Verify loop structure

    A1 pin, analogRead, Serial.println, delay(1000) must all align with correct math.
  4. 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 B
  5. Quick Check:

    TMP36 temp = (voltage - 0.5)*100 with float math [OK]
Hint: Use (voltage - 0.5)*100 for TMP36 temperature [OK]
Common Mistakes:
  • Using LM35 formula for TMP36 sensor
  • Integer division in voltage calculation
  • Using integer math like 5 / 1023 resulting in zero