From Arduino IDE the input is responding and bulb is glowing. However, the same thing from Python is not working.
What is the issue?
Desired Behavior
Arduino IDE serial input -> response behavior should match serial input -> response behavior with Python code.
LED bulb on Pin 13
should be set to ON
/ HIGH
when serial port is written to with input 1
.
Sample Code
Arduino code:
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char serialListener = Serial.read();
if (serialListener == 1) {
digitalWrite(13, HIGH);
} else if (serialListener == 0) {
digitalWrite(13, LOW);
}
}
}
Python code:
import serial
ser = serial.Serial('COM3', baudrate=9600, timeout=1)
ser.close()
ser.open()
ser.write("1".encode())
if (serialListener == '1')
andelse if (serialListener == '0')
. What you have right now compares the ASCII value of the character it receives to 0 or 1, which is not what you want.