can't upload sketch after flashing bootloader - Atmega328PB - Page 1
Forum section: electronics / general

author: AndyNZ
author_rank: Contributor
timestamp: May 21, 2022, 02:38:42 am
content: Hi all,after resolving the issues I had uploading a boot loader to an ATMega328PB (https://www.eevblog.com/forum/microcontrollers/problem-flashing-bootloader-to-atmega328pb/), I am now struggling to upload a blink sketch (or any sketch) to it. When I try uploading using a FT232R clone from Aliexpresshttps://www.aliexpress.com/item/32843185453.html?_randl_currency=AUD&_randl_shipto=AU&src=google&src=google&albch=rmkt&acnt=576-373-4425&albcp=16560254345&albag=&slnk=&trgt=&plac=&crea=&netw=x&device=c&mtctp=&albbt=Google_7_rmkt&gclid=Cj0KCQjw-JyUBhCuARIsANUqQ_JGzFj-JiPLZtzcvFcDcqsn2aeTDf4tLwtusCzj_n0QDsU_Ub2v5_8aArabEALw_wcB&aff_fcid=8221bc3a71534bbb8f13cc2800416d00-1653099495457-07072-UneMJZVf&aff_fsk=UneMJZVf&aff_platform=aaf&sk=UneMJZVf&aff_trace_key=8221bc3a71534bbb8f13cc2800416d00-1653099495457-07072-UneMJZVf&terminal_id=6ca1f3c260424bf7b4ded7bedc3de2aa&afSmartRedirect=y,I get an out of synch message, when I try using the Uno R3 I get an "device signature error" as per message below.I've connected pin 10 of the Uno to the chip reset pin on the target board via 100n cap and 10k resistor to VCC, and I'm getting a few us pulse on the scope, so that seems to work OK.The error output seems to indicate a 28 kHz oscillator frequency, while I'm using a 16MHz resonator on the target board, so something looks wrong here already.Any ideas are much appreciated.Thanks,Andreas----------------------------------Arduino: 1.8.15 (Windows 10), Board: "ATmega328, Yes (UART0), EEPROM retained, 328PB, BOD 4.3V, LTO disabled, External 16 MHz"Sketch uses 1256 bytes (3%) of program storage space. Maximum is 32256 bytes.Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.C:\Users\1520004\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino18/bin/avrdude -CC:\Users\1520004\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.3/avrdude.conf -v -patmega328pb -carduino -PCOM8 -b115200 -D -Uflash:w:C:\Users\1520004\AppData\Local\Temp\arduino_build_81012/Blink.ino.hex:iavrdude: Version 6.3-20201216Copyright (c) 2000-2005 Brian Dean,http://www.bdmicro.com/Copyright (c) 2007-2014 Joerg WunschSystem wide configuration file is "C:\Users\1520004\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.3/avrdude.conf"Using Port : COM8Using Programmer : arduinoOverriding Baud Rate : 115200AVR Part : ATmega328PBChip Erase delay : 9000 usPAGEL : PD7BS2 : PC2RESET disposition : dedicatedRETRY pulse : SCKserial program mode : yesparallel program mode : yesTimeout : 200StabDelay : 100CmdexeDelay : 25SyncLoops : 32ByteDelay : 0PollIndex : 3PollValue : 0x53Memory Detail :Block Poll Page PolledMemory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xffflash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xfflfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00Programmer Type : ArduinoDescription : ArduinoHardware Version: 3Firmware Version: 4.4Vtarget : 0.3 VVaref : 0.3 VOscillator : 28.800 kHzSCK period : 3.3 usAn error occurred while uploading the sketchavrdude: AVR device initialized and ready to accept instructionsReading | ################################################## | 100% 0.00savrdude: Device signature = 0x1e950f (probably m328p)avrdude: Expected signature for ATmega328PB is 1E 95 16Double check chip, or use -F to override this check.avrdude done. Thank you.https://downloads.arduino.cc/libraries/library_index.json.sigfile signature verification failed. File ignored.This report would have more information with"Show verbose output during compilation"option enabled in File -> Preferences.

author: JDubU
author_rank: Frequent Contributor
timestamp: May 21, 2022, 03:58:27 am
content: Fuse bits?https://circuitdigest.com/microcontroller-projects/understanding-fuse-bits-in-atmega328p-to-enhance-arduino-programming-skills#:~:text=In%20the%20ATmega328P%20IC%2C%20there,the%20%E2%80%9CLow%20Fuse%20Byte%E2%80%9D.

author: Psi
author_rank: Super Contributor
timestamp: May 21, 2022, 04:11:48 am
content: You appear to be instructing avrdude to flash a Atmega328PBbut avrdude is detecting that your chip is actually an Atmega328P (no "B" on end, which is slightly different chip)Are you sure your chip is Atmega328PB and not Atmega328P.Even if the chip does say Atmega328PB on it, If you got it from a non legit source like aliexpress/ebay it maybe recycled and re-stamped with the wrong part number on it.Try changing your IDE/code/avrdude command to be for a Atmega328P chiphttps://www.pololu.com/file/0J1464/Atmel-42559-Differences-between-ATmega328P-and-ATmega328PB_ApplicationNote_AT15007.pdfQuote

author: sleemanj
author_rank: Super Contributor
timestamp: May 21, 2022, 05:24:20 am
content: Quote from: Psi on May 21, 2022, 04:11:48 amHazard a guess that avrdude is talking to his Uno, not the DUT.

author: sleemanj
author_rank: Super Contributor
timestamp: May 21, 2022, 05:28:55 am
content: It is unclear exactly what you have connected where or what you are trying to achieve.Once you have flashed the bootloader your target no longer needs to be connected to your programmer (Uno in your case), indeed remove the programmer from the equation entirely.Your USB-UART chip (FTDI in your case, I prefer CP2012) should be connected thusly...* RX -> TX* TX -> RX* DTR -> 100nF Cap (Series) -> RESET* Vcc and Gnd as appropriateAgain, remove your programmer so you don't confuse the situation.Once connected in the Arduino IDE Tools > Port choose the port your USB-Uart is on.Then hit Upload, to Upload your sketch, not Upload Using Programmer you are no longer using a Programmer, just regular old upload.

author: AndyNZ
author_rank: Contributor
timestamp: May 21, 2022, 06:48:57 am
content: Thanks for all replies.@JDubU: I used the following settings when flashing the boot loader:Board: MiniCore / Atmega328Clock: external 16MHzBOD: 2.7V and also tried 4.3VEEPROM: retainedCompiler LTO disabledVariant 328PBBootloader yes (UART0)Programmer Arduino as ISP (MiniCore)I wasn't sure if the resonator I selected is OK, but if I can upload the bootloader successfully it shows that it works, correct?@Psi: Yes, I'm sure it's a 328PB, as I ordered it from the JLCPCB assembly service. And I managed to upload the boot loader successfully with the PB settings. But just in case I also tried to flash the 328P boot loader, which was not successful.@sleemanj: Yes, I think you're right, that chip ID would match the Uno R3 I think. Which brings me back to square one: when I tried with the FT232R FTDI first, I got the "out of synch" message, so I gather it can't talk to the 328PB. I hooked it up initially without programmer and just with the FTDI board exactly like you suggested. Also I initially omitted the peripherals at the DTR pin (100n cap and 10k resistor to VCC), as that was what I was used to from the Arduino Pro Mini, but looking at the signal it was just pulling the DTR pin to ground when trying to upload, so I included the peripherals which gave a few us pulse, I think that's correct?I'm unsure as to what board I select in the IDE, I tried the same settings that I used to flash the boot loader, i.e. the MiniCore Atmega328, variant 328PB and UART0, but since this didn't work I tried other options like Arduino AVR boards > Uno, Pro Mini and Nano, all of which gave the same out of synch error.I've got two prototype boards, both gave the same result, so I think a faulty chip is unlikely. I think I'm making a newbie mistake somewhere, but where?Again thanks for your input, it is much appreciated.Cheers,Andreas

author: sleemanj
author_rank: Super Contributor
timestamp: May 21, 2022, 07:25:40 am
content: You should not change the selection of board from between burning bootloader and uploading normally.If you burnt bootloader for minicore 328PB then you should upload as minicore 328PB too.Suggest you start again from square one, connect up programmer, burn bootloader, disconnect programmer, connect FTDI, upload sketch.

author: Ian.M
author_rank: Super Contributor
timestamp: May 21, 2022, 08:16:29 am
content: The first thing to do, before revisiting the bootloader, is to check the target can run an Arduino sketch loaded using a programmer. The customary 'Is it alive?' sketch to use is Examples>01.Basics>Blink, which flashes a LED, at 0.5Hz with 50% duty cycle. If your board definition doesn't have LED_BUILTIN, change the three places it occurs in the sketch to a pin number for a pin you have connected to a LED (+ usual series resistor). Compile, and Upload Using Programmer - if the LED doesn't blink and there were no errors during the upload, you've got a hardware problem. If it blinks, time 30 flashes and if its *NOT* one minute, you've got an oscillator frequency problem.Once Blink has run successfully, you need to exercise the AVR UART (Arduino serial interface) to check its working properly with your USB <=> UART adapter. The easiest way to do that is to program the target with Examples>04.Communication>SerialEvent, which accepts text you type in a terminal program character by character then echos the whole line back to you when you press Enter (or more accurately when your terminal program sends a newline, ASCII(10) after you press enter). It does that at 9600 baud, so configure your terminal accordingly. If it doesn't work, check for connection problems and scope the AVR RX and TX pins to see what's going on. Also try disconnecting the target and connecting RX to TX on your USB UART, to check that's OK, which should echo each character as soon as you send it.N.B. the Arduino built-in terminal buffers what you type and only sends it when you click the button. Select 'Newline' at the bottom, next to the baud rate, for it to work with this sketch.As the bootloader typically uses baud rates up to 115 Kbaud, change the baud rate in line 26 to:Code:Serial.begin(115200);and reprogram the target with the edited sketch, change your terminal program baud rate to 115200 and check it still works.Assuming the above were all successful, carry on with Sleemanj's suggestions above (Reply#6).

author: AndyNZ
author_rank: Contributor
timestamp: May 21, 2022, 09:10:52 am
content: QuoteThat was what I've tried first, only when this was not successful I tried the other things in desperation.I've also tried a new FTDI board, same result.@Ian.M: I've just tried the blink sketch using the programmer (Uno R3) connected to the ISP header, nothing on the UART header. I left the settings same as when flashing the bootloader, not sure if I need to change "bootloader UART0" to "no bootloader". I get a device signature error, as it's recognizing the Uno R3:avrdude: Device signature = 0x1e950f (probably m328p)avrdude: Expected signature for ATmega328PB is 1E 95 16Double check chip, or use -F to override this check.Not sure if that's what you suggested I try. So far I haven't been able to successfully upload any sketch, so the problems seem to be communication or settings related.

author: AndyNZ
author_rank: Contributor
timestamp: May 21, 2022, 09:48:47 am
content: Result! I managed to upload a sketch by burning the bootloader using the internal 8MHz clock, then using the FTDI to upload the sketch.So I guess the 328PB doesn't like the resonator I chose, I remember reading something about the requirements for the external oscillator, which I didn't understand so I went with a part that has been used for 328P before.I've read about the internal clock being less accurate than an external resonator or crystal, but my application is not really time critical, so I'll try with the internal and see how I go.

author: Kapple
author_rank: Newbie
timestamp: March 05, 2024, 09:49:18 pm
content: Hey Andy!I hope your circuit is running absolutely fine.I have also run into the same problem. I have used an atmega328pb microcontroller and was able to bootload it while using an external 16 MHz oscillator.On my board, I have a USB receptable and a FTDI232RL chip for serial programming. But I am unable to upload programmes with it. Can you please help me out?

author: kevin.gibbs
author_rank: Regular Contributor
timestamp: April 08, 2024, 01:46:35 pm
content: You may have a low power supply, so it didn't work at 16mHz, or maybe you programmed the fuse incorrectly. At high frequencies, a 4.5V supply is desirable. The datasheet includes a graph of the relationship between supply voltage and operating frequency.Also, if you program the BOD incorrectly, it will prevent the microcontroller from starting at low voltage.

author: aliarifat794
author_rank: Regular Contributor
timestamp: April 11, 2024, 03:27:23 pm
content: If you're using the FT232R clone for uploading, try using a different programmer or upload method. You can also try using the Arduino Uno as an ISP programmer.