Thursday, November 19, 2009

MCP9700A Temperature Sensor Arduino Library

I've done a little polishing on my code for using the Microchip MCP9700A Temperature Sensor and converted it to a library for the Arduino. I'm releasing it under the GPL in case it might be of use to anyone else. You can download it here:


This is my first attempt at an Arduino library. Comments welcome.


Tim, N9PUZ


John said...

The only thing I'd suggest is an optional input that defines the aref voltage. My quick scan of that part's data sheet shows it's a 0-1.9v signal (might have not studied it enough) and you can more than double the precision of the measurement by using a more appropriate aref than 5.0 volts.

Otherwise, thanks for sharing. I'm doing the same thing, but, I hadn't got to the point of turning it into a library. Now I guess I don't need to...

Tim, N9PUZ said...

Thanks for your thoughts John.

In my initial application knowing the temperature to the nearest degree was fine so I didn't worry about Vref. In fact my application casts the result to an 'int' and gets rid of the fractional part entirely. I did set Vref to default so I knew what it would be.

Of course the tinkerer in me would like the added resolution as you suggest. What I'm wrestling with is what to do in the library if the application uses other ADC inputs for things other than temperature. I'd appreciate any ideas you might have for that problem.


Anonymous said...

This looks useful for me, I'm about to start building a pump controller for a solar hot water panel using an arduino. The link doesn't appear to be working though...


Tim, N9PUZ said...

Tom --

Sorry about the broken link. There were some site changes a while back and I did not get that link fixed. It should be working now.


Anonymous said...

I made a change to mcp9700a.cpp that I think helps get a much more stable reading. When I first tried the example program, the temp was jumping all over the place.

float tmpRaw;

for (int i = 0; i < 20; i++) // read the value on analog input span times then divide by the number of span for average.
{tmpRaw = tmpRaw+analogRead(_pin);}

tmpResult = tmpRaw/20 * VOLTS_PER_BIT;

Roaring Monkey said...

Hi, this is really weird, but after i downloaded your library, i didn't put in in the 'libraries' folder so when i tried to compile the example code that came with your library, i got and error message saying, "mcp9700a.h did not exist"

so i copied your library folder and put it in the 'libraries' folder. after this is done, the problem comes--when i try to compile now,
The following error info is returned:

Binary sketch size: 1112 bytes (of a 14336 byte maximum)

avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

I've tried for the whole day to remedy it but to no avail. Anyone here have any idea how to fix this??

i tried reinstalling the driver and compiler, didn't work either(i'm not sure i did it the right way though).


Tim, N9PUZ said...

When the example program included with the library zip file is compiled it produces a size of 3962 bytes of 30,720 maximum. This is with Arduino development environment V 0017 configured for a Duemilanove board with bootloader.

You seem to have changed something as your code size is way too small. If you are not starting out with my example program I suggest you revert to that before you forge ahead on your own.

I don't understand why you are getting avrdude responses. The Arduino environment contains its own programming tool.

Tell me exactly what development environment you are using and I may be able to help but this library has been used by a lot of people and assumes some knowledge of Arduino development experience.

EA2DDC said...

Thanks for the code.

The sensor works before find your code, but this is more elegant and useful.

Thanks too for kb5uej. I aplied the mod and results seems more credible....

PatitoPower said...

hi, could you upload the .zip file again.

thanks a lot!


Tim, N9PUZ said...

Should be fixed again. Sorry for the problems.