# Serial Port Not Reading Correctly

**URL:** <https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289>\
**Category:** Interfacing w/ Software on the Computer\
**Created:** [December 31, 2013, 1:17am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289 "2013-12-31T01:17:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 31, 2013, 1:17am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/1 "2013-12-31T01:17:40Z")

</div>

I am trying to get my Arduino Uno to communicate with Processing in order to use my Kinect Sensor, and ran in to trouble passing data from the Processing to the Uno. I tried running a basic sketch to change the brightness of an LED based on the serial input, and got some unusual results. No matter what I type into the Serial Monitor, Serial.read() returns -1. Please help!

```arduino
void setup(){
  pinMode(11, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    int input = Serial.read();
    // debug serial input
    Serial.println(Serial.read());
    Serial.println(input);
    // write to LED on PWM 11
    analogWrite(11, input);
  }
}

```

Sample Serial Monitor:  
keys pressed: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0

> -1  
> 49  
> -1  
> 50  
> -1  
> 51  
> -1  
> 52  
> -1  
> 53  
> -1  
> 54  
> -1  
> 55  
> -1  
> 56  
> -1  
> 57  
> -1  
> 48

---

<div class="post-metadata">

**Author:** ![el\_supremo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/el_supremo/32/214787_2.png) [@el\_supremo](https://forum.arduino.cc/u/el_supremo)\
**Post date:** [December 31, 2013, 1:23am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/2 "2013-12-31T01:23:36Z")

</div>

That output is correct (of course).  
If Serial.available() is non-zero, which is what you test for, you are only guaranteed that there is at least one character in the queue. You are reading two.

Pete

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 31, 2013, 1:34am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/3 "2013-12-31T01:34:54Z")

</div>

OK, I see what you mean. But why would it output numbers in the 50s when I send numbers less than 10?

---

<div class="post-metadata">

**Author:** ![el\_supremo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/el_supremo/32/214787_2.png) [@el\_supremo](https://forum.arduino.cc/u/el_supremo)\
**Post date:** [December 31, 2013, 1:42am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/4 "2013-12-31T01:42:06Z")

</div>

You are printing each character as a decimal number. Each character is represented using ASCII code. Look up an ASCII table on Google. You'll find that the character for 1 is represented by the decimal number 49.  
Changing "int input" to "byte input" will cause the variable to be printed as a character instead of as a decimal number.

Pete

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 31, 2013, 1:45am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/5 "2013-12-31T01:45:23Z")

</div>

Even doing that, I still get 49 back by pressing 1. This is what the code says now:

```cpp
 if (Serial.available()) {
    byte input = Serial.read();
    // debug serial input
    Serial.println(input);

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 31, 2013, 2:03am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/6 "2013-12-31T02:03:27Z")

</div>

I added debugging lines to the code I was actually using while communicating with Processing, and got different results. Now the serial input never equals 1 and byte input always equals 255

```cpp
if (Serial.read() > 0) {
    byte input = Serial.read();
    if (input == '1') {
      Serial.println("1");
      digitalWrite(13, HIGH);
    } else {
      Serial.println("not 1");
      digitalWrite(13, LOW);
    }
    Serial.println(input);
  }

```

Serial Monitor:

```cpp
not 1
255
not 1
255
not 1
255
not 1
255

```

---

<div class="post-metadata">

**Author:** ![el\_supremo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/el_supremo/32/214787_2.png) [@el\_supremo](https://forum.arduino.cc/u/el_supremo)\
**Post date:** [December 31, 2013, 2:19am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/7 "2013-12-31T02:19:14Z")

</div>

Why did you do this? It was right before.

```cpp
if (Serial.read() > 0) {

```

Pete

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 31, 2013, 2:44am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/8 "2013-12-31T02:44:54Z")

</div>

I don't know why I had that. Thanks. So that solved my main problem of the Arduino not talking to Processing (silly me). Now this is probably a really stupid question, but how do I get the serial input to read integers (i.e. values from 0 - 255)?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 11:28am UTC](https://forum.arduino.cc/t/serial-port-not-reading-correctly/202289/9 "2021-05-06T11:28:43Z")

</div>

Closed on May 6, 2021, 11:28am UTC


