I have just realised that when the TX and RX pins are connected to a peripheral, such as a bluetooth module, arduino IDE cannot upload sketches, due to communication problem and gives the following error message;
avrdude: stk500_getsync(): not in sync: resp=0x00
When the TX and RX pins are disconnected, everything works as it should be.
The problem is, I had totally forget this problem and already designed and fabricated my PCB, which connects a bluetooth module to a 328p AVR. When I solder everything most probably I will have problems with burning the bootloader to brand new AVR and uploading sketches.
When you program normally over usb, it uses the tx and rx pins to do so (via a usb to serial converter on the board), so you need to disconnect anything else that might interfere on those pins
zoom:
Yes, I will be using ICSP pins to both burn the bootloader and upload the sketch. So, ICSP pins have nothing to do with TX and RX pins.
Why arduino IDE cannot upload the sketches through USB when the TX and RX pins are occupied?
The following MCUs have no USB functions. Their bootloaders rely on serial communication to receive new code from a separate USB serial chip:
ATMEGA328,2560,1284
Some MCUs have USB functions so their TX and RX are NOT used for receiving new code:
ATMEGA32U4 etc.
You just use this experience as a lesson learned. Should have been more careful when prototyping.