Serial Communication
The serial communication is a simple scheme that uses the UART (Universal
Asynchronous Receiver/Transmitter) on the Microcontroller. It uses,
o 5V for logic 1 (high)
o 0V for logic 0 (low)
For a 3.3V board, it uses
o 3V for logic 1 (high)
o 0V for logic 0 (low)
Every message sent on the UART is in the form of 8 bits or 1 byte, where 1 byte = 8
bits.
The messages sent to the computer from Arduino are sent from PIN 1 of the Arduino
board, called Tx (Transmitter). The messages being sent to the Arduino from the
computer are received on PIN 0, called Rx (Receiver).ime Ministers of India | List of Primeinister
of India (1947-2020)
These two pins on the Arduino UNO board look like the below image:
When we initialize the pins for serial communication in our code, we cannot use these
two pins (Rx and Tx) for any purpose. The Tx and Rx pins are also connected directly to
the computer.
The pins are connected to the serial Tx and Rx chip, which acts as a serial
to USB translator. It acts as a medium for the computer to talk to the Microcontroller.
The chip on the board looks like the below image:
The object can include any number of data members (information) and member
functions (to call actions).
The [Link]( ) is a part of the serial object in the Arduino. It tells the serial object to
perform initialization steps to send and receive data on the Rx and Tx (pins 1 and 0).
Arduino Mega has four serial ports. The Tx pins on the Mega board are listed below:
o 1 (TX)
o 18 (TX)
o 16 (TX)
o 14 (TX)
The Rx pins on the Mega port are listed below:
o 0 (RX)
o 19 (RX)
o 17 (RX)
o 15 (RX)
The communication with the Tx and Rx pins would cause interference and failed uploads
to the particular board.
If we require a serial port for communication, we need to use a USB-to serial adapter. It
is a mini USB connector, which converts the USB connection to the Serial RX and TX. We
can directly connect the adapter to the board.
There are five pins present on the USB-to serial adapter, including RX, TX, reset button,
and GND (Ground).
[Link] ( )
The [Link]( ) sets the baud rate for serial data communication. The baud rate
signifies the data rate in bits per second.
The default baud rate in Arduino is 9600 bps (bits per second). We can specify other
baud rates as well, such as 4800, 14400, 38400, 28800, etc.
The [Link]( ) is declared in two formats, which are shown below:
o begin( speed )
o begin( speed, config)
Where,
serial: It signifies the serial port object.
speed: It signifies the baud rate or bps (bits per second) rate. It allows long data types.
config: It sets the stop, parity, and data bits.
Example 1:
1. void setup ( )
2. {
3. [Link](4800);
4. }
5. void loop ( )
6. {
7. }
The [Link] (4800 ) open the serial port and set the bits per rate to 4800. The
messages in Arduino are interchanged with the serial monitor at a rate of 4800 bits per
second.
Let's consider another example.
Example 2:
It is shown below:
Arduino [Link] ( )
The [Link] ( ) in Arduino prints the data to the serial port. The printed data is stored
in the ASCII (American Standard Code for Information Interchange) format, which is a
human-readable text.
Each digit of a number is printed using the ASCII characters.
The printed data will be visible in the serial monitor, which is present on the right
corner on the toolbar.
The [Link]( ) is declared in two formats, which are shown below:
o print( value )
o print( value, format)
Note: In [Link]( ), S must be written in uppercase.
Where,
serial: It signifies the serial port object.
print: The print ( ) returns the specified number of bytes written.
value: It signifies the value to print, which includes any data type value.
format: It consists of number base, such as OCT (Octal), BIN (Binary), HEX
(Hexadecimal), etc. for the integral data types. It also specifies the number of decimal
places.
[Link]( value )
The [Link] ( ) accepts the number using the ASCII character per digit and value upto
two decimal places for floating point numbers.
Example 1:
1. [Link](15.452732)
Output:
15.45
It sends bytes to the printer as a single character. In Arduino, the strings and characters
using the [Link]( ) are sent as it is.
Example 2:
1. [Link]("Hello Arduino")
Output:
"Hello Arduino"
[Link]( value, format )
It specifies the base format and gives the output according to the specified format. It
includes the formats Octal -OCT (base 8), Binary-BIN (base 2), Decimal-DEC (base 10),
and Hexadecimal-HEX (base 16).
Let's understand by few examples.
Example 1:
1. [Link](25, BIN)
Output:
11001
It converts the decimal number 25 to binary number 11001.
Example 2:
1. [Link](58, HEX)
Output:
3A
It converts the decimal number 58 to hexadecimal number 3A.
Example 3:
1. [Link](58, OCT)
Output:
72
It converts the decimal number 58 to octal number 72.
Example 4:
1. [Link](25, DEC)
Output:
25
The conversion is from decimal to decimal. So, the output will be the same.
Flash Memory based strings
If we want to pass the flash memory in [Link] ( ) based on string, we need to wrap
the function statement with F.
For example,
[Link]( F ( "Hello Arduino") ).
Printing a Tab space
We can also print the tab in the output.
Let's consider the code below:
1. void setup ( )
2. {
3. [Link] ( 4800);
4. }
5. void loop ( )
6. {
7. [Link](" Hello Arduino" );
8. [Link](" \ t '');
9. }
Here, [Link](" \ t '') is used to print the tab in the output program.
[Link] ( )
The [Link] ( ) means print line, which sends the string followed by the carriage
return ('\r' or ASCII 13) and newline ('\n' or ASCII 10) characters. It has a similar effect as
pressing the Enter or Return key on the keyboard when typing with the Text Editor.
The [Link]( ) is also declared in two formats, which are shown below:
o println( value )
o println( value, format)
What is the difference between [Link]( ) and [Link]( )?
The text written inside the open and closed parentheses in the [Link]( ) moves in
a new line. With the help of [Link]( ) and [Link]( ), we can figure the order
and execution of certain things in our code.
Let's understand with an example:
Consider the below code.
1. void setup ( )
2. {
3. [Link] ( 4800);
4. }
5. void loop ( )
6. {
7. [Link](" Hello");
8. delay(1000);
9. [Link]("Arduino"); // It will print Arduino followed by a new line.
10. delay ( 1500); // delay of 1.5 seconds between each printed line.
11. }
Click on the Upload button-> Serial monitor for the output.
In the output, the word Hello will appear followed by the word Arduino 1 second later.
After 1.5 second, another line will be printed.
Output
1. Hello Arduino
2. Hello Arduino // The next line will be printed after the specified duration.
3. .
4. .
5. .
6. .
7. The output will be printed repeatedly.
Arduino Mega
The Arduino Mega board (type of Arduino board) has four serial ports. While
programming the Mega, we need to declare all the four serial ports. The baud rates of
all the four serial ports should be different.
It is shown below:
1. void setup() {
2. [Link](4800);
3. [Link](28800);
4. [Link](38400);
5. [Link](9600);
6.
7. [Link]("Serial port");
8. [Link]("Serial port 1");
9. [Link]("Serial port 2");
10. [Link]("Serial port 3");
11. }
12. void loop()
13. {
14. }
[Link]( )
The [Link]( ) function in Arduino gets the stored bytes from the serial port
that are available for reading. It is the data, which is already stored and arrived in the
serial buffer. The serial buffer in Arduino holds the 64 bytes.
[Link]( ) function inherits from the utility class called stream. The stream is only
invoked when the function relying on it is called. The stream class is considered as the
base class for binary and character-based streams.
Let's understand with an example.
Consider the below code.
1. int arrivingdatabyte = 0; // initializing the incoming serial byte
2. void setup( )
3. {
4. [Link](9600); // 9600 is the data rate in bps (bits per second).
5. }
6. void loop( ) // loop function that executes repeatedly
7. {
8. if([Link]( ) > 0) // It will only send data when the received data is greater than 0.
9. {
10. arrivingdatabyte = [Link]( ); // It will read the incoming or arriving data byte
11. [Link]("data byte received:");
12. [Link](arrivingdatabyte, DEC); // here, DEC means Decimal
13. }
14. }
In case of the Arduino Mega, the ports available are:
[Link]( )
[Link]( )
[Link]( )
Let's understand the concept of Mega with an example.
In this example, the data will be read and sent from one port to another.
It will read data from port 0 and will then send the data to port 1.
Consider the below code.
1. void setup( )
2. {
3. [Link](4800);
4. [Link](4800);
5. }
6. // two if conditions will be used.
7. //In first, if the data is available in the port 0, it will send to the port 1. In second, if the data is av
ailable in the port 1, it will send to the port 0.
8. void loop( )
9. {
10. // Now,the data will be read from port 0 and will be sent to the port 1.
11. if( [Link]( ) )
12. {
13. int incomingdatabyte = [Link]( );
14. [Link]('incomingdatabyte, byte');
15. }
16. // Now,the data will be read from port 1 and will be sent to the port 0.
17. if( [Link]( ) )
18. {
19. int incomingdatabyte = [Link]( );
20. [Link]('incomingdatabyte, byte');
21. }
22. }
We can also check the errors in our code by compiling. For that, click on
the Verify button.
The Arduino
screen will look like the below image:
The Done Compiling will signify the successful compiling of the code.
Arduino [Link]( ) and [Link]( )
Arduino [Link]( )
The [Link]( ) in Arduino reads the incoming serial data in the Arduino. The int data
type is used here. It returns the first data byte of the arriving serial data. It also returns -
1 when no data is available on the serial port.
The syntax used in the Arduino programming is [Link]( ),
Where,
serial: It signifies the serial port [Link]
The data is stored in the form of bytes, where 1 byte = 8 bits.
Let's understand with an example.
Consider the below code.
1. int arrivingdatabyte;
2. void setup( )
3. {
4. [Link](9600);
5. }
6. void loop( )
7. {
8. if([Link]( ) > 0)
9. {
10. arrivingdatabyte = [Link]( ); // It will read the incoming or arriving data byte
11. [Link]("data byte received:");
12. [Link](arrivingdatabyte);
13. }
14. }
The above code clearly explains that the [Link]( ) is used to get the available
number of bytes if it is greater than 0. The [Link]( ) function will read the data from
the data byte and print a message if the data is received. The data is sent from the serial
monitor to the Arduino.
[Link]( )
It reads the incoming serial data from the serial buffer in the string. The String data type
is used here.
Let's understand with an example.
Consider the below code.
1. String b;
2. void setup( )
3. {
4. [Link](4800);
5. }
6. void loop( )
7. {
8. while( [Link]( ) )
9. {
10. b = [Link]( );
11. [Link](b);
12. }
13. }
The above code clearly explains that the [Link]( ) is used to read the serial
data into the string. The string specified here is b.
The data in the Serial function is read as a string in the above code.
How serial data is read by [Link]( ) and [Link]( )?
The [Link]( ) function reads the data in terms of bytes, while the [Link]( )
reads the data in the term of string.
[Link]( )
It sends the binary data to the serial port in Arduino. The data through [Link] is
sent as a series of bytes or a single byte. The data type is size_t.
The [Link]( ) function will return the number of written bytes.
If we want to send the digits of numbers represented by the characters, we need to use
the [Link]( ) function instead of [Link]( ).
The [Link]( ) is declared in three formats, which are shown below:
o write( str )
o write( value )
o write( buffer, len)
Where,
Serial: It signifies the serial port object.
str: The str means string, which sends the data as a series of bytes.
buffer: It is an array that is used to send the data as a series of bytes.
value: It sends the data to the Arduino as a single byte.
len: It signifies the number of bytes, which can be sent from the array.
Let's understand with a simple example.
Consider the below code.
1. void setup( )
2. {
3. [Link](14400);
4. }
5. void loop( )
6. {
7. [Link](55); // the specified value is 55.
8. // [Link]( ) send the data as a byte with this value (55).
9. int Bytestosend = [Link]( " Arduino" );
10. // It sends the Arduino string.
11. //The length of the string is a return parameter in this function.
12. }