Skip to main content

 

Bi directional asynchronous serial communication from Linux/Mac/Windows PC to Atmega328p microcontroller using uart in embedded c for absolute beginners

 

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.

atmega328p transmitting a string to windows /linux pc and displaying on putty console window

 

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 

 

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.

usart pin differences between 32pin tqfp and 28 pin pdip variants 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.

Bi directional asynchronous serial communication from Mac/PC to Atmega328p microcontroller tutorial with source code in C

 

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

interfacing atmega328p with Pc using usb2serial and python for serial communication

 

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.

atmega328p communicating with Linux /windows/mac PC using USB to serial/rs485 converter using usart

 

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.