i am trying to send some integer values from Python to Arduino using the serial port, Python code seems to be working because i see the TX LED flicker on the Ardunio board, but still my LED connected to the 12th pin does not light up, Arduino code also seems to be working because when i open the serial port of the IDE of Arduino and i send '1' the LED lights up so i think there is some kind of incompatibility between the data sent by Python and the type Arduino expecting. Python Code:
ser = serial.Serial('COM3', 9600)
time.sleep(1)
ser.write('1'.encode())
Arduino Code:
void setup() {
Serial.begin(9600);
pinMode(12,OUTPUT);
}
void loop() {
int X;
if (Serial.available()>0)
{
X = Serial.read();
if(X == '1')
{
digitalWrite(12,HIGH);
}
else if(X == '0')
{
digitalWrite(12,LOW);
}
}
}
so my questions are:
1)_what seems to be the problem in the codes?
2)_what type of data does Arduino expect to get from the serial port?
3)_what is the best way to send an integer value over 255 from Python to Arduino? is this a proper way ?
ser = serial.Serial('COM3', 9600)
S = 102
time.sleep(1)
data = [int(x)for x in str(S)]
for d in data:
c = bytes(str(d), "ascii")
ser.write(c)
Serial.write(X)
and reading it back in python... also try try usingSerial.readString
... i think whatSerial.read
gets isord('1')
or 48 .. where as readString gets "1" (maybe...) – Joran Beasley