Serial.read is broken (adds 48 to every input)

int incomingByte = 0;   // for incoming serial data

void setup() {
  Serial.begin(9600);    
}

void loop() {


  if (Serial.available() > 0) {       // send data only when you receive data
    incomingByte = Serial.read();      // read the incoming byte


    Serial.print("I received: ");     // say what you got
    Serial.println(incomingByte);

   
  }
}

Screenshot serial Monitor

Serial.read() is reading a character, not a number

For example character '0' is decimal 48, character '1' is decimal 49 and so on

If you need the actual digit entered then subtract 48 from it as you have discovered

48 is ASCII code of character '0'. google "ASCII table"

UKHeliBob & juraj, thanks a lot for the help! :smiley:

(but before I fix this code I need to fix my doubleposting, ups)

You could also use Serial.write() but there's no Serial.writeln().

As a general comment on the Title of this Thread ...

You can be certain that a computer is NOT broken if it adds 48 to every input (or does anything else perfectly consistently)

...R

As a general comment on the Title of this Thread ...

Serial.read() does not add 48 to every input, only the digits 0 to 9

If you are wanting to echo the characters as received, then you can do this;

Serial.println((char)incomingByte);

"Serial.read() does not add 48 to every input, only the digits 0 to 9"

For other characters, it "adds" other amounts

www.asciitable.com

'0' to '9' come in as 48 to 57 decimal
'a' to 'z' come in as 97 to 122
'A' to 'Z' come in as 65 to 90

Serial data is transmitted as a series of 8 bit values, therefore these values will be in the range 0 to 255.

Serial.read() will capture the transmitted values one at a time in the assigned variable, Serial.read does not perform math the values you are seeing are what is being received.

The issue that you are experiencing is not that Serial.read() is malfunctioning the issue is how you are trying to display the value.

Generally you know ahead of time how and what kind of data packet you intend to transmit or receive, for example if you are using the serial port monitor of the IDE to enter and read values you will be working primarily with the values of ASCII character codes, as Juraj pointed out downloading a copy of the ASCII code table.

The ASCII code gives us a way to take numbers and make them readable for humans, if you look at at the table the values 32 to 127 are considered printable characters. ASCII is perhaps the easiest way to go to begin with and maybe later you might need a program that uses the raw values.

So sit down and think about what you are transmitting and how you will eventually handle the values at the receiving end. Try the example to give you a little insight.

int incomingByte = 0;   // for incoming serial data

void setup() {
  Serial.begin(9600);    
}

void loop() {


  if (Serial.available() > 0) {       // send data only when you receive data
    incomingByte = Serial.read();      // read the incoming byte


    Serial.print("I received char ");     // say what you got
    Serial.println((char)incomingByte);
    Serial.print("I received code value ");     // say what you got
    Serial.println(incomingByte);
    Serial.print("A different display method ");     // say what you got
    Serial.write(incomingByte);
    Serial.println("");
  }
}