Bird
Raised Fist0
Arduinoprogramming~15 mins

Reading temperature sensor (LM35, TMP36) in Arduino - Deep Dive

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
Overview - Reading temperature sensor (LM35, TMP36)
What is it?
Reading temperature sensors like LM35 and TMP36 means using an Arduino to measure temperature by converting the sensor's voltage output into degrees Celsius. These sensors produce a small voltage that changes with temperature, which the Arduino reads through its analog input pins. The Arduino then translates this voltage into a temperature value that we can understand and use. This process lets us monitor temperature in projects like weather stations or thermostats.
Why it matters
Without the ability to read temperature sensors, devices wouldn't know the current temperature, making it impossible to automate heating, cooling, or safety systems. This concept solves the problem of turning tiny electrical signals into meaningful temperature data. It helps us build smart devices that respond to the environment, improving comfort, safety, and energy efficiency in everyday life.
Where it fits
Before learning this, you should understand basic Arduino programming and how to read analog signals. After mastering temperature sensor reading, you can explore more complex sensors, data logging, or wireless sensor networks to build advanced monitoring systems.
Mental Model
Core Idea
A temperature sensor outputs a small voltage that changes with temperature, and the Arduino reads this voltage to calculate the temperature value.
Think of it like...
It's like using a ruler to measure the length of a table: the sensor's voltage is the length on the ruler, and the Arduino reads that length to know the temperature.
┌───────────────┐      ┌───────────────┐      ┌───────────────┐
│ Temperature   │      │ Voltage       │      │ Arduino       │
│ Sensor (LM35, │─────▶│ changes with  │─────▶│ reads voltage │
│ TMP36)        │      │ temperature   │      │ and converts  │
└───────────────┘      └───────────────┘      │ to degrees °C │
                                               └───────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding Analog Voltage Output
🤔
Concept: Temperature sensors output a voltage that changes smoothly with temperature, not just on or off.
The LM35 and TMP36 sensors produce a voltage that increases as temperature rises. For example, LM35 outputs 10mV per degree Celsius. This means at 25°C, it outputs 250mV. The Arduino reads this voltage on an analog pin, which measures voltage as a number between 0 and 1023 (for 0 to 5V).
Result
You learn that the sensor's voltage is a continuous signal representing temperature.
Understanding that sensors output analog voltages helps you grasp how the Arduino translates real-world signals into numbers.
2
FoundationReading Analog Values on Arduino
🤔
Concept: Arduino converts analog voltage into a digital number using its analog-to-digital converter (ADC).
When you use analogRead(pin), Arduino measures the voltage on that pin and returns a number from 0 (0V) to 1023 (5V). For example, if the sensor outputs 2.5V, analogRead returns about 512. This number is the raw data you use to find temperature.
Result
You can get a number representing the sensor voltage from the Arduino.
Knowing how analogRead works is key to turning sensor voltages into usable data.
3
IntermediateConverting Raw ADC Value to Voltage
🤔Before reading on: do you think the Arduino's analogRead value directly equals the sensor voltage? Commit to your answer.
Concept: You must convert the raw ADC number back into voltage using the Arduino's reference voltage and ADC resolution.
The formula is: voltage = (analogRead value) * (5.0 / 1023). For example, if analogRead returns 512, voltage = 512 * (5.0 / 1023) ≈ 2.5V. This step translates the digital number back into the real voltage the sensor outputs.
Result
You get the sensor's voltage in volts, which is easier to use for temperature calculation.
Understanding this conversion prevents mistakes in interpreting sensor data and ensures accurate temperature readings.
4
IntermediateCalculating Temperature from Voltage
🤔Before reading on: do you think LM35 and TMP36 use the same formula to convert voltage to temperature? Commit to your answer.
Concept: Each sensor has its own formula to convert voltage to temperature based on its design.
For LM35: temperature (°C) = voltage (in mV) / 10. Since voltage is in volts, multiply by 1000 first. For TMP36: temperature (°C) = (voltage - 0.5) * 100. TMP36 outputs 0.5V at 0°C, so subtract 0.5V before multiplying.
Result
You can calculate the temperature in degrees Celsius from the sensor voltage.
Knowing the sensor-specific formula is essential for correct temperature readings.
5
IntermediateWriting Arduino Code for Temperature Reading
🤔
Concept: Combine reading analog input, converting to voltage, and calculating temperature in a program.
Example code snippet: int sensorPin = A0; void setup() { Serial.begin(9600); } void loop() { int rawValue = analogRead(sensorPin); float voltage = rawValue * (5.0 / 1023.0); float temperatureC = (voltage - 0.5) * 100; // TMP36 formula Serial.print("Temperature: "); Serial.print(temperatureC); Serial.println(" °C"); delay(1000); } This code reads the sensor every second and prints the temperature.
Result
The Arduino outputs the current temperature in Celsius to the serial monitor.
Seeing the full code helps connect theory to practice and builds confidence in sensor programming.
6
AdvancedImproving Accuracy with Calibration
🤔Before reading on: do you think the sensor always gives perfect temperature readings without adjustment? Commit to your answer.
Concept: Sensors can have small errors; calibration adjusts readings to be more accurate.
Calibration involves comparing sensor readings to a known accurate thermometer and adjusting the formula or adding an offset. For example, if the sensor reads 1°C too high, subtract 1 from the calculated temperature. Calibration can also involve averaging multiple readings to reduce noise.
Result
Temperature readings become more reliable and closer to true values.
Understanding calibration is crucial for real-world applications where precision matters.
7
ExpertHandling Sensor Noise and Environmental Factors
🤔Before reading on: do you think temperature readings are always stable and exact? Commit to your answer.
Concept: Sensor readings can fluctuate due to electrical noise, power supply variations, or environmental conditions, requiring filtering and careful design.
Techniques like averaging multiple readings, using capacitors to stabilize power, and shielding sensor wires reduce noise. Also, understanding sensor response time helps interpret readings correctly. For example, TMP36 has a response time of about 10 seconds, so sudden changes won't show immediately.
Result
You get smoother, more trustworthy temperature data and avoid false alarms or erratic behavior.
Knowing how to handle noise and delays prevents common bugs and improves system robustness.
Under the Hood
The LM35 and TMP36 sensors contain semiconductor components that produce a voltage proportional to temperature. This voltage is analog, meaning it can take any value within a range. The Arduino's analog-to-digital converter (ADC) samples this voltage at discrete intervals and converts it into a digital number between 0 and 1023, representing 0 to 5 volts. The Arduino then uses formulas specific to each sensor to translate this voltage into temperature. Internally, the ADC uses a comparator and a successive approximation register to perform this conversion quickly and accurately.
Why designed this way?
These sensors were designed to provide a simple, low-cost way to measure temperature with an easy-to-use voltage output. The analog voltage output allows compatibility with many microcontrollers without complex communication protocols. The Arduino ADC was designed to convert analog signals into digital values efficiently, enabling hobbyists and professionals to read sensors easily. Alternatives like digital sensors exist but require more complex code and hardware. The simplicity of voltage-output sensors and ADCs makes them popular for learning and many practical applications.
┌───────────────┐      ┌───────────────┐      ┌───────────────┐      ┌───────────────┐
│ Temperature   │      │ Sensor        │      │ Arduino ADC   │      │ Arduino Code  │
│ Environment   │─────▶│ Voltage Output│─────▶│ Converts      │─────▶│ Converts ADC  │
│ (Heat)        │      │ (Analog)      │      │ Voltage to    │      │ Value to      │
└───────────────┘      └───────────────┘      │ Digital Value │      │ Temperature   │
                                               └───────────────┘      └───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does analogRead return the voltage directly in volts? Commit to yes or no.
Common Belief:AnalogRead returns the voltage value directly in volts.
Tap to reveal reality
Reality:AnalogRead returns a number between 0 and 1023 representing the voltage scaled to the Arduino's reference voltage, not the voltage itself.
Why it matters:If you treat the raw analogRead value as voltage, your temperature calculations will be wrong by a large factor.
Quick: Do LM35 and TMP36 sensors use the same formula to convert voltage to temperature? Commit to yes or no.
Common Belief:All temperature sensors output voltage that converts to temperature using the same formula.
Tap to reveal reality
Reality:Different sensors have different voltage-to-temperature relationships; LM35 and TMP36 use different formulas.
Why it matters:Using the wrong formula leads to incorrect temperature readings, which can cause system failures or wrong decisions.
Quick: Is the sensor output always perfectly stable and noise-free? Commit to yes or no.
Common Belief:Temperature sensor readings are always stable and accurate without extra processing.
Tap to reveal reality
Reality:Sensor readings can fluctuate due to noise, power supply variations, and environmental factors, requiring filtering or averaging.
Why it matters:Ignoring noise can cause erratic readings and unreliable system behavior.
Quick: Can you connect the sensor output directly to any Arduino analog pin without considering power supply? Commit to yes or no.
Common Belief:You can connect the sensor output to any Arduino analog pin without worrying about power supply or wiring.
Tap to reveal reality
Reality:Proper wiring, stable power supply, and sometimes additional components like capacitors are needed for accurate readings.
Why it matters:Poor wiring or unstable power can cause incorrect readings or damage components.
Expert Zone
1
The ADC reference voltage can be changed from 5V to improve accuracy, but this requires recalculating voltage conversions accordingly.
2
Temperature sensors have a response time delay; understanding this helps interpret sudden temperature changes correctly.
3
Environmental factors like self-heating of the sensor or nearby heat sources can bias readings, requiring physical placement considerations.
When NOT to use
Analog temperature sensors like LM35 and TMP36 are not ideal for very low power or digital communication needs. In such cases, digital sensors like DS18B20 or I2C sensors are better alternatives because they provide digital output, better noise immunity, and easier integration with complex systems.
Production Patterns
In real-world systems, temperature sensors are often combined with calibration routines, noise filtering (like moving averages), and error handling. Multiple sensors may be used for redundancy. Data is often logged or sent wirelessly for monitoring. Power supply stabilization and sensor shielding are standard practices to ensure reliable operation.
Connections
Analog-to-Digital Conversion (ADC)
Builds-on
Understanding how ADC works is essential to correctly interpret sensor voltages and convert them into meaningful digital values.
Signal Filtering in Electronics
Related concept
Knowing how to filter noisy signals helps improve sensor reading stability and accuracy in embedded systems.
Human Sensory Perception
Analogy in biology
Just like sensors convert physical temperature to electrical signals, human senses convert stimuli into nerve signals the brain interprets, showing a universal pattern of signal conversion.
Common Pitfalls
#1Using raw analogRead value as temperature directly.
Wrong approach:int rawValue = analogRead(A0); float temperature = rawValue; // Wrong: rawValue is not temperature
Correct approach:int rawValue = analogRead(A0); float voltage = rawValue * (5.0 / 1023.0); float temperature = (voltage - 0.5) * 100; // TMP36 formula
Root cause:Misunderstanding that analogRead returns a scaled digital number, not voltage or temperature.
#2Applying LM35 formula to TMP36 sensor output.
Wrong approach:float temperature = voltage * 100; // LM35 formula used on TMP36
Correct approach:float temperature = (voltage - 0.5) * 100; // Correct TMP36 formula
Root cause:Assuming all temperature sensors have the same voltage-to-temperature relationship.
#3Ignoring sensor noise and reading fluctuations.
Wrong approach:float temperature = calculateTemperature(); Serial.println(temperature); // Prints raw fluctuating values
Correct approach:float sum = 0; for(int i=0; i<10; i++) { sum += calculateTemperature(); delay(10); } float temperature = sum / 10; // Averaged value Serial.println(temperature);
Root cause:Not accounting for electrical noise and sensor instability in readings.
Key Takeaways
Temperature sensors like LM35 and TMP36 output analog voltages that change with temperature, which Arduino reads as digital values.
Converting the Arduino's raw analogRead value to voltage and then to temperature requires sensor-specific formulas.
Calibration and noise handling are essential for accurate and stable temperature measurements in real-world applications.
Understanding the Arduino ADC and sensor characteristics prevents common mistakes and ensures reliable sensor integration.
Advanced use involves managing sensor response time, power supply stability, and environmental effects for professional-grade results.

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