Arduino UNO restarts when using serial port outside the IDE

Asked

Viewed 966 times

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.

  • I tried this but got no results, it works normally when the serial monitor of the Arduino IDE is open, otherwise no.

  • turn off the DTR in the python serial itself, which probably solves.

1 answer

3

I’m starting with Arduino so I can’t speak properly. However I did some research on Google and found that Arduino makes a Auto Reset On Serial Connection after establishing a serial connection.

There are some ways to avoid this, the most common being the use of Jumper, or effect changes to the plate. What puzzles me is how the IDE can shut down Auto Reset On Serial Connection without changing the physical part of the board.

Analyzing some scripts in Python (I can’t program in Python), I noticed that many use a loop after connecting to the board.

from time import sleep
import serial

ser = serial.Serial('/dev/tty.usbmodem1d11', 9600)
while True:
    ser.write(Dados) 
    print ser.readline() 
    sleep(.1)

This causes the data to be sent at any time, so after the board restarts (if it restarts). The data will be available.

Another method that I thought was quite valid is that you could do the setup of Arduino write on the serial portal that he is ready. And waiting for this message in the Python script. Only after receiving this message would you send the message to the board. According to other users this option works perfectly.

If you need more information on how to disable the reset on the Hardware you can visit this link.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.