My Arduino UNO is restarting when I use its serial port with a Python script. But when I open the serial terminal through the Arduino IDE and run the same Python script, it works normally. Why does this happen and how do I resolve it?
Arduino Code:
#define LED 10
char c;
int led = 0;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
while(!Serial) {
}
}
void loop() {
if(Serial.available()) {
c = (char)Serial.read();
Serial.println(c);
if(c == 'a') {
led = HIGH;
} else if(c == 'd') {
led = LOW;
}
digitalWrite(LED, led);
}
}
Python code:
#! /usr/bin/env python
import serial
import sys
if len(sys.argv) > 1:
command = sys.argv[1]
ser=serial.Serial('/dev/ttyACM0',9600)
if not ser.isOpen():
ser.open()
print command
ser.write(command)
ser.close()
else:
print 'usage: luz.py a|d'