ArduinoGeneral

Python Arduino Communication Problems

userHead Account cancelled 2020-07-27 11:13:17 698 Views0 Replies
Hi guys,

I am a beginner to arduino programming. I'm trying to turn on an LED using serial communication in python. Really simple stuff like just hitting 1 in the python shell turns on the LED, 0 turns it off, etc... for whatever reason this isn't working. Both code pieces compile and the arduino flashes no problem. However when I hit 1 in the shell, the RX LED on the Uno lights up briefly then turns off, and my LED from p13 to gnd doesn't do anything.

Here's my code, if anyone can take a look... I've tried adding delays into the python, but no luck. I don't know what's wrong at all, and can't find any online resources... the Arduino seems to "reset" almost and not run any code, if that makes sense.

Also yes, I've checked the polarity of the diode as well as tested blinky with it. Using LED_BUILTIN has same outcome as well.

Arduino:
Code: Select all
#define LED 13
int value;

void setup() {
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  digitalWrite(LED,HIGH);
  Serial.println("Connection established");
} 

void loop() {
  while(Serial.available()){
    value = Serial.read();
  }

  if (value == 1)
    digitalWrite(LED,HIGH);
  else
    digitalWrite(LED,LOW);
}
here is the python code:

Code: Select all
import serial
import time

ser = serial.Serial('COM7', 9600,timeout=0,writeTimeout=1)
time.sleep(2)

print(ser.readline())
print('Enter 1 to turn ON, 0 for OFF')

while 1:
    input_data = input() #wait for user input
    print("you entered", input_data) #print data for confirmation

    if(input_data == '1'):
        ser.write(1)
        print("ON")
        time.sleep(1)
    else:
        ser.write(0)
        print("OFF")
        time.sleep(1)