TactivergeLite

Lite / Building Machines That Respond / 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 · Full interactive version

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.

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?

  1. 75 °C
  2. 25 °C
  3. 7.5 °C
Guess first, then reveal

Take away the 0 °C output: 750 − 500 = 250 mV. Each degree is 10 mV, so 250 ÷ 10 = **25 °C**. Forgetting the offset gives the tempting 75 °C.

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?

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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

Intuitive

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.

Technical

Datasheet transfer functions are often linear over a stated range with a stated error. Real sensors may need a two-point or multi-point calibration (next lesson) to remove part-to-part offset and slope differences.

Mathematical

y = c + m·x ⇒ x = (y − c)/m. This is the straight-line equation with intercept c and gradient m.

Applied

Pressure sensors in car tyres, light sensors in phones and current sensors in chargers all specify a linear transfer function like this.

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

°C

°C

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?

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.

Reflect

How would you notice, from the readings alone, that firmware was using the wrong offset?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Range, resolution, accuracy, precisionTwo-point calibration →