Serial Library

We all know what Serial Monitor is. Now, in this blog, I will discussed what is Serial Library.



A Serial Library is a big collection of procedures, where all the procedures are related. Procedures are a list of things to do. In Serial Library, we have six common procedures like Begin(), Read(), Flush(), Println(), Print(), and Available().

Now, we are going to discussed the three commonly used procedures: Begin(), Print(), and Println ().

1. Begin()

Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

Syntax
Serial.begin(speed)

Parameter:

speed - is the baud rate in bits per second

Example:


void setup()
{
  Serial.begin(9600); // set up Serial Library at 9600 bps
}

void loop()
{

}

  
2. Print()

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. For example:
  • Serial.print(78) gives "78"
  • Serial.print(1.23456) gives "1.23"
  • Serial.print('N') gives "N"
  • Serial.print("Hello world.") gives "Hello world." 
Syntax
Serial.print(val)

Parameters
val: the value to print - any data type

Example:

void setup()
{
  Serial.begin(9600); // set up Serial Library at 9600 bps
}

void loop()
{
Serial.print("Hello World!"); // prints Hello World
delay(1000);
}


3. Println()

Is a shorthand for "print line". Note that the 6th letter in println is the letter L not the number 1.It is also prints data to the serial port as human-readable ASCII text but the difference is that it has new line when execute in the Serial Monitor.

Syntax

Serial.println(val)

Parameters

val: the value to print - any data type


Example:

void setup()
{
  Serial.begin(9600); // set up Serial Library at 9600 bps
}

void loop()
{
Serial.println("Hello World!"); // prints Hello World
delay(1000);
}

If you have questions, just comment below! Thank you :)
 
Previous
Next Post »

6 comments

Write comments
meekmicah
AUTHOR
August 1, 2016 at 12:08 AM delete

In your example Serial.print(1.23456) it gives a value "1.23". Is it always rounded off to the nearest hundredths? If yes, What if the the other decimal places are important? How can I display it all?

Reply
avatar
Unknown
AUTHOR
August 1, 2016 at 12:16 AM delete

Hi Micah! Floats/Decimals are similarly printed as ASCII digits, defaulting to two decimal places. If you want to specify the number of decimal places to use, we can use this parameter: Serial.print(val, format). val is the value to print and the format is the number of decimal places. For example:

Serial.print(1.23456, 0) gives "1"
Serial.print(1.23456, 2) gives "1.23"
Serial.print(1.23456, 4) gives "1.2346"

Reply
avatar
meekmicah
AUTHOR
August 1, 2016 at 12:28 AM delete

Thank you ma'am! That one helps =D

Reply
avatar
Unknown
AUTHOR
August 1, 2016 at 12:40 AM delete

Welcome Micah! Do you have another question regarding to Serial Library?

Reply
avatar
Unknown
AUTHOR
August 3, 2016 at 12:21 AM delete

Ma'am Daisy does serial library have a limitations?

Reply
avatar
Unknown
AUTHOR
August 3, 2016 at 12:51 AM delete

Hi Christian! Yes. Serial Library has its limitations like the following:

- Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

- Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

- On Arduino or Genuino 101 the current maximum RX speed is 57600bps

- On Arduino or Genuino 101 RX doesn't work on Pin 13

Reply
avatar

“Sadyang mailap ang humanap ng tunay na pag-ibig. Madaling sabihing mahal kita, madaling isiping mahal ka niya, pero paano mo malalamang... ito na, eto na talaga?”

Ramon Bautista, Bakit Hindi Ka Crush ng Crush Mo?