27

I've uploaded a sketch to an Arduino Uno whose loop is something like this:

void loop(){
    Serial.println("Hello, World!");
}

So, now, I can't upload anything anymore, because the IDE says "port already in use".

Is there a way to "reset" the Arduino without another programmer?

Nothing else is using the serial port, and everything went just fine until I uploaded the previous sketch.

I've found some interesting things:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dugres
  • 12,613
  • 8
  • 46
  • 51
  • Do you have the Ardunio "Serial Monitor" window open? If so, that will block you from communicating with the board. – spring Jan 24 '15 at 19:51

21 Answers21

31

Try the following:

  1. Prepare the basic empty program (empty setup, loop, etc.)
  2. Compile it.
  3. Reset the Arduino using the hardware button on the chip
  4. Press Ctrl + U to upload your code.
  5. If unsuccessful - got to 3.

There is a delay before the boot loader starts the programs, just work on your timing. It worked for me when a bug in my Arduino's code was executing a soft reset every 500 ms.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Valed
  • 311
  • 3
  • 3
  • Worked the 8th time, thanks! Clicked the reset button about 1s after i started to upload my code. – Oskar Henriksson Nov 20 '20 at 20:15
  • 2
    It took me 2 hours but it finally worked. What worked for me was: (1) Hold down the reset button. (2) Press `Ctrl` + `U` to upload my code. (3) Wait until it says it’s uploading. (4) Release the reset button _and then press and release it again quickly_. – Brian Morearty Apr 27 '21 at 17:28
  • This general approach worked for me as well, except I instead ran this in terminal: `arduino-cli.exe upload . --fqbn arduino:mbed_nano:nano33ble --port COM4` (required a few tries of reset-button pressing on device). Presumably Arduino IDE runs something similar (with `COM4` replaced appropriately; on Windows, use Device Manager to check), but I preferred this non-IDE version, since it fully avoids the recompile step, and can easily lock in a chosen com port. Arduino CLI install instructions are [here](https://arduino.github.io/arduino-cli/0.33/installation/#download). – Venryx Jul 25 '23 at 09:57
5

I had the same problem on two Arduinos (one Uno, and one Modern Device Freeduino/USB Host board) and the window between reset and the beginning of serial port usage was so small that it was impossible to upload.

I finally fixed the problem by purchasing another Arduino Uno and building an ISP cable per these instructions, and using it to flash the Bare Bones app from the examples into each inaccessible board, using Arduino IDE version 0023, following these instructions to change preferences.txt. (Be sure to save the original file before editing it so you can replace it after you've rescued your Arduino.)

It took one quick upload to fix each board. Such a fast fix after so much grief. You might not want to purchase another Arduino, but consider these benefits:

  • You can overwrite the bootloader on your Arduino to gain more space.
  • Once the bootloader is overwritten, the board will boot faster.
  • Supposedly you can program raw AVRs for special projects, but I have not tried this: Google for ArduinoISP
  • It will quickly fix Arduinos that you block in the future.
  • You can now safely experiment to find ways to prevent serial port usage from locking up the device in the future.
dda
  • 6,030
  • 2
  • 25
  • 34
Sean Sheedy
  • 61
  • 1
  • 2
4

Arduino Leonardo board:

  1. Unplug the USB cable
  2. Connect the RX Pin to ground
  3. Plug in the USB cable
  4. Upload a new program
  5. Remove the USB cable
  6. Remove the RX grounding
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
PcTronics
  • 41
  • 1
3

Make sure you plug the Arduino directly into the computer and not through a hub. Using a hub will give you this error.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Kay
  • 61
  • 4
  • 2
    This was exactly my issue. I was using a USB hub built into my monitor. So at least some of the time this is the problem. – Dan Halbert Jan 27 '15 at 02:49
  • I think it depends. Sometimes it is OK with a USB hub. Perhaps the previous USB devices need to be cleaned up in [Device Manager](https://en.wikipedia.org/wiki/Device_Manager) (if on Windows)? [A menu item needs to be invoked to show those old devices](https://superuser.com/questions/408976/how-do-i-clean-up-com-ports-in-use/408981#408981). – Peter Mortensen Jan 05 '20 at 16:16
3

After scratching my head about this problem, here is a very simple solution that works anytime:

  • Unplug your USB cable
  • Go in Device Manager
  • Click on Ports (COM & LPT)
  • Right click on Arduino....(COMx)
  • Properties
  • Port Settings
  • Put Flow Control to HARDWARE
  • Create an empty sketch (Optional)
  • Connect the USB cable
  • Upload (Ctrl + U)

// Empty sketch to fix the upload problem
// Created by Eric Phenix
// Nov 2014

void setup()
{
}

// The loop routine runs over and over again forever:
void loop()
{
    delay(1000);
}

Et voila!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • For once I was very excited if that would work probably, but it didn't. Thanks for a different solution though. – sud007 Jan 31 '22 at 10:05
1

For Arduino Yún users, try uploading via Ethernet/Wi-Fi (menu ToolsPort).

I had exactly the same problem, and I tried pretty much everything (apart from burning a new bootloader). I am surprised it worked, but I've uploaded an empty sketch without any problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lokers
  • 2,106
  • 2
  • 18
  • 19
1

I just spent the last five hours searching for a solution to this problem (serial port COM3 already in use and grayed out serial port)...I tried everything every forum and Q&A site I could find suggested, including this one...

What finally fixed it (got rid of the last code I'd input that got stuck and uploaded simple blink function)?

Follow this link -- http://arduino.cc/en/guide/windows and follow the instructions for installing the drivers. My driver was "already up to date", but following these steps fixed the glitch. I am now a happy camper once again.

Note: Resetting the board manually with the button on the chip, or digitally through miscellaneous codes on the Internet did not work to fix this problem, because the signal was somehow blocked/confused between my Arduino Uno and the port in my laptop. Updating the drivers is like a reset for the "serial port already in use" problem.

At least so far...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sard
  • 11
  • 1
1

Here's what I did in Linux to be able to program my Arduino Micro which was stuck in a loop sending the 0 key when connected by USB;

# while true; do xinput float $(xinput --list | grep -i Arduino | awk '{print $7}' | cut -d'=' -f 2); done

Your output might be slightly different so just try running;

# watch xinput --list

then plug in the Arduino and see how the output is formatted.

This stopped X from accepting the keypresses and allowed the Arduino IDE to program finally!

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
james
  • 11
  • 1
1

I had this issue as well. I tried the above methods and none seemed to work, however something that did work (somehow, not sure if it was just a freak thing or it is actually a way to do it) was:

  1. Unplug USB from the Arduino
  2. Press and hold the reset button
  3. Plug in USB and power up
  4. Continue holding and upload the sketch. Once it's done uploading, release the reset button.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Keith
  • 11
  • 1
1

I had the very same problem today. Here is a simple solution we found to solve this issue (thanks to Anghiara):

Instead of loading your new code to the Arduino using the "upload button" (the circle with the green arrow) in your screen, use your mouse to click "Sketch" and then "Upload".

Please remember to add a delay() line to your code when working with Serial.println() and loops. I learned my lesson the hard way.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

If the port says Already used, then you are probably using the wrong port.

Switch it to the port where you connected your Arduino and then try to re-upload, you will definitely see it work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tgold brain
  • 399
  • 5
  • 13
1

Based on my experience with the communication already in use or blocked, I would say that the program you are interfacing with still has the communication open.

I also found that if you disconnect the USB cable it will rest the communication. It is not the greatest solution, but it solves the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jklmuk
  • 19
  • 1
0
  1. Unplug Tx from the microcontroller
  2. Insert USB cable
  3. Wait until full load
  4. Connect Rx with hardware
  5. Compile empty sketch in prosses and unconnect tx (don't worry about the error)
  6. Connect Rx with hardware
  7. Compile empty sketch
  8. Power off and connect Tx
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yury
  • 11
0

I got a similar problem.

If I power on my Arduino, there is a delay before the uploaded program execute.

So I use that delay for uploading new program, or empty program:

void setup(){}

void loop(){}

So my problem was solved.

Unplug any connection to Arduino pins before upload.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user599115
  • 49
  • 4
0

If nothing helped then you should arrange one more board and try to flash it through the Arduino as ISP option as shown in Arduino as ISP and Arduino Bootloaders or From Arduino to a Microcontroller on a Breadboard.

Instead of a boot loader, you can select your own programs to flash via ISP.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rijo Kuriakose
  • 164
  • 1
  • 11
0

Be sure you are not accessing the serial port from a terminal. That loop (or any code) should not prevent the Arduino from being programmed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chiva
  • 79
  • 1
  • 6
  • Try on an Arduino Uno and you will be able see that it does prevent the Arduino from being programmed. Yes, it's rather sad that it DOES prevent it from being programmed and a basic hello world fails because we don't put a delay(1000) in setup. I was skeptical, but now I'm stuck with a chip that needs to be cleared. – Pierre-Francoys Brousseau May 16 '14 at 19:03
0

If you still have the serial console open, it will reserve the serial port. Close it, and try again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
0

The only way it worked for me for arduino nano 33 iot is via pressing the reset button on the board continuously many time then press uoplad!

Mike
  • 551
  • 1
  • 6
  • 18
-1

Here is the best way that works out if you are trying to program through the USB cable:

  1. Ground the Tx signal on the board (connect digital I/O #1 to GND)
  2. Plug the USB cable
  3. Upload a new program
  4. Remove the USB cable
  5. Remove Tx grounding

You are all set!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Didn't work on my Arduino Uno. Note that grounding the TX signal will lead to the IDE saying it's not getting a response (obviously, you grounded TX!). – Pierre-Francoys Brousseau May 16 '14 at 19:01
  • Agree with @Pierre-FrancoysBrousseau program doesn't upload as a result, and hence didnt work for me either. – sud007 Jan 31 '22 at 10:16
-2

I also had your problem,and I solved the problem using the following steps (though you may already finish the problem, it just shares for anyone who visit this page):

  1. Unplug your Arduino
  2. Prepare an empty setup and empty loop program
  3. Write a comment symbol '//' at the end of program
  4. Set your keyboard pointer next to the '//'symbol
  5. Plug your Arduino into the computer, wait until the Arduino is completely bootloaded and it will output 'Hello, World!'
  6. You will see the 'Hello, World!' outputting script will be shown as comment, so you can click Upload safely.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
king
  • 1
-2
  1. First of all, connect your Arduino device.
  2. Then go to Device Manager and uninstall the driver.
  3. Now remove your Arduino device.
  4. Again connect you Arduino device to your system.
  5. Now install your driver (which you have uninstalled earlier).

It will free your port for sure.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lalit
  • 29
  • 1
  • 6