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.
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’.
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.