Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.

Measure Temperature with Arduino and MCP9700

Created on: 26 January 2022

Measure temperature with Arduino and a MCP9700 temperature sensor. Use an Arduino Uno or MEGA 2560 as a thermometer to read air temperature. MCP9700 and MCP9700A are analog temperature sensors. When an MCP9700 or MCP9700A is connected to an Arduino analog input, a voltage level is read. After that, the voltage is converted to temperature in an Arduino sketch. Send the measured temperature to a computer via USB for display.

Part 10 of the Arduino Tutorial for Beginners

MCP9700 and MCP9700A Temperature Sensors

MCP9700 and MCP9700A temperature sensors are described as “Low-Power Linear Active Thermistor ICs”. Their output voltage is directly proportional to measured temperature. That is, as the temperature of the sensor increases, so does its output voltage.

Find more information on MCP9700 temperature sensors in the MCP9700 and MCP9700A datasheet from Microchip. Although the datasheet has a lot of technical information on the temperature sensor, it is used in a very basic manor in this tutorial. MCP9700 and MCP9700A ICs are available in three different packages or physical housings. Get the sensor in a TO-92-3 package, which is the same as a TO-92 3-pin plastic transistor package. Because this tutorial uses a breadboard, the TO-92-3 package is necessary. The other available packages are surface mount packages. As can be seen below, a MCP9700 in a TO-92-3 package has long leads to plug into a breadboard.

TO-92-3 Package of a MCP9700 and MCP9700A Temperature Sensor IC
TO-92-3 Package of a MCP9700 and MCP9700A Temperature Sensor IC

Difference Between a MCP9700 and MCP9700A Temperature Sensor

The difference between MCP9700 and MCP9700A temperature sensors is their accuracy. That is, a MCP9700 has an accuracy of ±4°C (degrees Celsius) between 0°C and +70°C. On the other hand, a MCP9700A has an accuracy of ±2°C between 0°C and +70°C.

How to Measure Temperature with Arduino

This section describes how to measure temperature with Arduino and a MCP9700 temperature sensor. Detailed instructions and circuits follow in the next section. After this is Arduino sketch code that gets and displays the temperature.

A MCP9700 sensor in a TO-92-3 package has three pins. As can be seen in the image below, these pins are VDD, VOUT and GND. Connect the MCP9700 or MCP9700A temperature sensor VOUT pin to an Arduino Uno or MEGA 2560 analog input. For example, connect it to A0. Connect VDD of the sensor to the Arduino 5V. Finally connect the sensor GND pin to Arduino GND.

MCP9700 and MCP9700A TO-92-3 Pinout
MCP9700 and MCP9700A TO-92-3 Pinout

Use a sketch to get the temperature from the MCP9700 or MCP9700A sensor. Firstly, read the analog voltage from the sensor. Secondly, use some mathematics and convert the analog value to temperature. Finally, display the temperature in the serial monitor window of the Arduino IDE. This is similar to the Read an Analog Input with Arduino part of this tutorial. To clarify, that part of the tutorial gets the analog value from an analog input pin. Afterwards, the value is displayed as a raw analog value or converted to a voltage. Basically the same is done for the temperature sensor, but the analog value is converted to temperature, rather than voltage.

MCP9700 / MCP9700A Arduino Temperature Measurement Circuit

The circuit diagram below shows a MCP9700 temperature sensor connected to an Arduino Uno A0 pin. Use the same circuit for an Arduino MEGA 2560. Capacitor C1 and C2 are optional. Basically they are to stabilize voltages by acting as decoupling and filtering capacitors.

MCP9700 Arduino Circuit Diagram to Measure Temperature with Arduino
MCP9700 Arduino Circuit Diagram to Measure Temperature with Arduino

The image below shows the above circuit built on a breadboard and connected to an Arduino Uno. As can be seen in the image, the optional capacitors are left off.

Arduino Uno MCP9700 Temperature Sensor Breadboard Circuit
Arduino Uno MCP9700 Temperature Sensor Breadboard Circuit

Below is the same circuit connected to an Arduino MEGA 2560. GND can be connected to one of the GND pins on the POWER connector where 5V is connected. GND is connected to a bottom right GND pin to make the circuit easier to read. That is because wires do not cross each other with this arrangement.

Arduino MEGA 2560 MCP9700 Temperature Sensor Breadboard Circuit
Arduino MEGA 2560 MCP9700 Temperature Sensor Breadboard Circuit

Build the MCP9700 Temperature Measurement Circuit

Build the circuit above for the Arduino Uno or Arduino MEGA 2560. Insert the optional capacitors into the circuit, if desired. Load a sketch to the target Arduino board in the next section.

Arduino MCP9700 Temperature Sensor Sketch Code

Firstly get the voltage from the MCP9700 temperature sensor VOUT pin by using an example sketch, as described next. Afterwards, load a sketch to the Arduino and display the MCP9700 temperature, as described in the second subsection below.

Display MCP9700 Voltage using Example Sketch Code

Read the voltage that the sensor is producing on the Arduino A0 pin using the ReadAnalogVoltage example sketch. Select File → Examples → 01.Basics → ReadAnalogVoltage from the top menu bar of the Arduino IDE. As a result, the ReadAnalogVoltage sketch opens in a new Arduino IDE window. Upload the sketch to the target Arduino board. Afterwards, open the Arduino IDE serial monitor window. The serial monitor window displays the voltage from the MCP9700 or MCP9700A temperature sensor.

Manually calculate the temperature from the displayed voltage. For example 0.73 displayed in the serial monitor window is 0.73 volts. Firstly subtract 0.5 volts from 0.73. The result is 0.23 volts. Secondly, divide the result by 0.01 volts. The result is 23. That is 23 degrees Celsius. Information on this calculation is from the MCP9700 datasheet. Do this calculation in the sketch code in the next section.

Touch the plastic case of the MCP9700 sensor. Warmth from fingers touching the case causes the temperature to increase. As a result, the voltage from the sensor increases.

Calculate MCP9700 Temperature with Arduino

Modify the ReadAnalogVoltage example sketch from the previous section. As a result, display temperature instead of voltage. Below is the ReadAnalogVoltage sketch code without the top comment block.

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

The above code converts the analog ADC sensor value to a voltage. Display temperature in degrees Celsius by applying the formula from the previous section. That is, subtract 0.5 from the calculated voltage. Afterwards, divide the result by 0.01. The following code listing shows the modified sketch.

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // Convert the voltage to temperature in degrees Celsius
  float temperature = voltage - 0.5;
  temperature = temperature / 0.01;
  // print out the value you read:
  Serial.println(temperature);
}

Add the code in bold above to the ReadAnalogVoltage example sketch. Afterwards, upload the modified sketch to the target Arduino board. As a result, the sketch displays temperature instead of voltage. Open the serial monitor window in the Arduino IDE. The serial monitor displays the temperature in degrees Celsius. Touch the plastic case of the MCP9700 and the temperature increases.

How to Display MCP9700 Temperature with Arduino

Modify the ReadAnalogVoltage sketch as follows to display temperature with Arduino and the MCP9700 sensor. Firstly create a new temperature floating point variable, and assign it the sensor voltage minus 0.5. Do this after the voltage calculation, as shown in the following line of code

float temperature = voltage - 0.5;

The above code does the first part of the temperature calculation. Secondly, divide the above result by 0.01 and assign it back to the temperature variable, as follows.

temperature = temperature / 0.01;

Finally display the temperature value in the serial monitor window with the following code.

Serial.println(temperature);

The above code sends the calculated temperature out of the Arduino USB port. Open the serial monitor window and the temperature continually updates in it.

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual