USB connection to Adduino Uno

Hello,

I have an issue and a thought and would like input from others on how to resolve.

I have several programs to run on my Arduino. To make sure the board is clean, I run a progam called Clean_Up it has this code:

void setup() {}

void loop() {}

Then I run the program I select.

Here is the issue. The usb connection to my computer does not always connect. If I run Clean_Up I get no errors, and then Run one of my other programs I get the message at the end that it is done, however if I then try to start the Serial Monitor I get an error that the COM port is not working.

If I wiggle the usb connector and rerun the program after a few tries the load is good and the program runs perfectly.

I have replaced the usb cable 6 times and it does not fix the problem so it must be the computer port.

So here is my question and thought about what to do.

Is there a way to add some code to the Clean_Up that will check the connection between the computer through the usb cable to the board? Just FYI my board has devices connected to pins 8, 9, 10, 11, 12,and 13. What I would love to do is have the code open the serial monitor as well so it finds that error if it exists.

It is very frustrating to get everything set up and try to run a program and have it fail on this type of error. Also, sometimes in the load I do get an error that Com port 4 is not available and the load fails.

I thank you for any thoughts and guidance you can provide to help me.

Techhack: If I wiggle the usb connector and rerun the program after a few tries the load is good and the program runs perfectly.

I have replaced the usb cable 6 times and it does not fix the problem so it must be the computer port.

That's possible but it also might be the USB socket on the Uno. Even if you only wiggle the plug at the computer's USB port you're probably unintentionally wiggling the other end of the cable where it's plugged into the Uno.

Techhack: Is there a way to add some code to the Clean_Up that will check the connection between the computer through the usb cable to the board?

This will do it:

// Echos input from the serial monitor

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

void loop() {
  if (Serial.available() > 0) {
    Serial.write(Serial.read());
  }
}

If you send some input to the Arduino and receive the same back you know the USB data lines have a connection, rather than only the power lines.

Techhack: What I would love to do is have the code open the serial monitor as well

You could send a serial command to the computer from the Uno and have an application running on the computer that receives the command and starts the Serial Monitor (or more easily, a standalone serial terminal program). But that's a lot of work to do and of course if the USB connection is bad then that command will never reach the computer anyway so I don't think it's worth it.

I use the Blink sketch as my "clean up" to reset the board back to a known state. You can tell when Blink has been successfully loaded because the onboard LED starts blinking.

You could easily add a Serial.println("hello!"); to the Blink sketch.

Is this a laptop.

I have seen many laptops with (mechanically) broken USB sockets (user not careful enough). Use a different USB2 socket, or open the laptop and fix it, or buy a new laptop.

Clean the Uno? Never heard about that. Never had to do that. New code just overwrites the old code. Leo..

So you can always upload, but using serial monitor after that often fails?

If you use a 32U4 based board (e.g. Leonardo), you can program your cleanup to act as a HID and send some keystrokes M)to open the serial monitor.

For most other boards, the answer is no, you can’t control the PC from the Arduino without additional software running on the PC.

But you should fix the root cause of the issue instead of using workarounds.

Uploading a blank sketch could make sense before connecting components to your board so you know you won't have the previous program unexpectedly manipulating pins between the time you plug the board in and when the upload starts.

Hi all,

Thank you for your advice. I have implemented the serial availiable code and that has helped.