
In this tutorial, we will learn how to program the USART (uart) of ATmega328P microcontroller to communicate with a Linux/ Windows PC using asynchronous serial communication protocol.
The ATmega328p microcontroller will send and receive data (ASCII Strings) to a PC running either a Linux or Windows operating system using it's (ATmega328p) UART pins.
The data will be received/transmitted by a terminal emulation program like PuTTY or Tera Term. as shown in the below figure.

All the codes for configuring the USART (UART) of the ATmega328P is written in embedded C language and can be compiled using AVR - GCC compiler (Atmel Studio /Microchip Studio or MPLAB X IDE).
Source Codes
The code for the microcontroller section is written in embedded C and compiled using AVR-GCC (WinAVR) .
Hex code is uploaded into Atmega328p with AVRDUDE using USBasp Programmer.
All Source codes including "Make" files are available on our Github Repo.
Download the entire ATmega328P USART Programming Tutorial Source codes as a Zip File
Hardware Connection Setup
We are using a 32 pin TQFP version of the ATmega328p microcontroller .
If you are using the 28 pin DIP version, please make sure that the pin numbers match by referring to the data sheet of ATmega328p.

Here ATmega328p is clocked with an external Quartz crystal running at 11.0592MHz.If you are using a different clock frequency modify the F_CPU value accordingly
You can check this tutorial to learn how to configure ATmega328p Microcontroller to use an external Quartz crystal as CPU clock source.
Please note that ,
- TXD of ATmega328p is connected to RXD of USB to Serial Converter
- RXD of ATmega328p is connected to TXD of USB to Serial Converter
- GND should be common between ATmega328P and USB to Serial Converter
The following block diagram shows the hardware connections of the setup.

Here is the image of an ATmega328P sending data to PC using USB2SERIAL V3.0 (USB to Serial/RS232/Rs485 Converter)

Since the PC cannot receive the UART signals directly from the ATmega328P microcontroller, a USB to Serial Converter based on FT232RL is used to convert the serial TTL signals to USB Protocol.


Configuring ATmega328P USART for Serial Communication
Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART) is a configurable peripheral of ATmega328P which supports both Synchronous (SPI) and asynchronous(Serial) communication protocols.
In our case we will be dealing only with asynchronous communication.
ATmega328P USART Initialization
The USART has to be initialized before any communication can take place.The initialization process consists of the following steps.
Setting the Baud rate
Setting the frame format (here 8N1)
Enabling the Transmitter or Receiver depending upon the usage.
Setting the Baud rate
USART supports all commonly used baud rates from 2400 bps to 230.4kps without any issues.You can go up to 2.5Mbps for a crystal frequency of 20MHz.The maximum we are able to achieve on a Windows7 machine is 230.4k bps .
The required baudrate can be selected by writing the corresponding value to the UBBRn register.
The ATmega328p datasheet has a section on commonly used Baud rates,Error rates,Crystal Frequencies and their corresponding UBBRn values.You can look up that table on the datasheet and select your desired UBBRn values.
Tags
- Log in to post comments