Turn the analog temperature sensor into a thermometer

The KY-013 module only gives you a voltage. In this lesson you turn that voltage into a temperature step by step: from the reading, to the resistance of the thermistor, to degrees Celsius. You also learn how to check and...
  • Level:Intermediate
  • Duration: 45 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Turn the analog temperature sensor into a thermometer – EEMENTOR STEM™ lesson

The analog temperature sensor in your Sensor Kit (the KY-013) doesn't know anything about degrees. Inside it is a thermistor: a resistor whose resistance changes with temperature. The UNO can only measure a voltage, so getting from that voltage to "21.4 °C" takes three steps, and that's exactly what you'll program in this lesson. On the way you'll meet the voltage divider again, a real formula engineers use for thermistors, and a simple way to check that your thermometer tells the truth.

What you'll learn

  • What an NTC thermistor is, and how its resistance changes with temperature
  • How the module turns a resistance into a voltage with a voltage divider
  • How to work back from the UNO's reading to the thermistor's resistance
  • How the B-parameter equation turns resistance into degrees
  • How to check and calibrate a home-made thermometer

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • Analog temperature sensor module KY-013 (45-in-1 Sensor Kit)
  • Breadboard and jumper wires (UNO R3 Starter Kit)
  • The free Arduino IDE

Wiring

The module has three pins: S (signal), the middle pin (power) and − (GND). The middle pin is power on every version of this module. Follow the labels printed on your board.

Breadboard wiring diagram: KY-013 analog temperature sensor with S to UNO A0, the middle pin to 5V and minus to GND.
Breadboard view. Check each connection against the wiring table below.
From To
KY-013 S UNO A0
KY-013 middle pin (+) UNO 5V
KY-013 − UNO GND
FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The UNO and the parts for this lesson laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. The UNO and this lesson’s parts are already placed. Wire them using the wiring table above. Pins with no wire show red.
  2. Missing a part? Add it from the parts list on the left.
  3. Press Run, then try the inputs: click, hold or slide them under Selected.
  4. Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

The sketch

Copy the sketch into the Arduino IDE, choose Arduino Uno as the board and the right port, and click Upload. Then open the Serial Monitor at 9600 baud.

// Turn the KY-013 analog temperature sensor into a thermometer
// Step 1: read the voltage. Step 2: work out the thermistor's resistance.
// Step 3: turn the resistance into degrees with the B-parameter equation.
const int SENSOR_PIN = A0;
const float R_FIXED = 10000.0;   // the 10 kΩ resistor on the module
const float R_25 = 10000.0;      // the thermistor's resistance at 25 °C
const float BETA = 3950.0;       // the thermistor's B value
const bool NTC_ON_TOP = false;   // true if the temperature goes DOWN when you warm the sensor
const float OFFSET = 0.0;        // calibration: add or take away a few tenths of a degree

void setup() {
  Serial.begin(9600);
  Serial.println("KY-013 thermometer");
}

void loop() {
  int raw = analogRead(SENSOR_PIN);                  // Step 1: 0 to 1023
  if (raw <= 0 || raw >= 1023) {
    Serial.println("Reading at the end of the range: check the wiring.");
    delay(1000);
    return;
  }

  float rNtc;                                        // Step 2: the resistance
  if (NTC_ON_TOP) {
    rNtc = R_FIXED * (1023.0 / raw - 1.0);
  } else {
    rNtc = R_FIXED * raw / (1023.0 - raw);
  }

  float kelvin = 1.0 / (1.0 / 298.15 + log(rNtc / R_25) / BETA);   // Step 3
  float celsius = kelvin - 273.15 + OFFSET;

  Serial.print("reading ");
  Serial.print(raw);
  Serial.print("   resistance ");
  Serial.print(rNtc, 0);
  Serial.print(" ohm   temperature ");
  Serial.print(celsius, 1);
  Serial.println(" C");
  delay(1000);
}

Try it

  1. Upload the sketch and open the Serial Monitor. Every second you get a reading, a resistance and a temperature. At room temperature the resistance is close to 10,000 ohms, and the temperature is around 20–25 °C.
  2. Hold the thermistor (the small black bead) between your fingers. The resistance falls and the temperature climbs towards 30 °C. Let go, and it slowly drops back.
  3. Check the direction: if the temperature goes down when you warm the sensor, your module is wired the other way round inside. Change NTC_ON_TOP to true and upload again.
  4. Calibrate it: put the module next to a thermometer you trust and wait five minutes. If yours reads 0.8 °C too high, set OFFSET to -0.8.
  5. Open Tools → Serial Plotter and breathe on the sensor to watch the temperature curve.

What you should see

Serial Monitor at 9600 baud: readings around 520 with about 10.3 kilohms and 24.3 degrees at room temperature, then falling readings and rising temperatures up to 29.1 degrees while the sensor is held.
Hold the sensor and watch the temperature climb.

How it works

How it works: the 10 kilohm resistor and NTC thermistor as a voltage divider, the NTC resistance curve falling from 26 kilohms at 5 degrees to 3.6 kilohms at 50 degrees, and the three steps from reading 456 to 8.04 kilohms to 30.0 degrees.
From a voltage to degrees in three steps.

The thermistor on the KY-013 is an NTC type: Negative Temperature Coefficient. That means its resistance goes down as it gets warmer. It is about 10 kΩ at 25 °C, about 26 kΩ at 5 °C, and only about 3.6 kΩ at 50 °C. The change isn't a straight line, which is why we need a formula rather than a simple multiplication.

Step 1, the voltage: the module puts the thermistor and a fixed 10 kΩ resistor in a row between 5V and GND, a voltage divider. The S pin sits in the middle, so its voltage depends on how the two resistances compare. analogRead() turns 0–5 V into a number from 0 to 1023.

Step 2, the resistance: if the thermistor is the bottom half of the divider, the reading is 1023 × R_ntc ÷ (10 kΩ + R_ntc). Turned round, that gives R_ntc = 10 kΩ × reading ÷ (1023 − reading), the line in the sketch. Some modules have the thermistor on top instead, and then the other formula is the right one; NTC_ON_TOP picks between them.

Step 3, the temperature: the B-parameter equation, a simple form of the Steinhart–Hart equation, links resistance and temperature: 1/T = 1/298.15 + ln(R ÷ 10 kΩ) ÷ B. Temperatures in it are in kelvin (298.15 K is 25 °C), and B, here 3950, describes how strongly this thermistor reacts. Take away 273.15 and you have degrees Celsius. The parts on the module aren't perfect, so a reading a little off is normal; that's what OFFSET is for.

Try this next

  • Show the temperature on the 1602 LCD from the LCD lesson.
  • Record the highest and lowest temperature since the UNO started, and print them every minute without blocking the loop, using what you learned in Do two things at once with millis().
  • Compare it with the DS18B20 digital thermometer: put both side by side and see which reacts faster.
  • Average ten readings before you convert them, to make the last digit steadier.

Troubleshooting

  • "Reading at the end of the range": the reading is 0 or 1023, so the divider isn't working. Check that the middle pin goes to 5V, − to GND and S to A0. If S and − are swapped, you'll also see this message: swap them back (never move the middle pin).
  • The temperature goes down when you warm the sensor: change NTC_ON_TOP to true.
  • The last digit jumps around: that's normal. Near room temperature each step of analogRead() is less than a tenth of a degree, and electrical noise moves it by a step or two. Average several readings.
  • It reads a degree or two high near the UNO: the board warms up a little while it works. Use longer wires to keep the sensor away from it.