3
My Arduino UNO is rebooting when I use its serial port with a Python script. But when I open the serial terminal from the Arduino IDE and run the same Python script, it works normally. Why this happens and how to solve?
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'
Make sure you have access to access /dev/ttyACM0, during some tests I found problems trying to access Arduino without being root.
– Pedro Henrique
I tried this but got no results, it works normally when the serial monitor of the Arduino IDE is open, otherwise no.
– Kaoê Menna
turn off the DTR in the python serial itself, which probably solves.
– Bacco