PIC: PC Interfaced Digital Thermometer using PIC Microcontroller

Post image of PIC: PC Interfaced Digital Thermometer using PIC Microcontroller
Filed in Featured , PIC , Projects 28 comments


In our previous posts we learned how to ;

Today in this post we learn the following new stuff while developing the project mentioned in title;

  • Reading of analog values using built-in ADC in PIC
  • Sending data over RS232 Serial Communication using built-in USART in PIC


Our objective in this post to create such a system which measures ambient temperature and sends it to PC using Serial Port. To fulfill this we need the following major components ;

  • PIC16F876 – PIC Microcontroller – Main Microcontroller
  • LM35 – Precision Centigrade Temperature Sensor – For measuring temperature
  • MAX232Level Conversion RS232 – TTL – For interfacing MCU’s USART with PC’s Serial Port

Analog Digital Conversion in PIC Microcontrollers

Generally PIC Microcontrollers offer 10 Bit of Analog to Digital Conversion. This means that when they measure an incoming voltage, they compare that voltage to a reference voltage, and give you the comparison represented as a 10-bit number (0-1023).

You can wire as many analog inputs to the PIC as there are analog ports to accept them. However, you will notice some strange effects when you do so. The ADC circuits will affect each others’ readings, because all the circuits are pulling off the same +5V source. One thing you can do to minimize this is by using decoupling capacitors by each analog input, to smooth out dips and surges in the current caused by the other ADC’s. Typically 0.1µF to 1µF will do the job. Place the capacitors between power and ground, as physically close to the ADC input as you can get.

It also helps if you increase the sampling time and the delay between conversion and reading commands. Too much delay and you sacrifice interactivity; too little delay and you get random numbers. Start with the numbers in the sample code above, and vary them until you get readings you’re happy with.

Finally, it helps to sample at a lower resolution. Sampling at 8 bits instead of 10 will improve the speed and stability of a multiple ADC program.

Since the ADC registers provide you with 10 bit number converted from the analog input, you can use the following formula to have input voltage;

LM35 – Precision Centigrade Temperature Sensor

LM35 is an integrated circuit sensor that can be used to measure temperature with an electrical output proportional to the temperature in cenitgrade. The main reason behind using LM35 is that it doesn’t need amplification like thermocouples. The conversion ratio of LM35 is 10mV/’C . For e.g at temperature of 35 degrees LM35 will output 350 mV.

MAX232 – RS232 Transceiver

MAX232 is used for level translation between TTL and RS232. It is very common and integral component while communication between Microcontrollers and PC using RS232. The USART of PIC works on0-5V levels while RS232 works over -15 to +15 level. The MAX232 acts as a bridge between these two standards.


By combining the above three major components along with the power of CCS C Compiler we achieve our objectives.


Following is the schematic of whole project;


The output of LM35 is attached to PIN A0 which is a analog input.
Pin C6 & C7 are dedicated for USART as Tx and RX respectively. MAX232 sits between RS232 and Controller to translate their voltages. A DB9 connector is used to connect PC with Controller.


Following are the steps from the software we will design for the project;

  1. Define ADC Resolution.
  2. Configure USART with RS232 settings.
  3. Initialize ADC.
  4. Reading ADC Value.
  5. Conversion into Centigrade.
  6. Send temperature reading to PC over USART.

Steps 4 to 6 will be performed under an infinite loop. But a delay will be added explained later.

1.Define ADC Resolution


The above statement is used to define the resolution of ADC in bits. The maximum resolution for PIC16F876 is 10-Bit. But you can define it to any number less than the maximum resolution.

2. Initialize and configure USART with RS232 settings.

The following line is used to initialize and configure USART to start communication.

#use rs232 (baud=57600,rcv=PIN_C7, xmit=PIN_C6)

This line is used just after the oscillator clock declaration.Usually you have to give it Baudrate and the pins used for communication. If you want to use the builtin hardware USART then you use the above declared pins. But if you like more than one serial communication ports, you can declare communication on any pins. But note that using pins other than C6 and C7 will involve software serial data handling. And it can compromise speed as compared to hardware USART.

3.Initialize and Configure ADC

setup_adc_ports( ALL_ANALOG );
set_adc_channel( 0 );

These three lines setup the ADC and initializes it. setup_adc_ports is used to define which pins are to be used as analog inputs. Most suitable is ALL_ANALOG as it makes all AN pins as analog.

The next statement defines the clock source for ADC. As PIC also has external clock feature for ADC conversion. The best is internal clock.

To select the pin from which to read the analog voltages we use set_adc_channel . In our case LM35 is connected to Pin AN0, so we select Channel 0.

4.Reading ADC Value

value = read_adc();

The function read_adc() is used to read the value from ADC Register. It contains the digital equivalent value of analog input.

For e.g. if we input 2.5 volts and the resolution defined for ADC is 10-Bit, we will get a value of 512 from read_adc().

5. Conversion into Centigrade

As we know that we get a digital value equivalent to in out analog voltage. So first we convert it into voltage value;


Please note that by default the reference voltage for ADC in PIC is 5V.

For e.g if we get a value of 512 from ADC of 10-Bit resolution, the formula will have following values ;


We have got the voltage value which is coming from ADC, now we convert it into Temperature with help of formula provided by LM35;


If voltage of 0.375 are read from ADC, the above formula will return Temperature of 37.5°C.

Implementing both the  equations in C ;

volt = (value / 1023) * 5;
temp = volt * 100;

Read and conversion of voltages into digital and then temperature is done. In next we will send the data to PC using very simple command.

6.Send temperature reading to PC over USART.

The very reason of popularity of CCS C Compiler is its ease of use and powerful commands. Specially dealing with serial communication is very easy in this compiler. To send data to PC all you need to do is to use the following one line function;

printf(“Temperature: %.1f\n\r”,temp);

If you have used TurboC  you maybe very well familiarized with printf. This function is the same olf function but the only difference is that when used in TurboC it displays on Monitor and when used in PIC it sends data over to serial port.

The end result of this function at serial port would be;

Temperature: 37.5

The Complete Code

Combining all the parts together we have the following code;

#include <16F876.h>
#use delay(clock=20000000)
#use rs232 (baud=57600,rcv=PIN_C7, xmit=PIN_C6)

float value;
float temp;
float volt;

void main()

//Initialize and Configure ADC

setup_adc_ports( ALL_ANALOG );
set_adc_channel( 0 );


//1 Sec Delay

//Read ADC Value
value = read_adc();

//Convert Value into Volts
volt = (value / 1023) * 5;

//Convert Volts into Temperature
temp = volt * 100;

//Send data to PC
printf(“Temperature: %.1f\n\r”,temp);



You can use Hyper Terminal which comes with Windows XP. Or you can use many other serial port monitors easily available for download.

Following is the screenshot of Hyper Terminal being sent the temperature data;


In our next post we will learn how to make temperature readings more accurate by using filters and different techniques. Also we will learn how to recieve the serial data in Visual Basic 6 for further programming and GUI.

Happy InterfacING =)

Posted by hamzaazeem   @   21 October 2009 28 comments
Tags : , , , , , , ,

Share This Post

RSS Digg Twitter StumbleUpon Delicious Technorati


  • Krishan

    Very useful details.

  • Shb

    One more thing...
    i'm a bit confused between Vs+,Vs-,Vcc and gnd..
    i mean power is connected to Vs+...why is that...shouldn't that be grounded like Vs-..
    Plus In the above circuit IC's (Vcc and gnd) r not shown connected..so that is an understood thing right....so whats with Vs+ and Vs-

  • creativeelectron

    MAX232 enhances voltage by charging and connecting capacitors in series.All is done internally by its logic controller and voltage comparator. There polarity for series connection determines the polarity of output voltage that is TX.

  • Shb

    does that mean that Vs+ is to be connected to Vcc ??? and Vs- is to be connected to the common gnd ??? or are these two to be connected to a separate connector for communication purpose????

  • creativeelectron

    no it doesn't mean that. it is just meant to be connected with capacitors.

blog comments powered by Disqus
Previous Post
Next Post