Skip to main content

How to Send and Receive Data From UART in dspic30f and dspic33f

Follow along with this tutorial to learn how to establish communication between your micro-controller and PC.

Follow along with this tutorial to learn how to establish communication between your micro-controller and PC.

UART is the most commonly used communication protocol in microcontrollers due to its simplicity, easy implementation, and minumum hardware requirements.

This tutorial and example code implementation will walk you through the process of achieving successful UART communication between a dspic micro-controller and PC.

PL2302 USB to ttl converter module.

PL2302 USB to ttl converter module.

Things Needed for UART Communication

The first step should be to gather all the required things necessary for UART communication. The list below summarizes these hardware and software requirements:

1. UART Communication Software for PC

  • Option 1: I would prefer the Arduino IDE for this purpose. The Arduino IDE has a serial monitor which can be used to monitor sent data from the micro-controller. It also has an excellent graphical monitor to plot the received data. The same IDE can be used to send serial data also.
  • Option 2: The PicKit™ tool also has a serial monitor to send and receive data but it does not have a serial plotter. It also does not supports high baud rates up to 115000 which the Arduino IDE can support.

2. USB to Serial Converter/Transceiver

  • Option 1: The PL2302 USB to TTL converter can be very conveniently used as the transceiver between the USB serial port of the PC and the uart TX/RX pins of the micro-controller. It is very cheap and readily available on the internet and local electronic stores (at least in my area).
  • Option 2: The programming tool of the Pic micro-controllers, the PicKit™ also has an inbuilt UART transceiver. Advantage of using this is that you will not need to purchase any extra hardware as PicKit will do the job. (Assuming that you already have it since you are programming the Pics.) Downside is that you cannot put the PicKit inside your finished projects as you might need it for later use. In this case the USB to ttl converter is a better option.
  • Option 3: A UART to RS232 conversion circuit might be fabricated with the help of MAX232 ICs and RS232 cable. ICs are usually cheap but it will cost you more time to fabricate the circuit. This used to be the legacy solution before the advent of usb to ttl converter modules.

3. dsPic Micro-Controller and Its Programmer

A pic device and its programmer to load the code.

4. A Few Jumper Wires

To make the required connections between the TX/RX pins of the micro-controller and the USB to TTL converter

5. USB Extension Cable (Optional)

In case you need to wire the usb to ttl converter far from your PC when you are finally assembling your project.

After going through the list of things required you can proceed with the steps 1 through 4 below.

1. Install the Required Software and Drivers

Install the serial monitor of your choice on your PC; as I stated earlier, I prefer the Arduino IDE.

If Using USB to TTL Converter:

You will need to download its driver and install them before you can make the module to work. The drivers are available on the Prolific Systems website and elsewhere on the internet. Make sure the drivers you download are compatible with Windows 7, otherwise it will not work. Search for a separate tutorial for driver installation if you are having problems.

After installing the driver you must test the TTL converter to make sure it is working properly. It is recommended that you do not proceed ahead before testing the converter.

To test the converter:

  1. First install the required driver and reboot the system.
  2. Plug in the converter module and the PC should notice a new device connection.
  3. Short the TX and RX pins on the TTl converter with the help of a jumper.
  4. Open Arduino IDE and select the correct serial port to which the converter is connected.
  5. Send any data over the serial monitor.
  6. If you receive the same sent data back in the serial monitor then the module is working correctly.

If Using PicKit:

Just dwonload the Pickit program and connect the PicKit device to your PC.

Short the TX and RX pins with a jumper or wire to test the module.

Short the TX and RX pins with a jumper or wire to test the module.

2. Wire the Connections

For TTL Converter:

How to connect the PL2303 USB to TTL converter with the dspic micro-controller

Pin Name on the ConverterCorresponding PIN Name on the Micro-Controller






GND, Vss

5 V (optional)

Vdd, Vcc

  • Pin names are printed on the back side of the converter.
  • Exact pin numbers corresponding to their pin names must be found from the datasheet as they change from device to device.
  • A 5 V or 3.3 V supply from the converter may optionally be used to power the micro-controller.
  • It is mandatory to make the ground of the controller and converter common.

For PicKit:

The PicKit has a 6 pin connector with pin number 1 marked with a white arrow on the device.

Pin Number on the 6-Pin HeaderCorresponding Pin Name on the MCU


Vcc, Vdd


Gnd, Vss





  • Additionally the wiring schematic is also shown in an inset inside the PicKit's UART tool.

3. Send Data From dspic Through UART

After wiring the connections and testing the the TTL Converter module to make sure it has started working, upload the code for sending data through UART.

  • Text can be sent using the printf() function in the stdio.h library.
  • Variables can be sent by first converting them into array and then using printf() to print them.

Example Code to Send Data Through UART

#define FCY 30000000

#include <xc.h>
#include <stdio.h>
#include <delay.h>
#include <libpic30.h>
#include <math.h>
#include <p30F4011.h>

_FOSC(CSW_FSCM_OFF & FRC_PLL16);  // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off

void UART_Init ( void )
    _TRISF3 = 0;           //Look for the exact TX/RX pins of your pic device
    _TRISF2 = 1;           //TX pin must be set as output port and RX input
    U1BRG = 7;             // 115000 baud rate @ 30 MIPS
    U1MODEbits.UARTEN = 1; // Enable UART

int main( void )

    while( 1 )
         //To print a variable

         //To print text
         printf("Greetings from Pakistan\r\n");


4. Receive Data Through UART in dspic

To receive data from UART you will have to utilize a UART receive interrupt that will trigger every time a data packet is received in the UART receive registers (U1RXREG). This data is transferred to another variable and the receive buffer is cleared if full. You can use the following example code.

  • To make sure that the interrupt is actually triggering, an LED has been connected at Port D0 which will blink once when the interrupt is triggered.
  • If the LED blinks immediately after you send some data from the serial monitor, assume the code is working correctly.
  • The value of variable 'data' might also be checked in the debug mode.

Example Code to Receive Data From UART

#define FCY 30000000

#include <xc.h>
#include <stdio.h>
#include <libpic30.h>
#include <uart.h>
#include <delay.h>
#include <p30F4011.h>
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

 char data;

void Interrupt_Init( void )
    IEC0bits.U1RXIE = 1;

void __attribute__((interrupt, auto_psv)) _U1RXInterrupt( void )               
    _LATD0 = 1;
    _LATD0 = 0;
    data = U1RXREG;
    if ( U1STAbits.OERR = 1 )
        U1STAbits.OERR = 0;
    IFS0bits.U1RXIF = 0;

int main()
     // Set all port D pins as outputs
    _TRISD0 = 0;
    // Setup UART - most default options are fine
    return 0;

This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.

© 2017 StormsHalted


StormsHalted (author) from Pullman, Washington, United States on January 02, 2018:


Please see the table in the article which shows pin names of the UART module and their corresponding pin names on PIC.

Actual pin numbers are to vary, depending on the model and series of PIC used but pin names will always work.

Jeremy on January 01, 2018:

This is fantastic content!

Do you think you could please upload a circuit diagram of how you connected your dsPIC33F and Arduino?

Thank you