Default the Arduino UNO to serial communication?

Hello
I am new to Arduino and doing my first project on an Arduino UNO.

I am developing under Linux and want to switch an LED on and off via serial communication (via Arduinos USB).

This works great in the Arduino IDE Console. If I close it, it also works via the commands mentioned below.

But when I reconnect the Arduino, it does not work. I assume the firmware is not in ‘serial mode’.
How can I ensure that the Arduinos serial communcation is always accessible on /dev/ttyACM0?

void setup() {
  Serial.begin(9600);

  digitalWrite(13, HIGH);
  pinMode(13, OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    switch (inByte) {
      case '0':
        digitalWrite(13, LOW);
        break;
      case '1':
        digitalWrite(13, HIGH);
        break;
    }
  }
  delay(10);
}

In the end, I'd like to switch the LED with bash or python:

echo -ne '1' > /dev/ttyACM0
ser = serial.Serial('/dev/ttyACM0', 9600)
command = b'1'
ser.write(command)

Of course I've already searched here in the forum, I'm sure the question has already been asked. But I'm probably using the wrong english words.

Thank you
Steve

In your Python script, try adding a delay after opening the port. Opening the port resets the Uno, and it's missing your write command. In bash you'd have to play with stty to open the port w/o asserting DTR (which is what's causing the reset). It's probably possible, but I've never looked into it.

You're right van_der_decken, I would never have thought of that. But indeed, the LEDs flash with every connect, as if it were a restart.

edit: had a typo, it works with the delay!

Is there any way to prevent the restart? My script now always switches on the LED because of the setup(), only then does it react to the desired command.

(If I remove the digitalWrite(13, HIGH); in setup(), it always switches off the LED because of the restart. So sending 1 and 1 again toggles unexpected.)

The Audorino should not restart, but wait for new commands.
Unfortunately I can't prevent the new connection from Python, because I call one script for ‘ON’ and one for ‘OFF’.

You can try the following on the python side: python - Disable DTR in pyserial from code - Stack Overflow

com = serial.Serial()
com.port = port
com.baudrate = baud
com.timeout = 1
com.setDTR(False)
com.open()

Note that I'm not a python user so I have no idea if it will work; might also depend on operating system.

A physical way of preventing the reset is to cut the trace on the board. There is a small trace between two pads labelled "RESET EN"

If you cut this trace, the board will no longer reset when you open the com port. This also prevents you from programming the board so it is best to cut the trace and then solder a small switch across the two pads. With the switch "open" you will not get a board reset, but you can "close" the switch to re-program the board when needed. I have done this for several projects.

OK, you're right. I've now had time to test it.

The words to find the issue are ‘disable autoreset’ (I didn't realise it was the autorest at first).

The autoreset is done on purpose to be able to program the Arduino.

I have found four approaches:

  1. Pull reset input with 10k resistor plugged in to +5V. That didn't work for me.
  2. Buffer the reset input to ground with a 10µF capacitor plugged in. This did not work for me.
  3. Establish the connection without DTR. Didn't work for me even after many attempts.
  4. Disconnect = cut the Reset-EN line as @blh64 suggests.

Only 4) was successful. Now I can switch the LED on and off via serial commands (via USB) without any side effects.

Tkanks! Solved.