Arduino serial begin: Communication of Arduino to other devices

Arduino can communicate with other Arduino, computers, or other devices like Bluetooth, GPS, GSM, and many more. These devices communicate with Arduino using a special communication protocol called serial communication. In this post, you will learn how to start serial communication between Arduino and the computer and print Arduino messages on the computer screen. You can start serial communication with Arduino using the serial begin command. We will discuss more it later. In serial communication, we use two transmission lines. One for transmitting data (Tx) and another for receiving data (Rx). Every Arduino board has at least one serial communication port. We call it UART (universal asynchronous receiver transmitter) or USART (universal synchronous asynchronous receiver transmitter). Some Arduino board has several serial ports. See below.

Serial port(s) or pins in Arduino

In Arduino UNO, Nano, Mini, and Mega you can use pin 0 and 1 in serial communication. We can connect any device to these pins which support serial communication. But before connecting any device to it we have to program the Arduino board. If you will try to upload the code while any device is connected to serial pins then it will give you an upload error. In simple words, we should not connect anything to pin 0 and 1 while we are programming the board.

Arduino can communicate with a computer using its USB cable. The serial monitor is available in the Arduino IDE. You have to just choose the port and board type then click on the serial monitor button. After opening the serial monitor you have to just choose the baud rate at which Arduino is programmed to send or receive data. You can program the baud rate of serial communication on Arduino using the begin() function.

To use other serial ports available on some boards, we will need a USB-to-serial adapter. Connect the TX pin of the adapter to the RX pin of the serial port and the RX pin of the adapter to the TX pin of a serial port. Connect them with the same ground.

Now let’s see some functions which are mostly used in serial communication.

Start the serial communication (Serial.begin() function)

In Arduino Serial.begin() function starts the serial communication at a specific baud rate as it takes the baud rate as a parameter. We call it generally in the setup section. The most used baud rate is 9600 baud per second or you can say it as 9600 bytes (character) in one second. We have other standard baud rate options and they are 2400, 4800, 19200, 28400, 115200, etc. We can pass these values as parameters while calling the Serial.begin() function in Arduino code to set the baud rate of serial communication.

Syntax- Serial.begin(9600);

if(Serial)

if(Serial) indicates if the specified ports are ready. Boards with native USB indicate whether USB CDC serial ports are open or not. For other boards and non-USB CDC ports, this will always return true.

Syntax: – if(Serial)

Serial.available()

Serial.available gets the number of bytes (character) available on the serial port. Arduino stores the received data in the serial buffer. Boards that use ATMega328p such as Arduino UNO, Nano, and other boards can store a maximum of 64 bytes of data in the serial buffer. This function returns the number of bytes available to read.

Syntax: – Serial.available();

We will see an example code for a better understanding. We will first initialize the serial communication at a 9600 baud rate and then check for serial availability. If there is any data present in the serial buffer then the LED on pin 10 will glow. Let’s see the code first.

int led = 10;                   //LED on pin 10
void setup() {
  pinMode(led, OUTPUT);         //define pin 10 as output
  Serial.begin(9600);           //Initialize serial communication at 9600 baud rate
}

void loop() {
  if(Serial.available()>0)      //Check for any character available in serial buffer
    digitalWrite(led,HIGH);     //turn on led when serial available
  else
    digitalWrite(led,LOW);      //otherwise led will be off
}

Reading serial data (Arduino Serial.Read() function)

After checking the serial buffer now we need to read the serial buffer and take action accordingly. To read the serial buffer we have the function called read(). Once it is called then the buffer will be erased. To avoid that you can call the function Serial.peek().

Syntax- Serial.read();

We will see an example of Serial.read() function and check for the message whether it is ‘a’ or not. Let’s see the code for that.

int led = 10;                   //LED on pin 10
void setup() {
  pinMode(led, OUTPUT);         //define pin 10 as output
  Serial.begin(9600);           //Initialiaze serial communication at 9600 baud rate
}

void loop() {
  if(Serial.available()>0){     //Check for any character available in serial buffer
    if(Serial.read()=='a'){
      digitalWrite(led,HIGH);   //turn on led when serial available is a
    }
    else
    digitalWrite(led,LOW);      //otherwise it will be off
  }
}

Serial.peek() function

In Serial.Read() function buffer gets cleared on every call. To avoid that Serial.peek() was introduced. If you see the serial content using this function then the content will be still there until you flush the buffer using the Serial.flush() function or Serial read it.

Sytax – Serial.peek();

Let’s have an example so that you can understand this better. Let’s see the code first.

int led = 10;                   //LED on pin 10
void setup() {
  pinMode(led, OUTPUT);         //define pin 10 as output
  Serial.begin(9600);           //Initialiaze serial communication at 9600 baud rate
}

void loop() {
  if(Serial.available()>0){     //Check for any character available in serial buffer
    if(Serial.peek()=='a'){
      digitalWrite(led,HIGH);   //turn on led when serial available is a
      delay(1000);
      digitalWrite(led,LOW);    //otherwise it will be off
      delay(1000);
    }
  }
}

Now let’s see the output on the proteus.

You can see that Led starts blinking when we send ‘a’ to the Arduino only once. This can happen only when this ‘a’ remains in serial buffer forever but we are reading the buffer to check the character is ‘a’ or not. We are reading the buffer using Serial.peek() function. So that means the peek function just reads the character but doesn’t erase it.

Printing string on serial monitor (Arduino Serial.print() function)

Till now you seen to check for available serial data and read it. Now you will see how we can send messages in serial communication. For that we use Serial.print() function.

Syntax- Serial.print();

Have a look on some examples.

int led = 10;                   //LED on pin 10
void setup() {
  pinMode(led, OUTPUT);         //define pin 10 as output
  Serial.begin(9600);           //Initialiaze serial communication at 9600 baud rate

  Serial.print(10);             //Prints 10 (Number)

  Serial.print(1.23456);        //Prints 1.23 (Decimal point number)

  Serial.print('A');            //Prints A (single chacter should be in single quotes)

  Serial.print("Hello world."); //Prints Hello world. (String shound be in double quotes)
}

void loop() {

}

As you can see that those messages got print on the serial monitor. But they are not properly structured. Each message needs to print line by line. We can do that using the Serial.println() function. Let’s see how.

int led = 10;                   //LED on pin 10
void setup() {
  pinMode(led, OUTPUT);         //define pin 10 as output
  Serial.begin(9600);           //Initialiaze serial communication at 9600 baud rate

  Serial.println(10);             //printlns 10 (Number)

  Serial.println(1.23456);        //printlns 1.23 (Decimal point number)

  Serial.println('A');            //printlns A (single chacter should be in single quotes)

  Serial.println("Hello world."); //printlns Hello world. (String shound be in double quotes)
}

void loop() {

}

Now look at the output.

It is now structured.

There are some advance features in print() function for displaying numbers. Let’s have a look.

int led = 10;                   //LED on pin 10
void setup() {
  pinMode(led, OUTPUT);         //define pin 10 as output
  Serial.begin(9600);           //Initialiaze serial communication at 9600 baud rate

  Serial.println(65, BIN);        //prints "1000001" (65 in binary)
  
  Serial.println(65, OCT);        //prints "101" (65 in Octal)
  
  Serial.println(65, DEC);        //prints "65" (65 in decimal)
  
  Serial.println(65, HEX);        //prints "41" (65 in hexadecimal)
  
  Serial.println(1.23456, 0);     //prints "1"  (0 decimal place)
  
  Serial.println(1.23456, 2);     //prints "1.23" (2 decimal places)
  
  Serial.println(1.23456, 4);     //prints "1.2345" (4 decimal places)
}

void loop() {

}

The output looks something like we expected.

Leave a Comment

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