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. ![]() 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. ![]() 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( ). ![]() The Serial.write( ) is declared in three formats, which are shown below:
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.
Next TopicArduino analogRead ( )
|