Can't upload sketch to ATMEGA328PB board

I built a custom pcb with atmega328pb and want to upload a simple blinking sketch to it.
I have burnt bootloader with 16mhz external crystal using a Arduino Uno. However, i have this problem when trying to upload the code with a USB to TTL (FDTI module).

Avrdude version 8.0-arduino.1
Copyright see 


System wide configuration file is C:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\tools\avrdude\8.0-arduino.1\etc\avrdude.conf

Using port            : COM8
Using programmer      : urclock
Setting baud rate     : 115200
Warning: attempt 1 of 10: not in sync
Warning: attempt 2 of 10: not in sync
Warning: attempt 3 of 10: not in sync
Warning: attempt 4 of 10: not in sync
Warning: attempt 5 of 10: not in sync
Warning: attempt 6 of 10: not in sync
Warning: attempt 7 of 10: not in sync
Warning: attempt 8 of 10: not in sync
Warning: attempt 9 of 10: not in sync
Warning: attempt 10 of 10: not in sync
Warning: programmer is not responding; try -x strict and/or vary -x delay=100
Error: unable to open port COM8 for programmer urclock

Avrdude done.  Thank you.
Failed uploading: uploading error: exit status 1

Have you installed FTDFI drivers?

yes i have, and i have been using the usb-to-ttl for other projects without problem

I don't know if it applies to a 'custom' board, but try the processor settings if they are available under tools.

Can you show us the schematic of your design? Perhaps it’s something as simple as the reset circuitry not working as it should.

Keep in mind the ATMega328PB has TWO uart’s. During burning bootloader you need to select the UART that you have connected TX/RX for programming.

On a PB you can also select UART1 for the bootloader.

This is roughly the part that’s relevant. I will post the full schematic tomorrow.

I used oscilloscope to check the DTR signal, and observed that it went LOW for only 160us, then went HIGH again. I also tried to manually pull RESET pin low while uploading, but it didn’t help

This is roughly the part that’s relevant. I will post the full schematic tomorrow.

I used oscilloscope to check the DTR signal, and observed that it went LOW for only 160us, then went HIGH again. I also tried to manually pull RESET pin low while uploading, but it didn’t help

also this is the output when burning bootloader

"C:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\tools\avrdude\8.0-arduino.1/bin/avrdude" "-CC:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\tools\avrdude\8.0-arduino.1/etc/avrdude.conf" -v -patmega328pb -cstk500v1 -PCOM12 -b19200 -e -Ulock:w:0xff:m -Uefuse:w:0b11110101:m -Uhfuse:w:0xd7:m -Ulfuse:w:0b11111111:m Avrdude version 8.0-arduino.1 Copyright see https://github.com/avrdudes/avrdude/blob/main/AUTHORS` ```` System wide configuration file is C:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\tools\avrdude\8.0-arduino.1\etc\avrdude.conf Using port : COM12 Using programmer : stk500v1 Setting baud rate : 19200 AVR part : ATmega328PB Programming modes : SPM, ISP, HVPP, debugWIRE Programmer type : STK500 Description : Atmel STK500 v1 HW Version : 2 FW Version : 1.18 Topcard : Unknown Vtarget : 0.0 V Varef : 0.0 V Oscillator : Off SCK period : 0.0 us XTAL frequency : 7.372800 MHz AVR device initialized and ready to accept instructions Device signature = 1E 95 16 (ATmega328PB) Erased chip Processing -U lock:w:0xff:m Reading 1 byte for lock from input file 0xff in 1 section [0, 0] Writing 1 byte (0xFF) to lock, 1 byte written, 1 verified Processing -U efuse:w:0b11110101:m Reading 1 byte for efuse from input file 0b11110101 in 1 section [0, 0] Writing 1 byte (0xF5) to efuse, 1 byte written, 1 verified Processing -U hfuse:w:0xd7:m Reading 1 byte for hfuse from input file 0xd7 in 1 section [0, 0] Writing 1 byte (0xD7) to hfuse, 1 byte written, 1 verified Processing -U lfuse:w:0b11111111:m Reading 1 byte for lfuse from input file 0b11111111 in 1 section [0, 0] Writing 1 byte (0xFF) to lfuse"C:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\tools\avrdude\8.0-arduino.1/bin/avrdude" "-CC:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\tools\avrdude\8.0-arduino.1/etc/avrdude.conf" -v -patmega328pb -cstk500v1 -PCOM12 -b19200 "-Uflash:w:C:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.1.1/bootloaders/urboot/atmega328pb/watchdog_1_s/autobaud/uart0_rxd0_txd1/led+b5/urboot_atmega328pb_pr_ee_ce.hex:i" -Ulock:w:0xff:m , 1 byte written, 1 verified Avrdude done. Thank you. Avrdude version 8.0-arduino.1 Copyright see https://github.com/avrdudes/avrdude/blob/main/AUTHORS System wide configuration file is C:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\tools\avrdude\8.0-arduino.1\etc\avrdude.conf Using port : COM12 Using programmer : stk500v1 Setting baud rate : 19200 AVR part : ATmega328PB Programming modes : SPM, ISP, HVPP, debugWIRE Programmer type : STK500 Description : Atmel STK500 v1 HW Version : 2 FW Version : 1.18 Topcard : Unknown Vtarget : 0.0 V Varef : 0.0 V Oscillator : Off SCK period : 0.0 us XTAL frequency : 7.372800 MHz AVR device initialized and ready to accept instructions Device signature = 1E 95 16 (ATmega328PB) Auto-erasing chip as flash memory needs programming (-U flash:w:...) specify the -D option to disable this feature Erased chip Processing -U flash:w:C:\Users\DELL\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.1.1/bootloaders/urboot/atmega328pb/watchdog_1_s/autobaud/uart0_rxd0_txd1/led+b5/urboot_atmega328pb_pr_ee_ce.hex:i Reading 384 bytes for flash from input file urboot_atmega328pb_pr_ee_ce.hex in 1 section [0x7e80, 0x7fff]: 3 pages and 0 pad bytes Writing 384 bytes to flash Writing | ################################################## | 100% 0.42s Reading | ################################################## | 100% 0.24s 384 bytes of flash verified Processing -U lock:w:0xff:m Reading 1 byte for lock from input file 0xff in 1 section [0, 0] Writing 1 byte (0xFF) to lock, 1 byte written, 1 verified Avrdude done. Thank you.`

No bypass caps. 0.1uF ceramic, one for each Vcc pin, close to the chip as possible.

This is roughly the part that’s relevant. I will post the full schematic tomorrow.

I used oscilloscope to check the DTR signal, and observed that it went LOW for only 160us, then went HIGH again. I also tried to manually pull RESET pin low while uploading, but it didn’t help

In the actual schematic there are bypass capacitors. Sorry i forgot to include it here


this is the full schematic

Here are my hints and tips. Since you are not using a standard (that I know of) FTDI Basic style pinout for the programming header or have mentioned your serial adapter and how you’ve wired it, you need to check that you are connecting the TX pin of the adapter to the RX pin of the MCU, connecting the RX pin of the adapter to the TX pin of the MCU. If that crossover is not right on your new board, it will not program. I advise try setting the oscillator of the MCU to internal and bootload it; if it then uploads via serial perhaps the external clock circuit you have on the board may not be working as intended. Troubleshoot serial by using the ICSP programmer to load a sketch onto the MCU that accepts and sends some text and communicate with the MCU successfully over serial. If that works, you would know the problem is isolated to the bootloader/avrdude relationship or DTR reset circuit or perhaps fuse settings.

I found the problem.

In my board, the UART0 is connected to the connector for communicating with USB-to-TTL adapter.

UART0 is also connected to a RS485 auto direction circuit because i want to later use Modbus. And the RS485 circuit somehow caused the problem. Below is the RS485 circuit i used, with the IC being SP485 (5V logic)

When i checked the TX, RX lines with oscilloscope, the logic level was lower than the expected 5V. Removing the SP485 solved the problem.

Is there anything wrong with this circuit

Well eh, i would say so yes. Since the signal from the Tx line (input to the circuit) is HIGH or LOW depending on the bit being sent (HIGH for a 0 and LOW for a 1), the SP485 will be changing from driver to receiver all the time, and depending on the state of the A & B pins will switch the RX line HIGH or LOW, which obviously interferes with any other transmission. Plus that it doesn't work as i suspect you intend it to work.

How do i separate them?

I’m not familiar with the PB variant of the 328, but if port pins PE2 or PE3 can be outputs - and since, according to your schematic, you are not using them - then use one of them to directly drive the RE & DE pins.

With your Modbus RS485, you probably have the preTransmission and postTransmission functions - depending on the library you are using. Add code to these 2 functions to control RE & DE accordingly.

You place some jumpers on the PCB or a switch for Running your code and Loading your code.

Did you breadboard your prototype before going to PCB?

Tom.... :smiley: :+1: :coffee: :australia:

Unfortunately not that part

Big mistake from me…

and instead of branching the TX, RX traces to rs485 circuitry and the USB-to-TTL connectors, i routed them to rs485 circuit first then to USB-to-TTL, so it would be ugly to cut traces and separate them on this version..

For ATmega328PB, on your custom board if you have made available another FTDI header for the second set of RX and TX pins for UART1, you can burn the bootloader on the MCU that listens for the sketch on RX1, TX1, and just upload there. For your next PCB order you could simply use RX1 and TX1 for your RS485 stuff. If you have made available the pins necessary for ICSP programming, you can upload using a programmer or an Arduino as ISP through those pins.