An error occurred while uploading the sketch, I read the error message and didn't know why, please help

Hi, I am new to arduino, so my commented codes are a bit messy, please ignore the // comments.
My code is:
'''
int sensePin = PF1;
int sensePin1 = PF2;

// the analogue pin number

void setup(){
analogReference(DEFAULT);

Serial.begin(9600);
};

void loop(){
int sensorvalue = analogRead(sensePin);
int sensorvalue1= analogRead(sensePin1);
float voltage = sensorvalue * (5.0 / 1023.0);
float voltage1 = sensorvalue1 * (5.0/1023.0);
Serial.println(voltage);
Serial.print(",");
Serial.println(voltage1);
delay(500);
}
'''

The error message is (it is quite long, I turned on the 'show verbose output during compilation'):
'''
Arduino: 1.8.19 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\yis27\OneDrive\Documents\Arduino\libraries -fqbn=arduino:avr:mega:cpu=atmega2560 -vid-pid=2341_0042 -ide-version=10819 -build-path C:\Users\yis27\AppData\Local\Temp\arduino_build_624741 -warnings=none -build-cache C:\Users\yis27\AppData\Local\Temp\arduino_cache_773624 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose D:\emg sensor\AnalogInput\AnalogInput.ino

C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\yis27\OneDrive\Documents\Arduino\libraries -fqbn=arduino:avr:mega:cpu=atmega2560 -vid-pid=2341_0042 -ide-version=10819 -build-path C:\Users\yis27\AppData\Local\Temp\arduino_build_624741 -warnings=none -build-cache C:\Users\yis27\AppData\Local\Temp\arduino_cache_773624 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose D:\emg sensor\AnalogInput\AnalogInput.ino

Using board 'mega' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr

Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr

Detecting libraries used...

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega" "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741\sketch\AnalogInput.ino.cpp" -o nul

Generating function prototypes...

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega" "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741\sketch\AnalogInput.ino.cpp" -o "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741\preproc\ctags_target_for_gcc_minus_e.cpp"

"C:\Program Files (x86)\Arduino\tools-builder\ctags\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741\preproc\ctags_target_for_gcc_minus_e.cpp"

Compiling sketch...

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega" "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741\sketch\AnalogInput.ino.cpp" -o "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741\sketch\AnalogInput.ino.cpp.o"

Compiling libraries...

Compiling core...

Using precompiled core: C:\Users\yis27\AppData\Local\Temp\arduino_cache_773624\core\core_arduino_avr_mega_cpu_atmega2560_0c812875ac70eb4a9b385d8fb077f54c.a

Linking everything together...

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega2560 -o "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741/AnalogInput.ino.elf" "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741\sketch\AnalogInput.ino.cpp.o" "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741/..\arduino_cache_773624\core\core_arduino_avr_mega_cpu_atmega2560_0c812875ac70eb4a9b385d8fb077f54c.a" "-LC:\Users\yis27\AppData\Local\Temp\arduino_build_624741" -lm

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741/AnalogInput.ino.elf" "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741/AnalogInput.ino.eep"

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-objcopy" -O ihex -R .eeprom "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741/AnalogInput.ino.elf" "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741/AnalogInput.ino.hex"

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-size" -A "C:\Users\yis27\AppData\Local\Temp\arduino_build_624741/AnalogInput.ino.elf"

Sketch uses 3940 bytes (1%) of program storage space. Maximum is 253952 bytes.

Global variables use 202 bytes (2%) of dynamic memory, leaving 7990 bytes for local variables. Maximum is 8192 bytes.

An error occurred while uploading the sketch

'''

Thanks for the help in advance. Let me know if I chose the wrong category.

That means that the code compiled OK.

That indicates a communication problem between the PC and the Mega. You did have the right serial port selected in the Tools, Ports menu?

See the troubleshooting upload page.

That was the wrong one if you have an upload problem :wink: But the IDE stupidly says that you have to turn it on to see more :frowning: Please disable the above setting and enable verbose output during upload.

Some questions

  1. Were you able to upload in the past? Or is this the first time that you try?
  2. Is the board properly recognised in Windows Device manager under Ports?

Note: to post code, use back-ticks, not single quotes.

Is the correct port selected? Sometimes it fails on wrong port but doesn’t explicitly tell you that

On my side (Fedora-34 Linux OS), there is no port problem as it uses the correct one, but i got some error (upload on Arduino Nano v3.0):

Arduino : 1.8.13 (Linux), Carte : "Arduino Nano, ATmega328P"

Le croquis utilise 13270 octets (43%) de l'espace de stockage de programmes. Le maximum est de 30720 octets.
Les variables globales utilisent 1548 octets (75%) de mémoire dynamique, ce qui laisse 500 octets pour les variables locales. Le maximum est de 2048 octets.
La mémoire disponible faible, des problèmes de stabilité pourraient survenir.
/usr/bin/avrdude -C/etc/avrdude/avrdude.conf -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:/tmp/arduino_build_510852/Solar_tracker_jerome.ino.hex:i 

avrdude: Version 6.3, compiled on Jan 26 2021 at 00:00:00
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/etc/avrdude/avrdude.conf"
         User configuration file is "/home/jerome/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x41
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x72
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x64
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x75
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x69
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x6e
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x6f
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x41
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x4c
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x4c

avrdude done.  Thank you.

Une erreur est survenue lors du transfert du croquis

the board informations are: BN: unknown, VID: 1A86, PID: 7523

But i find the solution...
First, i bought this board in Asia (i'm in Thailand) at a Thailand shop (that come from China most probably).
And after that error, i tried to use the proc config as "AT Mega 328P with old bootloader". And this resolved the problem. I can also see that the baudrate is falling to 57600 b/s

Hope that could help someone.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.