I'm trying to do a serial read:

    if (Serial.available() > 0) {
  int inputValue = Serial.read();
  Serial.println("I got this:");
  Serial.print(inputValue, BIN);

However, it seems that what I receive is always 6 bits first and then followed by 4 bits. I'm really confused. I used a different board just to be sure that it's not just a problem with the board. Could it be the serial monitor I'm using? I'm using the Arduino serial. This is an example of the output:

21:31:02.021 -> 
21:31:02.021 -> I got this:
21:31:02.021 -> 110101
21:31:02.021 -> I got this:
21:31:02.021 -> 1010

Can anyone shine a light as to what is happening here?

  • you are confusing what you receive with what you print
    – jsotola
    Jul 2 '20 at 19:43

I guess you are sending 5 and have the line ending set to newline only. Then everything is correct and as expected. You see 2 bytes, that are transmitted. Serial.print() will cut out the leading zeros of the byte, so all other digits before that are just zero.

The first byte is the character 5 encoded in ASCII (53 is the decimal representation of the ASCII value here). The second byte is the newline character \n, which marks the end of a line or the end of the data message (depending on the interpretation of the data). If you don't want to send the newline character, select Nothing for the line ending (My Arduino IDE is in German, so I don't know, what exactly is the term in the english version here, but you need to set it for not using any line ending).

As it seems, that you don't know that currently: When you write a number into the serial monitor, you are not sending the direct binary value of that number. You are sending the ASCII representation of the individual digits. ASCII is a system, where the values of a byte represent a character. That includes decimal digits, the alphabet and some special characters. So when you type "255", you are not sending 0b11111111 (1 byte with all bits set --> decimal 255), you are sending 3 bytes with the ASCII byte value for the characters 2, 5 and 5. You can read the byte values for all the characters in tables, just by googling for "ASCII" and looking at the images found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.