VSCode Loses Connection to Port after Upload and Monitor for specific code

Just started messing around with Arduino for the first time and was following these sample projects

Everything has been working great until I got to this code:

int potpin = 0; // initialize analog pin 0
int ledpin = 11; //initialize digital pin 11(PWM output)
int val = 0; // Temporarily store variables' value from the sensor
void setup() {
  pinMode(ledpin, OUTPUT); // define digital pin 11 as “output”
  Serial.begin(9600); // set baud rate at 9600
  // attention: for analog ports, they are automatically set up as “input”
}
void loop() {

  val = analogRead(potpin); // read the analog value from the sensor and assign it to val
  Serial.println(val); // display value of val
  analogWrite(ledpin, val / 4); // turn on LED and set up brightness(maximum output of PWM is 255)
  delay(10); // wait for 0.01 second
}

When I Upload and Monitor this the first time, the program works as expected. However, when I try and Upload another program (simple Hello World to reset), VSCode / PlatformIO cannot detect the Port anymore. It is isnt until I upload a program from the official Arduino Cloud IDE that it begins to work again.

Is there a setting that I am missing that is preventing this from working, or something specific with this code?

Please also show the content of your platformio.ini.

Did you set the exact same baudrate as it is in our sketch?

monitor_speed = 9600

How did you know that? (Where do you look). The ports are managed by your operating system. PlatformIO will only show what’s available. Please check if the port is available in your OS.

Here is my INI file:

[env:uno]
platform = atmelavr
board = uno
framework = arduino
upload_port = /dev/ttyACM0
monitor_filters = printable, send_on_enter
monitor_echo = yes

How did you know that? (Where do you look). The ports are managed by your operating system. PlatformIO will only show what’s available. Please check if the port is available in your OS.

Maybe loses the port isn’t the right term. More like it hangs when it tries to upload the source code. When I select Upload and Monitor from the VSCode Command Palette, in the terminal that pops open at the bottom, the line it hangs on when it says attempting to connect / upload to /dev/tty{ArduinoPort}

On the actual board itself, after uploading the Source Code in my original post, the TX and RX LEDs are lit up the entire time.

Do you have anything connected to the D0 or D1 pin? You’re only supposed to plug something into A0 and D11.

Do you have the udev rules installed and is your used in the appropriate groups? https://docs.platformio.org/en/stable/core/installation/udev-rules.html

Have you uninstalled interfering software like brltty?

When you do just “Upload”, wait a bit, and then press “Monitor”, does the serial monitor start?

Do you have any other serial monitor programs open concurrently?

Do you have anything connected to the D0 or D1 pin? You’re only supposed to plug something into A0 and D11.

No only what is being used in the project tutorial.

Do you have the udev rules installed and is your used in the appropriate groups? https://docs.platformio.org/en/stable/core/installation/udev-rules.html

Yes, first ever time I tried to upload something it warned me about this. Installed, restarted computer, didn’t get warning after that.

Have you uninstalled interfering software like brltty?

Don’t see it installed currently, so “yes”.

When you do just “Upload”, wait a bit, and then press “Monitor”, does the serial monitor start?

So if I am uploading this source code for the first time, Yes. However, if I make a slight tweak to the code (I.e. set val to 1), no it does not, when it says its attempting to upload to the port, it just hangs.

Do you have any other serial monitor programs open concurrently?

To my knowledge no, this is a fairly clean / recent install of Debian Linux on this laptop.

I want to be clear, the program that I upload does work on the Arduino (dimmer of LED bulb). It’s just that after uploading it, I cannot upload anything else via PlatformIO. Only when I go to the Arduino Cloud IDE will it work. And again as mentioned above, the TX and RX LEDs are constantly on which leaves me to believe it has something to do with the analogRead and analogWrite commands possibly causing the issue.

This should not happen at all.

Can you disconnect your Arduino Uno from the circuit / breadboard, so that it’s not connected to any wires except the USB cable, and try to reupload and monitor the sketch? Does it still not open the serial port after the first upload?

I will do that now, is there any output or verbose setting you want me to run?

So I tried it again, and now I am not getting that issue. I apologize for raising this, clearly some sort of PEBKAC ID10T error