Arduino Function Serial.read() and Serial.readString()

Introduction: Arduino Function Serial.read() and Serial.readString()

Serial monitor of Arduino is a very useful feature.Serial monitor is used to see receive data, send data,print data and so on.Serial monitor is connected to the Arduino through serial communication. This serial communication occurs using RX (pin 0) and TX (pin 1) terminal of Arduino. Any kind of data can send through this serial monitor. To read incoming data in Arduino "Serial.reads ()" And "Serial. readString ()" are two very useful functions. This to function reads the data which are come to Arduino serial port. But there is a little difference between the two functions."Serial. reads ()" function reads the data in bytes.it mean if you write a command "int a=Serial.read();",then data store in "a" in bytes."Serial.readString()" read the serial data in string.It mean for "String a=Serial.readString();" command "a" store string.In the picture there are two photos one is ardino IDE serial monitor, another is proteus virtual terminal.

Step 1: Serial.read()

Serial Function

read() use to reads incoming serial data. read().

This has no parameter.

Returns

The first byte of incoming serial data available (or -1 if no data is available) - int

Syntax
Serial.read()

For more information

Link: https://www.arduino.cc/en/Serial/Read

Step 2: Serial.read() Example

Code:

int incomingByte = 0;

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

if (Serial.available() > 0) {

incomingByte = Serial.read(); // read the incoming byte:

Serial.print(" I received:");

Serial.println(incomingByte);

}

}

This is an example code of "Serial.reads ()" function.I send data from serial monitor and that data read Serial.read () function. As I said receive data by using Serial.reads () function is in bytes. So an integer variable is declared. To understand what is happeningin Serial.read () function Serial.println () function. But if we use Serial.writes () function then we can see which character is pressed in serial monitor.

Youtube

Step 3: Serial.readString()

Serial Function

readString() reads characters from the serial data into a string.

This has no parameter.

Returns
String that read from the serial.

Syntax
Serial.readString()

For more information

Link:https://www.arduino.cc/en/Serial/ReadString

Step 4: Serial.readString() Example

Code:

String a;

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

while(Serial.available()) {

a= Serial.readString();// read the incoming data as string

Serial.println(a);

}

}

"Serial. readString ()" function read serial data as a string. So, when some data are given in serial, Serial.readString () function read as string. In the picture uses a virtual terminal as serial monitor. I wrote "My name is Mamun" after some time it gives return "My name is Mamun". From here it is clearly understandable how does the "Serial.readString()" function work.

Youtube

1 Person Made This Project!

Recommendations

  • Lamps and Lighting Contest

    Lamps and Lighting Contest
  • Space Contest

    Space Contest
  • Organization Contest

    Organization Contest

21 Comments

0
bhatseerat20
bhatseerat20

Question 8 months ago

Plz tell me why the following code is not working.

16129775556203843619492072072009.jpg
0
themohdhaziq
themohdhaziq

Answer 8 months ago

Good Day bhatseerat20,

At a glance, I can see 2 main issues:

1. your string is "abc" but your 'if' function is looking for " abc ". Note the spaces.

2. 'while Serial.available()' is checking if you are receiving serial data (one way is the RX Pin 0 on an Arduino UNO). You could connect this pin to the TX of another microcontroller to do this.

If not, you could try to Serial.print the string 'x' before the 'Serial.available()' line, but I'm not sure if that would work.

Do let me know!

0
EarlWallaceNYC
EarlWallaceNYC

5 years ago

Thanks for the info.
The documentation on Arduino's String class is hard-to-come-by.

0
pezleha2
pezleha2

Reply 9 months ago

That is the ugly truth

0
VVS_98
VVS_98

Question 1 year ago on Step 3

Once you receive string into a variable say 'a' then can we find stringlength using the function strlen()or something like that?, Also can we access individual component elements like we access individual elements from string arrays in C?

0
TMPK
TMPK

1 year ago

Can you please make a video about how to control multiple servos via bluetooth. It would be really helpful.

0
mdjunaidahmed0123
mdjunaidahmed0123

Question 1 year ago on Step 2

If i enter a letter 'a' then its is returning three values like shown below. please resolve.
//////////////////////////////
I received: 97
I received: 13
I received: 10

0
Genital_Admiral
Genital_Admiral

Answer 1 year ago

That happens if you have activated 'Both NL & CR' in the bottom right corner of your serial monitor. NL stands for new line and CR stands for carriage return. When you are sending a letter over the terminal it is an ASCII Value. So in your case the 97 translates to 'a' in ASCII, the 13 is the CR and the 10 is the NL. Just google 'ASCII Table' and you will find these values there as the 'DEC' values. Hope that helps

0
BhadaniJ
BhadaniJ

5 years ago

hi

0
mdjunaidahmed0123
mdjunaidahmed0123

Reply 1 year ago

If i enter a letter 'a' then its is returning three values like shown below. please resolve.
//////////////////////////////
I received: 97
I received: 13
I received: 10

0
BhadaniJ
BhadaniJ

5 years ago

bov mast

0
TechnicalS10
TechnicalS10

Reply 2 years ago

hello jaydeep im parth from gec bhuj your junior can u jst tell me how to write the serial read data of the arduino to peripheral device on uart pin.

0
russ_hensel
russ_hensel

6 years ago on Introduction

The price of Proteus seems a bit steep for diy projects. Do you know of a free, legal version?

0
Abdullah_Al_Mamun_EEE_EWU_Bangladesh
Abdullah_Al_Mamun_EEE_EWU_Bangladesh

Reply 6 years ago on Introduction

hi, i am using a free version of "PROTEUS 7.2 sp2".You can download this link https://docs.google.com/folderview?id=0B6bMc7UD3os... hope this will help you.I don't know is this legal or not.Also there are other links from where you can download Proteus.And in PROTEUS 7.2 sp2 you need to add arduino, all information are available in the link

Link: https://www.instructables.com/id/How-to-Simulate-Ar...

Thanks.

0
codebeat
codebeat

Reply 4 years ago

That is definitely NOT legal and also when you include a crack like you do. The software costs about 600 dollars. You don't use a free version, you are using a cracked version. Be careful, it can contain virusses, trojans and other not to want software. Be warned.

0
rohank26
rohank26

5 years ago

in serial.read it means it returns the first byte of incoming data.What about the second byte will it be returned or not?My second query is what is the difference between incoming data and stored data in the serial buffer.

0
BhadaniJ
BhadaniJ

5 years ago

maja na avi

0
Fezder
Fezder

5 years ago

Thanks mate, this helped a lot!