I am using Atmega328p with arduino bootloader in a project. There happens to be an LED connected on SCK pin. During Start (or restarts), the LED appears to flicker a couple of times (3 times to be exact). I don't want this to happen. I can change the connections but I would prefer a software solution because hardware change means a PCB modification.

I probed the SCK pin and I got some expected waveforms. There were two different cases:

1) When I powered the arduino using USB to serial programmer i.e. Tx Rx connected to a PC:

with UART connected

2) When I powered the arduino with an external power source (the case with my project):

Without UART

This is what I think:

1) As mentioned in this thread, the bootloader has a piece of code which makes it act that way. The bootloader code has a pin definition file which declares SCK pin as a LED and blinks it a couple of times to indicate that it has entered the bootloader. I found the code here:

#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega88) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__)
/* Onboard LED is connected to pin PB5 in Arduino NG, Diecimila, and Duemilanove */ 
#define LED_DDR     DDRB
#define LED_PORT    PORTB
#define LED_PIN     PINB
#define LED         PINB5

Other relevant piece of code is here.

In this case, changing the LED definition to PINB4 and burning the bootloader and program again should take care of the issue. However this didn't work out for me. This is what I did exactly:

a) Opened the file D:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders\optiboot\pin_defs.h

b) Modified the PINB5 to PINB4 on line 6.

c) Saved the file, burnt the bootloader and then program.

d) Powered up the chip and tested whether I am getting those 3 pulses. I got those 3 pulses.

(Even though it didn't work, I am still going to try it a few more times just in case I might have made an error.) :(

2) This brings me to second thought that since atmega328 accepts code through SPI by default, it checks for incoming data by clocking the SCK pins 3 times during startup. In this case, a software fix won't do anything.

What do you guys suggest? Should I go for the PCB modification or is there still some hope left?

Ignacio pointed out that I can disable bootloader to get rid of the issue. I want to program the chip using arduino IDE (preferably via UART) but I can consider SPI as well in case everything else fails.

Note: I am not sure whether this post is more suited for EE or Arduino. In case you guys(mods) think I'd get better suggestions there, feel free to move my post to Arduino.

  • Can you explain in more detail how modifying the bootloader "didn't work out"?
    – Dave Tweed
    Commented May 5, 2016 at 12:15
  • I modified PINB5 to PINB4, burnt the bootloader, burnt the code and powered the circuit. I was still getting the same 3 pulses. Commented May 5, 2016 at 12:19
  • It might make more sense to set LED_START_FLASHES to 0. There is code in the file you linked to that does that as long as it hasn't been defined elsewhere.
    – Dave Tweed
    Commented May 5, 2016 at 12:26
  • I can try that Dave. I am more of a hardware guy rather than coder. I'll try what you suggested and update my question. Thanks Commented May 5, 2016 at 12:27
  • In that case, you need help from the people who understand the details of the Arduino software. Migrating.
    – Dave Tweed
    Commented May 5, 2016 at 12:31

2 Answers 2


I compiled Optiboot a while back using the shell script below. This is for Linux, you may need to adapt slightly for other operating systems.


$BIN_LOCATION/avr-gcc -mmcu=atmega328p \
    -D__AVR_ATmega328P__ \
    -DF_CPU=16000000L \
    -DBAUD_RATE=115200 \
    -Os \
    -gdwarf-2 \
    -fno-inline-small-functions \
    -fno-split-wide-types \
    -fno-tree-scev-cprop  \
    -fno-exceptions -ffunction-sections -fdata-sections \
    -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables  \
    -fno-keep-inline-functions -fno-common \
    -std=gnu99 \
    -Wl,--section-start=.text=7E00 \
    -Wl,--relax -nostartfiles \
    -Wl,--gc-sections \
    -ooptiboot.elf optiboot.c 

rm -f optiboot.hex

$BIN_LOCATION/avr-objcopy -O ihex --set-section-flags=.eeprom=alloc,load \
    --no-change-warnings \
    --change-section-lma \
    .eeprom=0 \
    -R .eeprom optiboot.elf optiboot.hex

$BIN_LOCATION/avr-objdump -j .sec1 -d -m avr5 optiboot.hex > optiboot.lst

You will need to find where your copy of avr-gcc and insert that into the first line above.

Notice I have changed the define for LED_START_FLASHES to be zero above. That should fix the issue.

To save you the trouble of doing that, I have made a "no blink" version for the Atmega328 and put it here:


Warning - the script above compiled the "no blink" version OK, but using the latest version of the Optiboot.c code, and the latest compiler, makes a .hex file that is four bytes too long, for reasons I can't work out. However it compiles fine if you omit the blinks, because there is less code there.

Note re the LED on D13. Because the LED is driven by an op-amp (on the Uno), it will flash even with the above code. However that is because the op-amp has a high-impedance input. If you use a pull-down resistor on D13 it won't flash.

Now that I look at the download, it is so short I may as well include it here:


Just save that as optiboot.hex.

  • Thanks a lot, Nick. I'll be using the hex file that you generated for me. I really appreciate your help and efforts. I'll give it a go and update you about the results. Have a nice day, Sir. Commented May 6, 2016 at 3:28
  • I downloaded the file and burnt the bootloader. The issue went away. Thanks a lot Nick. You helped more than I asked for. Commented May 7, 2016 at 10:02
  • Hey Nick, the bootloader seems to be causing some RESET issues. Can you check once. Sometimes I feel that I am having difficulty uploading code or resetting the chip by grounding the RESET pin. Can you check if you disabled the reset pin in the bootloader? Thanks Commented May 16, 2016 at 17:16
  • It isn't possible to disable the reset pin, whatever bootloader you use. You might do it with a fuse setting, but the bootloader itself can't do it.
    – Nick Gammon
    Commented May 17, 2016 at 5:58
  • I posted a new question here. Can you please take a look? Thanks Commented May 17, 2016 at 6:04

There is nothing in the default megaAVR initialization that causes SCK to behave in this manner; it is all caused by the bootloader.

Fortunately all you have to do to solve this is disable the bootloader. Unprogramming the BOOTRST fuse (ext:0) will cause execution to start at address 0 instead of the beginning of the bootloader on reset.

  • 1
    But then I won't be able to program the chip using Arduino IDE. I need that functionality. Any other suggestion? Commented May 5, 2016 at 12:14
  • Sure you will. You'll simply program it via ISP instead of serial. Commented May 5, 2016 at 12:14
  • Ohh that's what you meant. I can consider this option. However is there a way I can keep UART programming feature intact? Commented May 5, 2016 at 12:20
  • If I was going to then I'd just remove the whole LED blinking part. Which I wouldn't, because I'm far too used to ISP to put up with serial programming. Commented May 5, 2016 at 12:22
  • Dave suggested something in comments - seems equivalent to deleting the LED blinking part. I was just hoping that there is a code way to do which is there it seems after reading your answer and his comment. I'll give it a try and let you guys know. Thanks a lot for posting the answer. (+1) Commented May 5, 2016 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.