Arduino Serial Communication Tutorial

Serial Communication is a protocol in an embedded system for communication between the microcontroller based devices like arduino and raspberry pie to other peripheral devices supporting the serial communication protocol. This protocol is also known as UART or USART(universal asynchronous receiver/transmitter). It follows the UART protocol

It is very popular because it uses only two wire for communication the digital pin 0(RX) and digital pin 1(TX). When using serial communication these two pins can’t be used as the digital pins.

What is UART

Any devices that have UART capability can communicate with each other with using the two wires TX and RX. A serial device sends information(byte) one bit at a time to the receiver at a certain rate that is called baud rate.

In UART there is a data frame to send the data. The start of the frame is Zero(0) tells the other UART receiver that some data is about to come. And then there is a Data of 8-bit length, each goes one bit by one bit. Then stop bit is one(1) indicating receiver that frame is over and be ready for the next frame in the same manner.

UART Baud Rate

For a reliable communication both the devices should have a same baud rate. In data communication some time both hardware are working at different frequencies even though we need to establish the communication between the devices. For doing this we need to calculate the common baud rate for both devices and putting this value in programming code on both devices. AVR microcontroller has a register called UBRR(UART baud rate register). By calculating the ubrr for a given baud rate and placing this into the programming we get the desired baud rate.
In arduino there is a library function,Serial.begin(9600) so we don’t require to calculate the . We simply put the desired baud rate we need.
Serial.begin(9600)
Here we have set the arduino to 9600 baud rate.

How to Connect Arduino to PC for Serial Communication

Arduino PC interface is very easy by writing serial communication program into arduino and by using serial monitor program. If a board doesn’t have USB to TTL IC in it we need to add it separately to perform serial communication with pc. One advantage here is arduino has some on board IC that converts the TTL serial data to USB understandable. And at the PC side, we have the Arduino IDE running its serial monitor for accepting the data coming from the arduino board. This data could be a text or any sensor value.
We have used serial communication in different arduino projects like:

How to Make Connections in Serial Communication

Suppose we have two arduino. Arduino 1 and Arduino 2 board want serial communication in between. The TX of one Arduino goes to RX of Arduino 2 and RX of Arduino 1 to TX of Arduino 2. Means a cross connection is required.

 

Bluetooth Serial Connection with Arduino

Arduino can be connected to any devices like gps, rfid-reader, bluetooth, GSM modem, RF pair and more. For communication, all peripheral should have UART in it. Then simply make cross connection between RX and TX of peripheral to the arduino.
Here we have shown a connection between arduino and bluetooth module.

bluetooth serial connection with arduino

 

Arduino Serial Monitor

Write the code for serial, that will read the sensor value you want to display over the PC using arduino serial monitor. A text also be displayed over the pc for some interaction with user and arduino. Its very easy to work with serial and serial monitor. By simply click, the serial monitor pops up. See images below:

Arduino Serial Monitor

A sensor value with some text is displayed over the pc by the arduino using serial communication.

Hardware Required

In this tutorial, we are covering some useful basic functions of arduino. So for that, we are using only the Arduino UNO board. For some project, we may require more components.

  • Arduino UNO

Wiring Diagram

Simply connect the board to PC and make sure driver is installed for the board and it is detected by PC.

Arduino Code for Serial Communication

Program 1:

Send Hello World to Serial Monitor

void setup(){
  //Initialize Arduino to 9600 baud rate
  Serial.begin(9600); 
  //send the below text to serial monitor once
  Serial.println("Hello World!");  
}

void loop() {}

Program 2:

Send Analog Data to Serial Monitor

This Arduino program reads the analog data from the analog pin A0 then send it to the serial monitor. Here for quick understanding, A0 pin is not connected to any sensor and it will display some random numbers. If you connect some sensor then it will display its data to the Arduino serial monitor. Touch your finger to A0 pin you will see the change in values in the serial monitor. We have a tutorial on Analog to Digital Conversion with Arduino where we displayed data over the serial terminal.

/*In this program we are reading the analog value from analog pin A0 converting it and send/print it to the serial monitor. Connection: Center Pin of Preset : Arduino Analog Pin A0 Outside Pin : Vcc/+5V Outside Pin : GND */

void setup() {  
  int ADCvalue = 0;  
   // initialize serial communication at 9600 baud rate
  Serial.begin(9600); } 
  void loop() { 
   // Read the analog value from pin A0 
   int ADCvalue = analogRead(A0); 
   // print the value at serial monitor 
   Serial.println(ADCvalue); 
   delay(100); 
   // delay in between reads for stability 
   }
}

Program 3:

Display value in DEC, HEX, OCT, BIN to Serial Monitor

int value = 10;  

void setup() {
  // open the serial port at 9600 bps
  Serial.begin(9600);
  // print value in many formats:
  Serial.println(value);       // print as decimal
  Serial.println(value, DEC);  // print as decimal
  Serial.println(value, HEX);  // print as hexadecimal
  Serial.println(value, OCT);  // print as octal
  Serial.println(value, BIN);  // print as binary
}
void loop() {  

}

Leave a Reply

Your email address will not be published. Required fields are marked *