Serial.read()

Description

Reads incoming serial data and consumes it from the serial buffer.

Serial.read()
inherits from the Stream utility class.

Syntax

Use the following function to read incoming serial data:

Serial.read()

Parameters

The function admits the following object:

Serial
: serial port object. See the list of available serial ports for each board on the Serial main page.

Returns

The function returns the first byte of incoming serial data available (or -1 if no data is available). Data type:

int
.

Example Code

1int incomingByte = 0; // for incoming serial data
2
3void setup() {
4 Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
5}
6
7void loop() {
8 // send data only when you receive data:
9 if (Serial.available() > 0) {
10 // read the incoming byte:
11 incomingByte = Serial.read();
12
13 // say what you got:
14 Serial.print("I received: ");
15 Serial.println(incomingByte, DEC);
16 }
17}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.

ON THIS PAGE