user4396: I would like to use an Atmega328p micro-controller with a 4 Mhz oscillator in order get some low power consumptions. I am using an Arduino board to flash the bootloader into the Atmega.
EDIT
In order to compile the optiboot bootloader I had to install the avr-libc package and clone the repo 
sudo apt-get install avr-libc
git clone https://github.com/Optiboot/optiboot.git

and edited the Makefile in /optiboot/bootloaders/optiboot/Makefile by adding a new entry called atmega328_4
HELPTEXT += "target atmega328     - ATmega328p 4Mhz\n"
atmega328_4: TARGET = atmega328
atmega328_4: MCU_TARGET = atmega328p
atmega328_4: CFLAGS += $(COMMON_OPTIONS)
atmega328_4: AVR_FREQ ?= 4000000L
ifndef BIGBOOT
atmega328_4: LDSECTIONS  = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe
else
# bigboot version is 1k long; starts earlier
atmega328_4: LDSECTIONS  = -Wl,--section-start=.text=0x7c00 -Wl,--section-start=.version=0x7ffe
endif
atmega328_4: $(PROGRAM)_atmega328_4.hex
atmega328_4: $(PROGRAM)_atmega328_4.lst

atmega328_4_isp: atmega328
atmega328_4_isp: TARGET = atmega328
atmega328_4_isp: MCU_TARGET = atmega328p
ifndef BIGBOOT
# 512 byte boot, SPIEN
atmega328_4_isp: HFUSE ?= D9
else
# 1k byte boot, SPIEN
atmega328_4_isp: HFUSE ?= D9
endif
# Low power xtal 46MHz) 16KCK/14CK+65ms
atmega328_4_isp: LFUSE ?= 7D
# 2.7V brownout
atmega328_4_isp: EFUSE ?= FE
atmega328_4_isp: isp

I saved the file, run the make command and got the following error:
userk@norepinephrine:~/development/git/optiboot/optiboot/bootloaders/optiboot$ make atmega328_4
avr-gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

BAUD RATE CHECK: Desired: 115200, Real: 125000, UBRRL = 3, Difference=8.5%
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=4000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3           -c -o optiboot.o optiboot.c
optiboot.c:348:6: error: #error BAUD_RATE off by greater than 5%
     #error BAUD_RATE off by greater than 5%
      ^
optiboot.c:359:2: error: #error Unachievable baud rate (too fast) BAUD_RATE
 #error Unachievable baud rate (too fast) BAUD_RATE 
  ^
<builtin>: recipe for target 'optiboot.o' failed
make: *** [optiboot.o] Error 1

So I changed the -DBAUD_RATE parameter to 19200 and rerun the make command
BAUD_RATE_CMD = -DBAUD_RATE=19200

Then 2 new files were created: optiboot_atmega328_4.elf and optiboot_atmega328_4.hex
Then I copied these files into the arduino configuration folder located
 at: /home/userk/.arduino15/packages/arduino/hardware/avr/1.6.21/bootloaders/optiboot 
TIDE
I am using a custom board configuration file board.txt
##############################################################

nz.name=Arduino Noize

nz.vid.0=0x2341
nz.pid.0=0x0043
nz.vid.1=0x2341
nz.pid.1=0x0001
nz.vid.2=0x2A03
nz.pid.2=0x0043
nz.vid.3=0x2341
nz.pid.3=0x0243

nz.upload.tool=avrdude
nz.upload.protocol=arduino
nz.upload.maximum_size=32256
nz.upload.maximum_data_size=2048
nz.upload.speed=19200

nz.bootloader.tool=avrdude
nz.bootloader.low_fuses=0x7D
nz.bootloader.high_fuses=0xD9
nz.bootloader.extended_fuses=0xFE
nz.bootloader.unlock_bits=0x3F
nz.bootloader.lock_bits=0x0F
nz.bootloader.file=optiboot/optiboot_atmega328_4.hex

nz.build.mcu=atmega328p
nz.build.f_cpu=4000000L
nz.build.board=AVR_UNO
nz.build.core=arduino
nz.build.variant=standard

Flashing the Bootloader
I have followed these steps:

Uploaded the ArduinoISP sketch onto the Arduino UNO board.
Wired up the Arduino board and microcontroller as shown in the diagram below



Select "Arduino Noize" from the Tools > Board menu.
Select "Arduino as ISP" from Tools > Programmer
Run Tools > Burn Bootloader 

At this point I got a success message from IDE. So the bootloader has been correctly flashed into the Atmega328p
The Problem
When I try to upload a sample sketch (blink) using the wiring below I get a timeout from the IDE.

I have double checked the connections but the sketch doesn't upload. 
Do you have any advice?

user42539: nz.bootloader.low_fuses=0x7D
nz.bootloader.high_fuses=0xD9
nz.bootloader.extended_fuses=0xFE

Extended fuse byte. bits 2,1,0 determine the brown out level (bits 7-3 not used)
110 = 1.7 to 2.0V, 1.8 typical
Low fuse byte bits 3,2,1 determine the clock source
110 = low power crystal oscillator in 3-8 MHz range.
So those settings look okay.

user4143: As Gerben notes in the comments, the bootloader has to know how fast the board is running. It uses this to work out the baud rate settings for the serial communication.
If you haven't compiled the bootloader yourself with the right clock settings you will probably find that it is operating at a quarter of the speed you expect it to. Dividing your upload baud rate by four in your board settings may make it work, but you really should compile the bootloader to match the board properly.

user37857: As mentioned before, you need to compile the Optiboot bootloader with the correct clock frequency. Here's a guide that shows you how to do that, as well as adding an option to the Arduino IDE menu to select the right clock speed without having to add your own board definition.
Optiboot - ATmega328P at custom frequency

4396: <p>I would like to use an <strong>Atmega328p</strong> micro-controller with a 4 Mhz oscillator in order get some <strong>low power consumptions</strong>. I am using an Arduino board to <strong>flash the bootloader</strong> into the Atmega.</p>

<p><strong>EDIT</strong></p>

<p>In order to compile the optiboot bootloader I had to install the avr-libc package and clone the repo </p>

<pre><code>sudo apt-get install avr-libc
git clone https://github.com/Optiboot/optiboot.git
</code></pre>

<p>and edited the Makefile in <code>/optiboot/bootloaders/optiboot/Makefile</code> by adding a new entry called atmega328_4</p>

<pre><code>HELPTEXT += "target atmega328     - ATmega328p 4Mhz\n"
atmega328_4: TARGET = atmega328
atmega328_4: MCU_TARGET = atmega328p
atmega328_4: CFLAGS += $(COMMON_OPTIONS)
atmega328_4: AVR_FREQ ?= 4000000L
ifndef BIGBOOT
atmega328_4: LDSECTIONS  = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe
else
# bigboot version is 1k long; starts earlier
atmega328_4: LDSECTIONS  = -Wl,--section-start=.text=0x7c00 -Wl,--section-start=.version=0x7ffe
endif
atmega328_4: $(PROGRAM)_atmega328_4.hex
atmega328_4: $(PROGRAM)_atmega328_4.lst

atmega328_4_isp: atmega328
atmega328_4_isp: TARGET = atmega328
atmega328_4_isp: MCU_TARGET = atmega328p
ifndef BIGBOOT
# 512 byte boot, SPIEN
atmega328_4_isp: HFUSE ?= D9
else
# 1k byte boot, SPIEN
atmega328_4_isp: HFUSE ?= D9
endif
# Low power xtal 46MHz) 16KCK/14CK+65ms
atmega328_4_isp: LFUSE ?= 7D
# 2.7V brownout
atmega328_4_isp: EFUSE ?= FE
atmega328_4_isp: isp
</code></pre>

<p>I saved the file, run the make command and got the following error:</p>

<pre><code>userk@norepinephrine:~/development/git/optiboot/optiboot/bootloaders/optiboot$ make atmega328_4
avr-gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

BAUD RATE CHECK: Desired: 115200, Real: 125000, UBRRL = 3, Difference=8.5%
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=4000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3           -c -o optiboot.o optiboot.c
optiboot.c:348:6: error: #error BAUD_RATE off by greater than 5%
     #error BAUD_RATE off by greater than 5%
      ^
optiboot.c:359:2: error: #error Unachievable baud rate (too fast) BAUD_RATE
 #error Unachievable baud rate (too fast) BAUD_RATE 
  ^
&lt;builtin&gt;: recipe for target 'optiboot.o' failed
make: *** [optiboot.o] Error 1
</code></pre>

<p>So I changed the -DBAUD_RATE parameter to 19200 and rerun the make command</p>

<pre><code>BAUD_RATE_CMD = -DBAUD_RATE=19200
</code></pre>

<p>Then 2 new files were created: optiboot_atmega328_4.elf and optiboot_atmega328_4.hex</p>

<p>Then I copied these files into the arduino configuration folder located
 at: <code>/home/userk/.arduino15/packages/arduino/hardware/avr/1.6.21/bootloaders/optiboot</code> </p>

<p><strong>TIDE</strong></p>

<p>I am using a <strong>custom</strong> board configuration file board.txt</p>

<pre><code>##############################################################

nz.name=Arduino Noize

nz.vid.0=0x2341
nz.pid.0=0x0043
nz.vid.1=0x2341
nz.pid.1=0x0001
nz.vid.2=0x2A03
nz.pid.2=0x0043
nz.vid.3=0x2341
nz.pid.3=0x0243

nz.upload.tool=avrdude
nz.upload.protocol=arduino
nz.upload.maximum_size=32256
nz.upload.maximum_data_size=2048
nz.upload.speed=19200

nz.bootloader.tool=avrdude
nz.bootloader.low_fuses=0x7D
nz.bootloader.high_fuses=0xD9
nz.bootloader.extended_fuses=0xFE
nz.bootloader.unlock_bits=0x3F
nz.bootloader.lock_bits=0x0F
nz.bootloader.file=optiboot/optiboot_atmega328_4.hex

nz.build.mcu=atmega328p
nz.build.f_cpu=4000000L
nz.build.board=AVR_UNO
nz.build.core=arduino
nz.build.variant=standard
</code></pre>

<p><strong>Flashing the Bootloader</strong>
I have followed these steps:</p>

<ul>
<li><p>Uploaded the ArduinoISP sketch onto the Arduino UNO board.</p></li>
<li><p>Wired up the Arduino board and microcontroller as shown in the diagram below</p></li>
</ul>

<p><a href="https://i.stack.imgur.com/5zCLC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5zCLC.png" alt="enter image description here"></a></p>

<ul>
<li><p>Select "Arduino Noize" from the Tools > Board menu.</p></li>
<li><p>Select "Arduino as ISP" from Tools > Programmer
Run Tools > Burn Bootloader </p></li>
</ul>

<p>At this point I got a success message from IDE. So the bootloader has been correctly flashed into the Atmega328p</p>

<p><strong>The Problem</strong></p>

<p>When I try to upload a sample sketch (blink) using the wiring below I get a <strong>timeout</strong> from the IDE.</p>

<p><a href="https://i.stack.imgur.com/krPb7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/krPb7.png" alt="enter image description here"></a></p>

<p>I have double checked the connections but the sketch doesn't upload. 
Do you have any advice?</p>
 42539: <pre><code>nz.bootloader.low_fuses=0x7D
nz.bootloader.high_fuses=0xD9
nz.bootloader.extended_fuses=0xFE
</code></pre>

<p>Extended fuse byte. bits 2,1,0 determine the brown out level (bits 7-3 not used)
110 = 1.7 to 2.0V, 1.8 typical</p>

<p>Low fuse byte bits 3,2,1 determine the clock source
110 = low power crystal oscillator in 3-8 MHz range.</p>

<p>So those settings look okay.</p>
 4143: <p>As Gerben notes in the comments, the bootloader has to know how fast the board is running. It uses this to work out the baud rate settings for the serial communication.</p>

<p>If you haven't compiled the bootloader yourself with the right clock settings you will probably find that it is operating at a quarter of the speed you expect it to. Dividing your upload baud rate by four in your board settings may make it work, but you really should compile the bootloader to match the board properly.</p>
 37857: <p>As mentioned before, you need to compile the Optiboot bootloader with the correct clock frequency. Here's a guide that shows you how to do that, as well as adding an option to the Arduino IDE menu to select the right clock speed without having to add your own board definition.</p>

<p><a href="https://tttapa.github.io/Pages/Arduino/Bootloaders/ATmega328P-custom-frequency.html" rel="nofollow noreferrer">Optiboot - ATmega328P at custom frequency</a></p>
