Arduino Nano Upload Error

Hi, I plugged in my nano and wrote this code for master slave system:

#include <Wire.h>

// Define Slave I2C Address
#define SLAVE_ADDR 9

// Define Slave answer size
#define ANSWERSIZE 5

// Define string with response to Master
String answer = "Hello";

void setup() {

  // Initialize I2C communications as Slave
  Wire.begin(SLAVE_ADDR);
  
  // Function to run when data requested from master
  Wire.onRequest(requestEvent); 
  
  // Function to run when data received from master
  Wire.onReceive(receiveEvent);
  
  // Setup Serial Monitor 
  Serial.begin(9600);
  Serial.println("I2C Slave Demonstration");
}

void receiveEvent() {

  // Read while data received
  while (0 < Wire.available()) {
    byte x = Wire.read();
  }
  
  // Print to Serial Monitor
  Serial.println("Receive event");
}

void requestEvent() {

  // Setup byte variable in the correct size
  byte response[ANSWERSIZE];
  
  // Format answer as array
  for (byte i=0;i<ANSWERSIZE;i++) {
    response[i] = (byte)answer.charAt(i);
  }
  
  // Send response back to Master
  Wire.write(response,sizeof(response));
  
  // Print to Serial Monitor
  Serial.println("Request event");
}

void loop() {

  // Time delay in loop
  delay(50);
}

And it gives me this error when I upload it:

Sketch uses 4700 bytes (15%) of program storage space. Maximum is 30720 bytes.
Global variables use 448 bytes (21%) of dynamic memory, leaving 1600 bytes for local variables. Maximum is 2048 bytes.
avrdude: ser_open(): can't set com-state for "\.\COM5"
Failed uploading: uploading error: exit status 1

Does anyone know what is wrong?
Thanks.

is it your first sketch or earlier it has uploaded?

It is my first sketch. It has a pre uploaded blink sketch, but that is all...

Search results on the forum for your problem and possible solution; you'll have to dig a bit.

https://forum.arduino.cc/search?expanded=true&q=com-state%20%40ptillisch

You have a clone Nano with a CH340 TTL-to-USB converter; and that CH340 might be fake. Is there any marking on the CH340 (on the bottom of the board)? If not, it's fake.

I would actually suggest that you return your Nano; where did you buy it?

Install the last version of CH340 driver

Set "Old bootloader" in Processor menu option

well, if on plug in the board you see some COM port adding in device manager then driver is installer and problem may be an old bootloader uploaded by producer;
if no COM port appear then you should install driver for this particular board. we suppose it is CH340 UART-USB converter.

The driver is installed. The problem is that the latest version of the driver is incompatible with the "CH340" chip on @n00b_1's "Nano" board.

The error produced by a mismatch between the Tools > Processor menu selection and the version of the bootloader on the board is different than the one @n00b_1 posted. It looks something like this:

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xd3

It might be that after fixing the problem with the driver, @n00b_1 will then encounter this unrelated error since it is indeed common to find that the cheap Nano derivative boards from the Chinese sellers ship with the pre-2018 "old bootloader".

or @n00b_1 interfere upload by using com port like: open serial plotter/monitor, open terminal software, Ultimaker Cura is active, or just connect something to arduino pins D0/D1.

Those all produce different error messages than the one @n00b_1 posted.

So select the appropriate board "ATmega328P with old bootloader" as the processor under the Tools menu.

That will never solve the error message @n00b_1 posted.

sterretje already pointed @n00b_1 in the right direction in post #4. In case that advice was not explicit enough, I'll post the instructions directly here in the topic:

Hi @n00b_1

There were some previous reports of this error occurring specifically when using boards that have the CH340 USB chip. It seems that a driver upgrade included in a recent Windows update is the cause of this problem.

The affected users have found that uninstalling reinstalling the CH340 device fixed the problem. I'll share the instructions you can follow to do that:

  1. Close Arduino IDE if it is running.
  2. Connect the Arduino board to your computer with a USB cable.
  3. Open the Windows Device Manager.
  4. Select View > Devices by type from the Device Manager menus.
  5. Open the "View" menu.
  6. If there is a to the left of the "Show hidden devices" menu item, click on "Show hidden devices" to disable it.
  7. Open the "Ports (COM & LPT)" section of the Device Manager tree.
  8. You should see a port identified as "USB-SERIAL CH340 (COM_n_)" under the "Ports (COM & LPT)" section (where "COM_n_" is some serial port e.g., COM4). Right click on that item.
    A context menu will open.
  9. Select "Uninstall device" from the context menu.
    An "Uninstall Device" dialog will open.
  10. Check the box next to "☐ Attempt to remove the driver for this device" in the "Uninstall Device" dialog.
  11. Click the "Uninstall" button.
  12. Wait for the uninstallation process to finish.
  13. Unplug the USB cable of the Arduino board from your computer.
  14. Wait for the Device Manager tree to reload.
  15. Connect the USB cable of the Arduino board to your computer.
  16. Wait for the "USB-SERIAL CH340 (COM_n_)" port to reappear under the "Ports (COM & LPT)" section of the Device Manager tree.

My experience is that the device will be reinstalled automatically by Windows. However, it is maybe possible that the complete driver removal will actually have been accomplished by the above instructions, in which case it will be necessary to reinstall the driver. If this happens to you, please let me know and I'll provide instructions for installing the driver.

Once the port has reappeared in Device Manager, start Arduino IDE again and check to see whether the problem still occurs.


Please, let's allow @n00b_1 to follow those instructions and report back with the results. If that procedure doesn't fix the problem, I have an alternative workaround that has solved the problem for everyone else who experienced this distinctive "can't set com-state" error.

I followed your instructions and here is what happened.

  1. I deleted the device
  2. the device tree refreshed
  3. A second tree opened
  4. it had a warning triangle on something that said "2.0 Usb-ser" or something
  5. 5 seconds later it goes bye bye
  6. USB-Serial ch340 (com6) pops up

So i guess it reinstalled like you said but i tried to upload a simple print hello in serial monitor sketch, and it failed with same error.

I am going to try that again, but can you post the work around?

Alright same error again...
Here it is.

Sketch uses 1480 bytes (4%) of program storage space. Maximum is 30720 bytes.
Global variables use 194 bytes (9%) of dynamic memory, leaving 1854 bytes for local variables. Maximum is 2048 bytes.
avrdude: ser_open(): can't set com-state for "\.\COM6"
Failed uploading: uploading error: exit status 1

Can you please post the work around?
thx.

I'm sorry to hear that. Some of the users who experienced these errors reported that doing a "roll back" to an older version of the driver was an effective workaround. You can give that a try.

I'll provide instructions:

  1. Open this tutorial from SparkFun in your browser:
    https://learn.sparkfun.com/tutorials/how-to-install-ch340-drivers/all#drivers-if-you-need-them
    We are using this tutorial as a source for an older version of the CH340 drivers. Generally I recommend only obtaining drivers from the manufacturer of the device, but WCH does not provide older versions of their drivers and SparkFun is trustworthy.
  2. Click the "Windows (EXE)" link you see in red under the "Drivers (If You Need Them)" section of the tutorial.
  3. Wait for the download to finish.
  4. Run the downloaded CH341SER.EXE file.
  5. A "User Account Control" dialog may now appear asking "Do you want to allow this app to make changes to your device?". Click the "Yes" button.
  6. A "DriverSetup(X64)" dialog will appear. Click the "INSTALL" button in the dialog.
  7. Wait for the driver installation to finish, as indicated by the appearance of a "Driver install success!" dialog.
  8. Click the "OK" button in the "Driver install success!" dialog.
  9. Click the X icon at the top right of the "DriverSetup(X64)" dialog to close it.
  10. Close Arduino IDE if it is running.
  11. Connect the Arduino board to your computer with a USB cable.
  12. Open the Windows Device Manager.
  13. Select View > Devices by type from the Device Manager menus.
  14. Open the "View" menu.
  15. If there is a to the left of the "Show hidden devices" menu item, click on "Show hidden devices" to disable it.
  16. Open the "Ports (COM & LPT)" section of the Device Manager tree.
  17. You should see a port identified as "USB-SERIAL CH340 (COM_n_)" under the "Ports (COM & LPT)" section (where "COM_n_" is some serial port e.g., COM4). Right click on that item.
    A context menu will open.
  18. Select the "Drivers" tab of the "USB-SERIAL CH340 (COM_n_) Properties" dialog.
  19. You will see a "Roll Back Driver" button in the dialog. If it is not clickable, perform the following instructions:
    1. Select "Update driver" from the context menu.
      An "Update Drivers Device - USB-SERIAL CH340 (COM_n_)" dialog will open.
    2. Click on "Search automatically for drivers" in the "Update Drivers Device - USB-SERIAL CH340 (COM_n_)" dialog.
    3. You should now see the driver installation wizard update the driver. Wait for the update to finish, as indicated by the message "Windows has successfully updated your drivers" in the dialog.
      :exclamation: It is possible you will instead see the message "The best drivers are already installed". If so, please stop following the instructions here and reply on the forum thread to let me know. I'll provide alternative instructions you can follow.
    4. Click the "Close" button in the "Update Drivers Device - USB-SERIAL CH340 (COM_n_)" dialog.
  20. Click the "Roll Back Driver" button in the "USB-SERIAL CH340 (COM_n_) Properties" dialog.
    The "Driver Package rollback" dialog will open.
  21. Click the radio button next to "My apps don't work with this driver" in the "Driver Package rollback" dialog .
  22. Click the "Yes" button.
  23. Click the "Close" button in the "USB-SERIAL CH340 (COM_n_) Properties" dialog.

Now start Arduino IDE again and repeat whatever you were doing before when you encountered that error. Please reply here to let us know whether the error still occurs.

Will this affect other arduino I have? for example I have a uno that works perfectly. Does it use the same driver?

It is not expected to have any negative effect on other Arduino boards. If it did, you could always install the latest version of the driver once more.

Only if the "uno" has a CH340 USB chip. The real Arduino Uno and faithful clones use the ATmega16U2 USB chip, which has a completely different driver. But the cheap "Uno" derivative boards do use the CH340.

couldn't rollback, using the update method it said that the best version is already installed.

There is no marking. It is the one close to the usb port right? The real CH340 have some WCH logo...

This is the CH340 on one of my clone Nanos

Best solution:
Buy a real Arduino Nano which uses the FTDI chip.

Alternative:
You might give the link in Unable to Upload to Arduino Nano - #14 by transistor_man a shot. You do this at your own risk and no guarantee that it works.

Please follow these instructions and then reply here on the forum thread to let us know what you found:

  1. Open the Windows Device Manager.
  2. Select View > Devices by type from the Device Manager menus.
  3. Open the "View" menu.
  4. If there is a to the left of the "Show hidden devices" menu item, click on "Show hidden devices" to disable it.
  5. Open the "Ports (COM & LPT)" section of the Device Manager tree.
  6. You should see a port identified as "USB-SERIAL CH340 (COM_n_)" under the "Ports (COM & LPT)" section (where "COM_n_" is some serial port e.g., COM4). Right click on that item.
    A context menu will open.
  7. Select the "Drivers" tab of the "USB-SERIAL CH340 (COM_n_) Properties" dialog.

Do you see "3.4.2014.8" after the "Driver Version" in that "USB-SERIAL CH340 (COM_n_) Properties" dialog?