SERIAL COMMUNICATION
As you saw in Chapter 5, you can communicate with devices over the USB port using a serial communication protocol. Here are the serial functions.
Serial.begin(speed)
Prepares Arduino to begin sending and receiving serial data. You'll generally use 9600 bits per second (bps) with the Arduino IDE serial monitor, but other speeds are available, usually no more than 115,200 bps.
Example:
Serial.begin(9600);
Serial.print(data) Serial.print(data, encoding)
Sends some data to the serial port. The encoding is optional; if not supplied, the data is treated as much like plain text as possible.
Examples:
Serial.print(75); // Prints "75"
Serial.print(75, DEC); // The same as above.
Serial.print(75, HEX); // "4B" (75 in hexadecimal)
Serial.print(75, OCT); // "113" (75 in octal)
Serial.print(75, BIN); // "1001011" (75 in binary)
Serial.print(75, BYTE); // "K" (the raw byte happens to
// be 75 in the ASCII set)Serial.println(data) Serial.println(data, encoding)
Same as Serial.print(), except that it adds a carriage return and linefeed (\r\n) as if you had typed the data and then pressed Return or Enter.
Examples:
Serial.println(75); // Prints "75\r\n" Serial.println(75, DEC); // The same as above. Serial.println(75, HEX); // "4B\r\n" Serial.println(75, OCT); // "113\r\n" Serial.println(75, BIN); // "1001011\r\n" Serial.println(75, BYTE); // "K\r\n"
int Serial.available()
Returns how many unread bytes are available on the Serial port for reading via the read() function. After you have ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access