2

I am writing a very simple code in Arduino Nano, in which I am reading data that I send on Serial. Here is the code snippet.

int incomingByte ; 
void setup() {
 Serial.begin(9600); 
}
void loop() {
 if (Serial.available() > 0) {
 incomingByte = Serial.read();
 Serial.print("I received: ");
 Serial.println(incomingByte);
 }

}

When I send 'A' , I receive the following output:

I received: 65

I received: 13

I received: 10

What is wrong with the working of Serial Port here?

Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked Jul 24, 2017 at 11:38

3 Answers 3

3

nothing is wrong, it's to do with operator overloading and the print function trying to be helpful.

if you change this line:

int incomingByte ;

to this:

char incomingByte ;

it should display correctly

And the extra characters are because the software on the computer is sending line endings. these may be a linefeed with or without a carriage return. you could either just ignore them, or make use of them to create multi character commands (if you need them)

Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
answered Jul 24, 2017 at 11:55
4
  • hi, thanks for your input , but when I change it to char I still receive following :I received: A I received: I received: Commented Jul 24, 2017 at 12:02
  • I don't want trailing carriage return and enter in my output, I simply need the character. How to trim ? Commented Jul 24, 2017 at 12:03
  • If you don't want any trailing carriage returns, just ignore them Commented Jul 24, 2017 at 12:21
  • @James Kent ... sorry for editing your answer (I moved it to my own answer). But still upvoted yours (for the inconvenience). Commented Jul 24, 2017 at 15:01
3

You send a string "A"

However, to denote the end, a carriage line (code 13) + line feed (code 10) is sent.

So what is sent is actually an 'A' character followed by a return. This way you also know when a new line starts

Just strip off the return (13,10) items if you do not need them.

If you do not want to send the return, try sending 'A' (which is the character A, and not the string A.

Update

You can ignore them by:

 if ((incomingByte != 10 && (incomingByte != 13))
 {
 Serial.print("I received: ");
 Serial.println(incomingByte);
 }

If you only want printable characters, check if you can use the isprint function (isprint function

so it should look like

 if (isprint(incomingByte))
 {
 Serial.print("I received: ");
 Serial.println(incomingByte);
 }
answered Jul 24, 2017 at 11:55
3
  • can you please tell me how to strip return (13, 10)? Commented Jul 24, 2017 at 12:04
  • I added it initially accidentally to the wrong answer, now added it to my own. Commented Jul 24, 2017 at 15:01
  • The line feed/carriage return is used to terminate the line, not the string. You can send the "A" all by itself (no delimiter); with a single cr or lf; or both (cr & lf). Commented Jul 24, 2017 at 16:13
2

this is just because you are typing a letter i the serial monitor ans in your code your are declaring it as a integer 'int' so you not need to worry about that just try the following code

int incomingByte ; 
void setup() {
 Serial.begin(9600); 
}
void loop() {
 if (Serial.available() > 0) {
 incomingByte = Serial.read();
 Serial.write(incomingByte);
 }
}

-------------------------------- OR ---------------------------------------

char incomingByte ; 
void setup() {
 Serial.begin(9600); 
}
void loop() {
 if (Serial.available() > 0) {
 incomingByte = Serial.read();
 Serial.print(incomingByte);
 }
}

Both of the programs will work try that.......

answered Jul 26, 2017 at 3:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.