How to use UART on Raspberry Pi

Raspi UART
In order to use the dedicated UART pins on the raspberry pi, first they have to be removed from their default application which is debugging.
To do this,we need to edit “/boot/cmdline.txt” and “/etc/inittab“.
You can backup this files if you want to return to the default configuration:

$ sudo cp /boot/cmdline.txt /boot/cmdline.bak
$ sudo cp /etc/inittab /etc/inittab.bak

Now, you can edit files:

$ sudo nano /boot/cmdline.txt

Remove “console=ttyAMA0,115200” and “kgdboc=ttyAMA0,115200” configuration parameters from the “/boot/cmdline.txt” configuration file, save and quit.

$ sudo nano /etc/inittab

Comment the last line on the “/etc/inittab” file. Put a ‘#’ before “T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100, save and quit.

Now the RXD (GPIO15) and TXD (GPIO14) pins are available for general UART use.

Here is the code of uart part for my project, uart.c:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include "Uart.h"

int uart0_filestream = -1;

void UART_Init()
	//----- SETUP USART 0 -----
	//The flags (defined in fcntl.h):
	//	Access modes (use 1 of these):
	//		O_RDONLY - Open for reading only.
	//		O_RDWR - Open for reading and writing.
	//		O_WRONLY - Open for writing only.
	//	O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
	//											if there is no input immediately available (instead of blocking). Likewise, write requests can also return
	//											immediately with a failure status if the output can't be written immediately.
	//	O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
	uart0_filestream = open("/dev/ttySAC0", O_RDWR | O_NOCTTY | O_NDELAY);//Open in non blocking read/write mode
	if (uart0_filestream == -1)
		printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
	//The flags (defined in /usr/include/termios.h - see
	//	Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
	//	CSIZE:- CS5, CS6, CS7, CS8
	//	CLOCAL - Ignore modem status lines
	//	CREAD - Enable receiver
	//	IGNPAR = Ignore characters with parity errors
	//	ICRNL - Map CR to NL on input
	//	PARENB - Parity enable
	//	PARODD - Odd parity (else even)
	struct termios options;
	tcgetattr(uart0_filestream, &options);
	options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;//<Set baud rate
	options.c_iflag = IGNPAR | ICRNL;
	options.c_oflag = 0;
	options.c_lflag = 0;
	tcflush(uart0_filestream, TCIFLUSH);
	tcsetattr(uart0_filestream, TCSANOW, &options);

void UART_TX_Char(char tx_buffer)
	if (uart0_filestream != -1)
		int count =write(uart0_filestream, &tx_buffer, 1);
		if (count < 0)
			printf("UART TX error\n");
void UART_TX()
	//----- TX BYTES -----
	unsigned char tx_buffer[20];
	unsigned char *p_tx_buffer;
	p_tx_buffer = &tx_buffer[0];
	*p_tx_buffer++ = 'H';
	*p_tx_buffer++ = 'e';
	*p_tx_buffer++ = 'l';
	*p_tx_buffer++ = 'l';
	*p_tx_buffer++ = 'o';
	if (uart0_filestream != -1)
		int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));//Filestream, bytes to write, number of bytes to write
		if (count < 0)
			printf("UART TX error\n");
int UART_RX()
	int check = 0;
	if (uart0_filestream != -1)
		// Read up to 255 characters from the port if they are there
		int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);//Filestream, buffer to store in, number of bytes to read (max)
		if (rx_length < 0)
			//An error occured (will occur if there are no bytes)
		else if (rx_length == 0)

			//Bytes received
			rx_buffer[rx_length] = '\0';
			printf("%i bytes read : %s\n", rx_length, rx_buffer);

void UART_Close()


#ifndef __UART_H
#define __UART_H

unsigned char rx_buffer[256];

void UART_Init();
void UART_TX_Char(char a);
void UART_TX();
int UART_RX();
void UART_Close();


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s