Low cost temperature data logger using PIC and Processing
|
This project describes an easy and inexpensive way of adding a digital thermometer and data logging feature to a PC. It involves a PIC microcontroller that gets the surrounding temperature information from the Microchip MCP9701 sensor, and sends it to a PC through an USB-UART interface. The USB port of the PC is also used to power the device. The open-source Processing programming platform is used to develop a PC application that displays the temperature in a graphics window on the computer screen. The PC application also records the temperature samples plus date and time stamps on an ASCII file.
Theory of operation
This project is based on Microchip’s PIC12F1822 microcontroller from the enhanced mid-range PIC family. It has got 8-pins in total and the power supply voltage range of 1.8V to 5.5V. The microcontroller has four 10-bit ADC channels and one Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) module for serial communication. The temperature sensor used here is MCP9701A, which is a Low-Power Linear Active Thermistor IC from Microchip Technology. The range of temperature measurement is from -40°C to +125°C. The output voltage of the sensor is directly proportional to the measured temperature and is calibrated to a slope of 19.53mV/°C. It has a DC offset of 400mV, which corresponds to 0°C. The offset allows reading negative temperatures without the need for a negative supply. The output of the sensor is fed to one of the ADC channels of the PIC12F1822 microcontroller for A/D conversion. The internal fixed voltage reference (FVR) module is configured to generate a stable 2.048 V reference voltage for A/D conversion. The use of FVR module ensures the accuracy of the A/D conversion even when the supply voltage is not stable. The PIC12F1822 microcontroller then serially transmits the 10-bit ADC output to a PC.
Modern PCs are no more equipped with serial ports and therefore this project requires a USB-UART adapter that enables very easy connection of the PIC12F1822 to the PC via the USB port. You can get them really cheap on ebay. I bought one for $3.39 (see the picture below) from here: http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item=370532286388
It can be directly interfaced to the TTL input and output of EUSART module of PIC12F1822. This module also provides +5 V, +3.3 V, and ground terminals. The power supply for the microcontroller circuit is derived from the same +5 V and ground pins.
On PC’s side, the open source programming language Processing is used to receive the ADC output and convert it into the actual temperature. The temperature is displayed on a graphics window on the computer screen in numeric format as well as with a wall tube thermometer looking image where the level of alcohol rises with increasing temperature. A clickable Start/Stop button also appears on the window to enable or disable the data logging.
Circuit diagram
The circuit diagram of this project is pretty simple. The microcontroller reads the temperature sensor’s output through RA2/AN2 pin and convert it to a 10-bit digital number. The Tx (RA0) and Rx (RA1) port of the EUSART module are connected to the corresponding pins of the USB-UART module. The microcontroller runs at 4.0 MHz using an internal clock source. Although I have disabled the MCLR function here, you can use it for an external reset if you want.
I soldered the above circuit (except the USB-UART adapter) on a general purpose prototyping board with a 6-pin female header connector so that it could be easily plugged into the male header pins of the USB-UART adapter (shown below).
Software
The firmware for PIC12F1822 is developed in C and compiled with mikroElektronika’s mikroC Pro for PIC compiler. The compiler does provide an ADC library but that uses the external supply voltage as a reference for A/D conversion. In order to configure the FVR module to generate a fixed 2.048V for A/D conversion, you have to write your own code for ADC operation. The ADC sample is taken every 2 sec and is sent to the PC through USB-UART module as two bytes. The complete source code is provided below with comments. It can be compiled with the demo version of mikroC Pro for PIC compiler. Make sure that you select the internal clock source at 4.0 MHz from Project->Edit window.
/* Project: PC thermometer Description: Sends ADC samples to a PC through UART port MCU: PIC12F1822 running at 4.0 MHz internal clock Written by: Rajendra Bhatt Date: Oct 10, 2011 */ unsigned int adc_value; unsigned short MS_Byte, LS_Byte; char error; int i; void main() { ANSELA = 0b00000100; // RA2 analog input TRISA = 0b00100110; // RA1, RA2, RA5 inputs PORTA = 0; OSCCON = 0b01101000; UART1_Init(9600); Delay_ms(100); // Configure FVR to 2.048 V for ADC FVRCON = 0b11000010 ; // Configure ADCON1 ADCON1.ADPREF0 = 1; // Vref+ is 2.048 V ADCON1.ADPREF1 = 1; ADCON1.ADCS0 = 0; // Use conversion clock, Fosc/2 ADCON1.ADCS1 = 0; // Fosc = 500 KHz ADCON1.ADCS2 = 0; ADCON1.ADFM = 1; // result is right Justified // Configure ADCON0 for channel AN2 ADCON0.CHS0 = 0; ADCON0.CHS1 = 1; ADCON0.CHS2 = 0; ADCON0.CHS3 = 0; ADCON0.CHS4 = 0; ADCON0.ADON = 1; // enable A/D converter do { ADCON0.F1 = 1; // start conversion, GO/DONE = 1 while (ADCON0.F1); // wait for conversion MS_Byte = ADRESH; LS_Byte = ADRESL; UART1_Write(MS_Byte); Delay_ms(50); UART1_Write(LS_Byte); // Line Feed delay_ms(2000); } while(1); } |
Download the complete mikroC code
As I mentioned earlier, the PC application is developed using the Processing programming language. Processing is an open-source software development environment designed for simplifying the process of creating digital images, animations and interactive graphical applications. It is free to download and operates on Mac, Windows, and Linux platforms. I have written a simple application here that receives the 10-bit ADC sample from the serial port, converts it to temperature, and display it on the computer screen. The Processing serial library allows for easily reading and writing data to and from the serial ports. Read more about the Processing serial library HERE.
You should import the Processing Serial library first before accessing the serial port. This can be done by,
import processing.serial.*;
Next, you can open a serial port as
PIC_Board = new Serial(this, "COM6", 9600);
In my case, the USB-UART module appear as COM6, you should find the right COM number to make it work for you, which you can find out from the Device Manager tool in Windows.
Once the two bytes of ADC data are received serially, the actual temperature is retrieved by applying the sensor specific conversion factors. For MCP9701A, the conversion equation would be,
temp = MS_Byte*256 + LS_Byte; tempC = (2*temp - 400)/19.5; // Factor 2 corresponds to VREF = 2.048 V tempF = ((tempC*9)/5) + 32;
A clickable Start/Stop button is also provided on the display window. The Processing Mouse functions are used to detect a mouse press over the button. When the Start is pressed, data logging begins and the label on the button turns into ‘Stop’. If Stop is pressed, the data logging is paused. The temperature samples are recorded along with the date and time stamp (from PC) into an ASCII file. Every time the Start is pressed, the program creates a new ASCII log file. The name of the file contains the current system date and time so that there won’t be any overwriting of files. However, the data are temporarily stored into the PC’s RAM and are transferred to the ASCII file on the hard drive only after pressing the Stop button.
The Processing source code and exported applications can be downloaded from the following link.
Download the Processing source code
Future enhancements
There is a lot of room for improvements in this project. The sampling interval of the data logger is currently hard coded into the firmware of the PIC12F1822 microcontroller. However, both the firmware and the PC application can be modified to make the sampling time user-configurable from the application window. Similarly, a plotting program can also be added to the Processing application to display the temperature profile from the logged files.
Update
Conversion formula for MCP9701A
Resolution of A/D conversion (Vref = 2.048V) = 2.048 V/1024 = 2 mV/count
=> Equivalent voltage for 10-bit ADC output (Count) = 2*Count (mV)
=> Temperature (°C) = (Voltage – Sensor Offset)/Sensor conversion coefficient,
where Sensor Offset = 400 mV and conversion coefficient = 19.5 mV/°C from datasheet.
|
Hello Sir, do you made any pid temperature controller
The program is not working .
I can,t get the thermometers.,only the start ,stop button
My version of this project: https://www.youtube.com/watch?v=-JlZPPUafio
Has anyone tried to get this to work on a Raspberry Pi?
The PIC12F1822 has a built in temperature sensor. Why not use it to make the project even lower cost? See Chapter 15 of the data sheet.
What does it means ? : // Fosc = 500 KHz
thank you Raj!
marC:)
Dear Sir,
This is very usefull circuit. Please help me for
a) Since unavailiblity of PIC12F1822 can i use PIC12F683.(or any other please suggest)
b) Since unavailiblity of MCP9701A can i use DS18B20.(or any other please suggest)
C) Above changes will require changes in code? please guide since i am new in MCU programming.
I have PIC KIT2.
Thanks & Regards,
Nikhil
Hi
The gui I have downloaded but it doesn’t work. Can you tell how to use that please?
You may have to modify the Processing code if your USB-UART module appears different then COM6 port. If so, then download Processing software and edit the .pde file in the directory for the serial port. It’s easy once you download Processing and open the .pde file inside the folder.
Parece bueno . Vamos a poner manos a la obra. Terminado les cuento
Saludos
Pingback: Temperature Logger For Your PC | HACKOLOG - Amazing Hacks and Mods
Now it works properly, who knows where he was the first problem.
Hello.
*Then start the software PICkit2 Programmer 2.61, click on Programmer -> Manual Select Device.
After clik on Device Family -> Midrange -> 1.8V Min
PS: sorry the translation is not accurate from Italian to English by Google translator.
@sstip
To program the 12F1822 with PICkit2 Clone (in my case), must be upgraded first the file .dat (Device File 1.62.14) and from http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en023805 unzip it into “C: \ Program Files \ Microchip \ PICkit 2 v2 \”
Then boot up 2.61 PICkit2 Programmer Manual and click Select Device -> Device Family -> Midrange -> 1.8V Min
PS: is not automatically recognized by PICkit2 Clone, but you have to manually select it when it should be programmed, but it works well.
Hello. 🙂
Hello, I’m trying this product, but once assembled everything on the breadboard and then start the program (TempDataLogger.exe), appears only the Start/Stop log.
After clicking the Stop log file is created. Txt and only within this string: MM DD YYYY HH MM SS C F
How can I do?
Hello and thank you.
Hi,
I have an issue with programming the PIC. I compile the code you gave with mikroC PRO, but then I try using the mikroProg Suite to program the PIC and the PIC12F1822 is not listed anywhere!? How did you program it?
Thanks!
@sstip,
I used PICKit2 to program PIC12F1822.
Unfortunately, this device is not supported by this programmer.
Hi RB
Nice project.Could you please let me know the part numbers of the above circuit and it would also be helpful if you could give the overall project cost.
Regards
Ajay
Hello R-B,
thank you for your answer.
I read the output voltage on the datasheet,
http://ww1.microchip.com/downloads/en/DeviceDoc/21942e.pdf
(page 9).
and i’haven’t connect the output MCP9701A to the PIC.
I conect the output directly to my voltmeter.
What do you think about?
So it goes up to 3V and that true. I assumed it was your typo then as you mentioned 3 mV. If you are measuring 5.0 V then may be the device is bad. Make sure that you are using the right pins for Vcc and Gnd.
Hello everybody!!!!
One thing that i don’t understand.
According to the datasheet,for MCP9701A the output voltage is between 200mV and 3mV.
i use the EASYPIC6 from Mirkoe.The power of this device is connected to the input power of the MCP9701A (5V).And when i mesure the output voltage,there is 4,7V and i don’t know why??
Someone can help me??
Thank you.
@thehist,
Describe your problem in more detail. Where did you read that the output voltage of MCP9701A is between 200mV and 3mV? Did you connect the output of MCp9701A to a PIC input pin while measuring the voltage? If so, make sure that you have defined the pin as input and is pulled down.
where can I get the protoboard with the 6 pin header attached?
@Singh,
You can solder it by yourself.
Pingback: Electronics-Lab.com Blog » Blog Archive » Low cost temperature data logger using PIC and Processing
Hi,
Nice project, i fancy building a few to stop people complaining about how hot/cold it is !
Do you have parts numbers for the PIC, socket, pin header etc, from Farnell or similar ? It always takes me ages to find the right bits.
Pingback: Low cost temperature data logger using PIC and Processing | Look4tech.com
Pingback: Throw together a temperature logger in minutes | CisforComputers
Pingback: Throw together a temperature logger in minutes | The Depot of Talk
Pingback: Throw together a temperature logger in minutes | ro-Stire
Pingback: Throw together a temperature logger in minutes | You've been blogged!
Pingback: Throw together a temperature logger in minutes - Hack a Day
Good day,
Thanks for th conversion formula for the MCP9701A. Could you possible elaborate on how you obntained this calculation?
I have been working with this sensor for a while and although I have a partially working formula it’s performance is not adaquate.
I would also need the formula to work on an 8 BIT PIC and write to an LCD dsiplay and then later transmit over EasyBee modules.
Any assistance would be greatly appreciated.
Kind regards
Michael
Hi Michael,
I have added the detail of the conversion equation at the end of the article.