Home > electronics, projects > Serial Interface Between PIC and PC – PR4

Serial Interface Between PIC and PC – PR4

This one is really useful.

Most of the times you are developing a project, you will need some sort of monitoring or visual output to understand the status of your program. Sure some LEDs blinking are great but some times you need more. This is where the good old serial communication comes in. Through this communication you will be able to send information in the form of data or text, to and from your computer. You can use it as a debugging tool to monitor what is going on in your microcontroller during execution time. Or you can use this communication to send data to the PIC for processing and then receive the result back. I am sure you will appreciate the usefulness of this as soon as you implement it.

And all this happens by sending a sequence of zeros and ones…

What do you need

First of all you need your PIC of course. Now because I have my super convenient simple PIC prototyping platform 🙂 I was able to set up this project within a minute. There is a small problem though. A computer distinguishes 1 and 0 using different voltage levels than the microcontroller. For a computer, a valid serial signal lies between plus or minus 3 to 15V (read here) while for a microcontroller 0 is 0V and 1 is 5V. So you need some kind of converter between the two (so far I was using MAX232). Another thing to consider is how the two are connected together in hardware. I am sure you are familiar with the classic serial port but why not connect it with the very convinient USB port?

For some time now whenever you wanted to connect a microcontroller with a PC, the standard solution was the FTDI chip. You can find the chip EVERYWHERE, seriously! Unfortunately, if you budget is tight, FTDI is a bit expensive. You can get a breakout board off ebay for about €12. A much cheaper alternative that I am using in this project is CP2102. I got this about €4 including delivery off ebay and it seems to work just great!

CP2102 Connections

Also make sure you get a PC terminal software. I highly recommend Br@y Terminal.


So on the CP2102 breakout board you have 4 connections you can use which are

  • 5Volts
  • RX – Receive
  • TX – Transmit
  • GND

Connection between CP2102 and PIC

Connect the GND to the GND of the board. The TX of the PIC to the RX of the CP2102 and the RX of the PIC to the TX of the CP2102. Then connect the CP2102 on your PC. It will need to fetch some drivers from the internet and after that you should have a new COM port created. Open up you terminal and connect to that port and set the 115200 baud rate.

Our “Hello World” program

OK this is a pretty simple program to demonstrate the communication. Note that I am using the C18 Lite compiler by Microchip. First of all we are initializing the serial communication by defining some settings which you can look up in the PIC’s datasheet. Note that I am using SPBRG=21 value while the BRGH is HIGH. Since my board runs on 40MHz, this settings give me 115200 baud rate.

Also note that we defined a function putstr which sends a string through the serial character by character. Using this function we then output the “Hello World” string. Then we enter an infinite loop where the PIC checks whether we have a new data in the buffer using the DataRdyUSART function. So when new data is received it is echoed back to the PC. The delay between the transmissions is necessary to prevent overwritting the data in buffer before being sent. We could have used while(BusyUSART()) to do the same thing as in the putstr function.

As you can see it is a pretty simple program.

#pragma config OSC = HSPLL
#pragma config WDT = OFF
#pragma config PWRT = ON
#pragma config LVP = OFF
#pragma config PBADEN = OFF
void putstr(const rom char*);
void main(void)
    unsigned char received_char;
              USART_RX_INT_OFF &
              USART_ASYNCH_MODE &
              USART_EIGHT_BIT &
              USART_CONT_RX &
    putstr("Hello World!\r");
    putstr("from psychoul.com\r");
    while (1)
        if (DataRdyUSART())
            received_char = ReadUSART();
            putstr("You typed the character: ");
void putstr(const rom char *data)
    char c;
    while ((c = *data++))
    { // Transmit a byte
        while (BusyUSART());
        TXREG = c;

So program your PIC, make sure your connections are correct and you should be seeing a “Hello World” in your terminal as in the screen shot below. Press any button on the keyboard and it should be echoed back off to you.

Screenshot from my terminal

Now you are talking!

So now you can talk with your microcontroller through UART. You can use UART not only to talk to your computer but with other devices as well. For example, the XBee modules communicate with UART. So using the same things you learnt here, you can actually transmit data wirelessly without changing anything. I use this method to send data from my robot to MATLAB which runs on my PC to analyse the data and send directions.

Let me know how it goes.

Related Posts:

Incoming search terms:

  • cp2102 (76)
  • cp2102 pic (24)
Categories: electronics, projects Tags: , ,
  1. nasir
    March 3rd, 2012 at 14:55 | #1

    hi there >>>actually i have my matlab codes and i have also my PIC circuit then i need to interface them by using USB ….so what i need is how to send data from matlab to the USB ..Do i need to send them as characters or as voltage for example 5v then the USB can read that ..I’m really confused about that

  2. psychoul
    March 3rd, 2012 at 19:47 | #2

    Hey nasir. Connect the CP2102 on your PC and it will be assigned to a COM port. You can go to your computer device manager and find out which port it is connected to. That is for Windows. If you are using linux, look in the /dev/ directory. It should be something like /dev/ttyUSB0

    Now in MATLAB you can connect to a COM port using the serial command. Search it on the internet. Also set the baudrate and you are ready to go. Send you data using the PIC USART library (or directly write to the TX register) and the data will end up in the serial buffer within MATLAB. From there you can use fread command to read the data from the buffer.

    If you need further help write below.

  1. No trackbacks yet.