# How to measure temperature with Arduino and MCP9700

One of the sensors included in Arduino Starter Kit is a temperature sensor. It’s analog device which doesn’t need any additional elements to work (it’s exactly MCP9700-E/TO). So, briefly speaking, after connecting ground and power supply we are already able to measure temperature. On the sensor’s page in Nettigo shop there is PDF datasheet in Files bookmark. So, let’s begin from terminals:

Important – the terminals are showed from the bottom of the sensor. To the first we connect supply voltage (both 3.3V and 5V from Arduino would be OK), ground (GND) goes to the third and the second terminal should be connected to Arduino Analog0 pin. As we can read from the datasheet (first page), sensitivity equals 10 mV/ºC. On the second page we have Output Voltage, 500 mV for 0°C. That’s all we should know to write a simple program:

```float temp;

void setup() {
Serial.begin(57600);
};

void loop () {
temp = temp - 0.5;
temp = temp / 0.01;
Serial.println(temp);
delay(500);
};```

The program measures temperature and sends the reading to Serial, from which we can read it using serial port monitor in Arduino IDE. How the temperature is calculated? I divded the whole process in three steps to make it easier to understand:

1. `temp = analogRead(0)*5/1024.0;` Reading value from analog input and converting it to voltage. Maximal voltage Arduino is able to measure equals 5V and the A/D converter’s resolution is 10 bits, which means 1024 values, thus voltage value on Analog0 input is the value returned by analogRead multiplied by the voltage equaling one step of A/D converter. You have to remember, that dividing 5 by 1024 would be interpreted by compiler as integer operation and the result in this case will equal 0. Because of this in the code appears `1024.0` and thanks to this entry, the compiler will treat the dividing as floating point operation.
2. `temp = temp - 0.5;` Calibrating to 0°C – the difference between voltage read from the sensor and 500 mV is linearily dependent on temperature.
3. `temp = temp / 0.01;` This difference is divided by 10mV/step and now we have temperature

As you can see, the MCP9700 sensor is very simple to use and beginners-friendly. Now everyone can measure temperature with Arduino.

## Join the Conversation

1. JT says:

This really helped me out to get the MCP9700 going on my arduino. What do you do to get the readings into the realm of sanity? My readouts bounce around by 5 degrees constantly. I’m thinking I am going to read a bunch of samples to average them together or is there something else I can do to get a more stable readout? Have you gotten any better precision with a different voltage?

1. nettigo says:

I’m using MCP9700 mostly powered with 5V. I have no problems with readout stability, results are repeatable. I was using many (like 10) different sensors bought in few lots so I think it is quite good sample…

2. nettigo says:

Quick note – if You have troubles with readouts stability – verify how Arduino is powered. If via USB – then maybe computer does not give 5V. If You use external power – ground shield well – I had problems with ADC when no ground and external power was used.

At some point this post will be translated and published here, but for now Google Translate has to do work (or if You speak polish :) )

2. Hi there,

I have some problems with the accuracy of the temperature read-out.
If I compare the temperature with a thermo-coupler thermometer, I get large differences at higher temperatures.
At 25°C the values fit.
At 100°C, MCP9700 gives 84°C.
My setup is as follows;
MCP9700, thermo-coupler and heater (resistor) packet tightly together.
Anyone else trier to compare their values with an external thermometer?

1. nettigo says:

To be honest I have not tested MCP with temperatures over 40 degrees, so hard to say what can be wrong. I’ll try to setup some test env, using DS18B20 as reference, I’m curious what it will show…

3. I have the Lilypad USB board + the Lilypad Temp version (MCP9700). Very simple code as copied from you. The ref is 3.335, verified using DMM. As you can see, the output is jumping all over the place. I have a SHT11 and DS18B20 right next to it on another board and both are reporting 26 +/- 0.25 C apart from each other. The MCP9700 is jumping all over the place and plus not close. I’ve even used a Temporal Scanner to check the temp and all 3 are close, except the MCP9700. I tried a second one with the same results. Any ideas?

float temp;

void setup() {
Serial.begin(9600);
};

void loop () {
temp = temp – 0.5;
temp = temp / 0.01;
Serial.println(temp);
delay(1000);
};

21.65
18.72
17.42
18.72
17.42
20.02
18.72
18.72
21.65
21.65
17.42
23.28
21.65
18.72
17.42

4. Although variable “temp” is only used inside the “loop()” function, you still declared it as global. Why?

1. nettigo says:

@Xavier
There is no reason for that. Arduino sketches are so simple that in most cases it does not have meaning, and for beginners in programming (and this often case with Arduino users) it is easier not to introduce global/local variable concept.

5. I know this is a couple years late for Curtis, so I apologize.

I would try to introduced an averaging function for those temps and also place a compensation variable for the difference of the final temp by the amount it was off (error). Those two steps might have helped out. The average would introduce a delay, but wouldn’t be too bad.

I’m not sure about prices of these sensors, but I find I have to make changes like this quite often when I purchase cheaper components,,, which is all the time ;-)

6. Chris tix says:

My temperatures are very high. Im just at home. so it would be 20 celcius, something like that but the arduino says its like 40/41 celcius. If i hold the sensor with my fingers (It will probally raise for 1 celcius or something) but it raise like 10/15 celcius.

it is connected to 5 volt en to analog 0.

1. nettigo says:

@Chris tix
It looks like there is some error in formula. Are You sure You calculate it correctly? Take multimeter and measure output voltage, and do calculations by hand. It is 10 mV per degree and 0.5V for 0 Celsius. So if You have ~20 degrees it should be 0.5V + 20 *0.01V = 0.7V. If You keep sensor with fingers minute or longer, temperature should go to 30 degrees, so it should be 0.5 + 30*0.01 so ~0.8V. If these readings are something like that, sensor work OK.