Skip to content
Tactiverge

School/△ Builder/Week 21

Reading the datasheet line

A temperature sensor says 750 mV. The datasheet has one line of maths. What is the temperature?

About 30 min · 3 goals · Checked by automated tests

1.Why should I care?

Many sensors are designed so their output is a straight line in the thing they measure. The datasheet gives you two numbers, an offset and a slope, and the firmware's job is one line of algebra. Get it wrong and your greenhouse thinks it is 50 °C in winter.

  • ◇ Write a sensor's output as a straight line: output = offset + slope × quantity.
  • ◇ Rearrange the line to turn a reading back into a physical quantity, with units.
  • ◇ Check a conversion by putting the answer back into the original formula.

2.Predict

A sensor outputs 500 mV at 0 °C and rises by 10 mV for each 1 °C. It reads 750 mV. What is the temperature?

Guess first. Being wrong here is how the surprise works.

3.Discover

Focus on stages 1 and 2. Move the temperature and watch the sensor output change. Write down the millivolts at 0 °C, 10 °C and 20 °C. What is the pattern?

Loading the interactive…

Can't use the interactive? Read a description instead

The pipeline diagram starts with a temperature slider. Stage 2 shows the sensor output in millivolts using 500 mV + 10 mV per °C: 500 mV at 0 °C, 600 mV at 10 °C, 700 mV at 20 °C. The output rises by the same amount for every degree, so it is a straight line.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A temperature sensor outputs 400 mV at 0 °C and rises by 19.5 mV for every 1 °C. It reads 205 mV. What temperature is it measuring, in °C?

°C

5.Understand

Offset and slope

A linear sensor's output is output = offset + slope × quantity. The offset is the output when the quantity is zero; the slope is how much the output changes per unit. A TMP36-style sensor has an offset of 500 mV (so it can show temperatures below 0 °C) and a slope of 10 mV per °C. An LM35-style sensor has 0 mV offset and 10 mV per °C.

Undo it in firmware

Firmware reads the output and needs the quantity, so it rearranges: quantity = (output − offset) ÷ slope. Subtract first, then divide. Doing it in the wrong order is one of the most common sensor bugs.

def temp_c(mv):
    return (mv - 500) / 10

Units are your safety net

Keep units on every number: (750 mV − 500 mV) ÷ (10 mV/°C) = 25 °C. The mV cancel, leaving °C. If the units come out as mV² or °C/mV, the formula is upside down.

Explain it another way

It is like a taxi fare: a fixed starting charge plus a price per kilometre. To find the distance from the fare, first remove the starting charge, then divide by the price per kilometre.

Derivation: Rearranging a sensor line
  1. Datasheet form: V = V₀ + k × T, with V₀ the output at 0 °C and k the millivolts per °C.

  2. Subtract V₀ from both sides: V − V₀ = k × T.

  3. Divide both sides by k: T = (V − V₀) ÷ k.

  4. Example (TMP36-style, V₀ = 500 mV, k = 10 mV/°C): V = 620 mV gives T = (620 − 500) ÷ 10 = 12 °C.

  5. Check: 500 + 10 × 12 = 620 mV. ✓

Worked example 1

An MCP9700A-style sensor gives 400 mV at 0 °C and 19.5 mV per °C. It reads 790 mV. Temperature?

T = (790 − 400) ÷ 19.5 = 390 ÷ 19.5 = 20 °C. Check: 400 + 19.5 × 20 = 790 mV. ✓

Common mix-ups

  • Divide the reading by the slope and you are done.
    ✓ Subtract the offset first. Only sensors with zero offset can skip that step.
  • Every sensor of the same type has exactly the datasheet numbers.
    ✓ Datasheet values are typical. Individual parts differ slightly, which is why calibration exists.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A temperature sensor outputs 500 mV at 0 °C and rises by 10 mV for every 1 °C. It reads 980 mV. What temperature is it measuring, in °C?

°C

Practice 3

A TMP36-style sensor (500 mV at 0 °C, +10 mV per °C) is at −15 °C. What does it output, in mV?

mV

Practice 4

Firmware computes `temp = mv / 10 - 500` for a 500 mV-offset, 10 mV/°C sensor. At 750 mV, what does it get, and what is wrong?

Practice 5

Which formula has units that work out to °C?

7.Apply it

Write a Python function for each of the three sensor types in this lesson (TMP36-style, MCP9700A-style, LM35-style) that converts millivolts to °C. Test each one with assert statements at two temperatures you work out by hand.

8.Reflect

Concepts: How sensors measure · Analog-to-digital conversion