user3546: I am new to Arduino (and computer programming in general), so I apologize if this question  looks silly.
Once I set up a basic arduino-LED connection, I have problems sending INTEGERS to arduino through the serial port. I can easily send characters such as 'm', 'o' and so on.. However if I send a number it looks like it simply does not get it.
Here is the Arduino code, ideally it should get a value from the usb port through python or the serial monitor and then adjust the brightness of the LED according to the value. (value must be in range [0,255]).
NOTE: I am using ARDUINO UNO and PYTHON 3
//--------------------------
int LED = 10;
int number;

void setup(){

pinMode(LED,OUTPUT);
Serial.begin(9600);
}

void loop(){

number = Serial.read();
Serial.print(number);
Serial.print('\n');
if(number == -1){
number = 0;
}

else if(number > 255){

number = 255;

}

else if(number < 0){

number = 0;
}

analogWrite(LED,number);
delay(1000);
}

However, when I input a value into the Serial port or through Python, for instance 0, it gives me 48 as answer (which, interestingly, is the ASCII code for 0!) and lights up the LED which is not what should happen since at 0 the LED should be off!!
I am missing something but I cannot find what... Could you please tell me what is wrong?
Here is the code I use in Python:
import serial
import time
try:
    arduino = serial.Serial(port,speed)
    time.sleep(2)
    print("Connection to " + port + " established succesfully!\n")
except Exception as e:
    print(e)

#Note: for characters such as 'a' I set data = b'a' to convert the data in bytes
#However the same thing does not work with numbers...
data = 0
data = arduino.write(valueToWrite)
time.sleep(2)
arduino.close()

what am I doing wrong or misunderstanding? Thank you.

user84: Serial.read() reads only ONE character from the serial port and returns its ASCII value, which explains the 48 you got.
To read a complete number, you have 2 options:

Use Serial.parseInt() that reads as many characters as needed and performs the conversion before returning the read number as an int. Please further check the reference here.
You read eachn character in a loop (with Serial.read()) until the last one, put each read charcater into a buffer (char array) then convert the resulting string to a number with the standard C function atoi.

Of course, the first option is the easiest one but it will return 0 if nothing is read after a 1s timeout (configurable with Serial.setTimeout()).

user368: Parsing on the Arduino can be slow and time-consuming (which is bad if you use clock prescaling or have time-critical tasks), so let's do it in Python.
The problem is that you're sending the numbers as ASCII whereas you need to be sending them as raw binary. This is where struct comes in.
3>> import struct
3>> print(struct.pack('>B', 0))
b'\x00'
3>> print(struct.pack('>B', 255))
b'\xff'
3>> print(struct.pack('>2B', 255, 0))
b'\xff\x00'
3>> print(struct.pack('>H', 9000))
b'#('

So what you really want is:
data = arduino.write(struct.pack('>B', valueToWrite))

or something to that effect.

3546: <p>I am new to Arduino (and computer programming in general), so I apologize if this question  looks silly.</p>

<p>Once I set up a basic arduino-LED connection, I have problems sending INTEGERS to arduino through the serial port. I can easily send characters such as 'm', 'o' and so on.. However if I send a number it looks like it simply does not get it.</p>

<p>Here is the Arduino code, ideally it should get a value from the usb port through python or the serial monitor and then adjust the brightness of the LED according to the value. (value must be in range [0,255]).</p>

<p>NOTE: I am using ARDUINO UNO and PYTHON 3</p>

<p>//--------------------------</p>

<pre><code>int LED = 10;
int number;

void setup(){

pinMode(LED,OUTPUT);
Serial.begin(9600);
}

void loop(){

number = Serial.read();
Serial.print(number);
Serial.print('\n');
if(number == -1){
number = 0;
}

else if(number &gt; 255){

number = 255;

}

else if(number &lt; 0){

number = 0;
}

analogWrite(LED,number);
delay(1000);
}
</code></pre>

<p>However, when I input a value into the Serial port or through Python, for instance 0, it gives me 48 as answer (which, interestingly, is the ASCII code for 0!) and lights up the LED which is not what should happen since at 0 the LED should be off!!
I am missing something but I cannot find what... Could you please tell me what is wrong?</p>

<p>Here is the code I use in Python:</p>

<pre><code>import serial
import time
try:
    arduino = serial.Serial(port,speed)
    time.sleep(2)
    print("Connection to " + port + " established succesfully!\n")
except Exception as e:
    print(e)

#Note: for characters such as 'a' I set data = b'a' to convert the data in bytes
#However the same thing does not work with numbers...
data = 0
data = arduino.write(valueToWrite)
time.sleep(2)
arduino.close()
</code></pre>

<p>what am I doing wrong or misunderstanding? Thank you.</p>
 84: <p><code>Serial.read()</code> reads only <strong>ONE</strong> character from the serial port and returns its ASCII value, which explains the <code>48</code> you got.</p>

<p>To read a complete number, you have 2 options:</p>

<ol>
<li>Use <code>Serial.parseInt()</code> that reads as many characters as needed and performs the conversion before returning the read number as an <code>int</code>. Please further check the <a href="http://arduino.cc/en/Serial/ParseInt" rel="nofollow">reference</a> here.</li>
<li>You read eachn character in a loop (with <code>Serial.read()</code>) until the last one, put each read charcater into a buffer (<code>char</code> array) then convert the resulting string to a number with the standard C function <code>atoi</code>.</li>
</ol>

<p>Of course, the first option is the easiest one but it will return <code>0</code> if nothing is read after a 1s timeout (configurable with <code>Serial.setTimeout()</code>).</p>
 368: <p>Parsing on the Arduino can be slow and time-consuming (which is bad if you use clock prescaling or have time-critical tasks), so let's do it in Python.</p>

<p>The problem is that you're sending the numbers as ASCII whereas you need to be sending them as raw binary. This is where <a href="https://docs.python.org/3/library/struct.html" rel="noreferrer"><code>struct</code></a> comes in.</p>

<pre><code>3&gt;&gt; import struct
3&gt;&gt; print(struct.pack('&gt;B', 0))
b'\x00'
3&gt;&gt; print(struct.pack('&gt;B', 255))
b'\xff'
3&gt;&gt; print(struct.pack('&gt;2B', 255, 0))
b'\xff\x00'
3&gt;&gt; print(struct.pack('&gt;H', 9000))
b'#('
</code></pre>

<p>So what you really want is:</p>

<pre><code>data = arduino.write(struct.pack('&gt;B', valueToWrite))
</code></pre>

<p>or something to that effect.</p>
