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
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
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
21 Comments
Question 2 years ago
Plz tell me why the following code is not working.
Answer 2 years 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!
7 years ago
Thanks for the info.
The documentation on Arduino's String class is hard-to-come-by.
Reply 2 years ago
That is the ugly truth
Reply 7 years ago
Thanks
Question 3 years 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?
3 years ago
Can you please make a video about how to control multiple servos via bluetooth. It would be really helpful.
Question 3 years 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
Answer 3 years 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
7 years ago
hi
Reply 3 years 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
7 years ago
bov mast
Reply 4 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.
Reply 4 years ago
hello parth,
i think first of all you read basic information about UART communication.
https://www.teachmemicro.com/microcontroller-seria...
above link useful to learn about UART
good wok
keep it up
never never never give up
8 years ago on Introduction
The price of Proteus seems a bit steep for diy projects. Do you know of a free, legal version?
Reply 8 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.
Reply 6 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.
7 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.
7 years ago
maja na avi
7 years ago
Thanks mate, this helped a lot!