Please note, this is a STATIC archive of website www.javatpoint.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Javatpoint Logo
Javatpoint Logo

Arduino Serial.read( ) and Serial.write( )

Arduino Serial.read( )

The Serial.read( ) 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 Serial.read( ),

Where,

serial: It signifies the serial port object.

Arduino Serial.read( ) and Serial.write( )

The data is stored in the form of bytes, where 1 byte = 8 bits.

Let's understand with an example.

Consider the below code.

The above code clearly explains that the Serial.available( ) is used to get the available number of bytes if it is greater than 0. The Serial.read( ) 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.

Serial.readString( )

It reads the incoming serial data from the serial buffer in the string. The String data type is used here.

Arduino Serial.read( ) and Serial.write( )

Let's understand with an example.

Consider the below code.

The above code clearly explains that the Serial.readString( ) 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 Serial.readString( ) and Serial.read( )?

The Serial.read( ) function reads the data in terms of bytes, while the Serial.readString( ) reads the data in the term of string.

Serial.write( )

It sends the binary data to the serial port in Arduino. The data through Serial.write is sent as a series of bytes or a single byte. The data type is size_t.

The Serial.write( ) 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 Serial.print( ) function instead of Serial.write( ).

Arduino Serial.read( ) and Serial.write( )

The Serial.write( ) is declared in three formats, which are shown below:

  • write( str )
  • write( value )
  • 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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA