Programming Arduino with Arduino

Arduino is a great development tool. It’s got an IDE, the community supplies code for almost any device imaginable and everything is completely open source. While it is easy to develop a project around Arduino, sometimes you want to give your new creation a life of its own. Using the method explained here you can create a standalone Ardiono and load sketches to any compatible ATmega chip.

The target chip can be ATmega 8/168/328 etc. (List of compatible IC’s listed here). The target chip is not required to be preloaded with bootloader.
Uploading code to the chip requires a programmer. One common type of programmer is an ISP (In System Programmer). Its advantages are that it requires only few connection pins and is easy to operate. There are many available dedicated ISP’s (for example one from Atmel). But, a programmer is a little more than a microcontroller with a serial connection to the computer. This means that you can easily program chips using Arduino itself with no need of an extra programmer!

As we’ll see, the target chip (the chip we want to program), requires little supporting circuitry, and can run any code that runs on Arduino. This allows designing your circuit around the chip directly, as you would with Arduino and load it with your favourite sketch.

The Circuit

Target chip connected connections to ArduinoISP

Eagle .sch file can be downloaded here. The center of the circuit is the target chip. Notice that the pins are numbered like Arduino, rather than vanilla ATMega. You’ll need this eagle part.
This makes the inclusion of this circuit a very easy reference as it mirrors the Arduino board. Note that the ‘meaning’ of the pins will be valid only after you program the chip with either a bootloader or and Arduino applet.

ATMega Supporting Circuitry

The circuit supporting the target chip is as simple as it can be. The reset pin is chained to a resistor and a 16Mhz crystal (with two 22pf capacitors) provides clock. We’ll discuss alternative clock sources later.

SPI Programmer

We will program our new Arduino using an existing Arduino. This novel idea of bootstrapping was presented here. The software used for the programmer is MegaISP (Recently integrated into the Arduino IDE). MegaISP is an open source Arduino applet that allows it to act as an ISP, with normal programming tools, including Arduino IDE.

To connect the circuit to the Arduino, let’s look at the documentation of ArduinoISP:
// this sketch turns the Arduino into a AVRISP
// using the following pins:
// 10: slave reset
// 11: MOSI
// 12: MISO
// 13: SCK


// Put an LED (with resistor) on the following pins:
// 9: Heartbeat - shows the programmer is running
// 8: Error - Lights up if something goes wrong (use red if that makes sense)
// 7: Programming - In communication with the slave

To summarize connections:

SPI Programming Interface
Status LEDs
(each connected to ground via a resistor)

Finally, here’s the circuit connected to the Arduino:

Arduino connected as an ISP to an ATmega168

In this setup the chip is placed in a ZIF socket. This is not mandatory, but ensures the chip is easily extracted.

The Method

    1. Make sure your version of Arduino IDE is at least 0018.
      From the IDE go to File → Examples → ArduinoISP.

      Click upload. If all went well the heartbeat LED should start winking
    2. Next up, setup the IDE to work on the target board. Click Tools → Board and choose the board according to the chip you want to program:
      ATmega8 – Arduino NG or older w/ ATmega8
      ATmega168   – Arduino Diecimila, Duemilanove, or Nano w/ ATmega168
      ATmega328 – Arduino Duemilanove or Nano w/ Atmega328
    3. If this is the first time you program the target chip, upload the Arduino bootloader. This will make sure the fuses are set properly (more on that later).
      Click Tools → Burn Bootloader → w/ Arduino as ISP

      This process can take some time! Give it a minute or two to complete.

⇒ You should now have a chip running the Arduino bootloader. If you want to program a sketch onto the new chip do the following:

  1. Close all Arduino IDE windows
  2. Find the preferences.txt file (a list of common places can be found here)
  3. Open preferences.txt and change:
    This is explained in details here
  4. Open the Arduino IDE, open your sketch
  5. Make sure Tools → Board is set to the target chip
  6. Compile and upload the sketch
  7. Your new chip should now be running the new sketch!
  8. Revert preferences.txt (remember to close all IDE windows first)

Using these steps you can upload any sketch that would normally work on Arduino. Remember that each target chip has different capacity and limitations.

An alternative to steps 4-11 would be to upload sketches to the target chip using a serial connection like you use on Arduino. This will be explained in details in the next section.

Communicating with the New Chip

While the method showed above in steps 4-11 is useful and stable, it is a bit cumbersome. The manual steps can be avoided with some additional hardware; The new chip can be transformed to a full fledged Arduino by adding it serial access. This will allow loading sketches directly from the IDE without jumping through programmer hoops. As a bonus, this can even be accomplished remotely! Here are a few ideas:

  1. Use an FTDI cable (available here or as a breakout). Once connected, it will register as a new serial port, which can be accessed normally from the Arduino IDE.
  2. Another possibility is to leverage the existing FTDI chip on the Arduino board. This requires removing the original chip from Arduino and use its TX/RX pins. Directions can be found here
    Note: extracting the chip from the Arduino board should be done with care to prevent damage. Also remember to store the chip in proper container to avoid static-discharge damage.
  3. And how about doing a remote-wipe Dollhouse style using an XBEE as a remote wireless uploader? Some links on this: Connections basics, forum discussion.

Additional Use Cases

  1. The simple circuit shown here can be used as a ‘skeleton’ for many projects. Just drop-in an ATmega suited for your needs with supporting circuitry and load it with your favourite sketch. We can also replace the IC with a low power version, like the one used in Arduino LilyPad. These IC’s excellent for projects with special space / power-usage / weight requirements.
  2. This method can be applied to upgrade your Arduino Duemilanove from ATmega168 to Atmega328. Get an ATmega 328 and follow steps 1-3 shown above to bootstrap it with a bootloader. Finally, replace the chip on your Arduino with the new one.

Alternative Clock Sources
Traditionally, Arduino uses an external 16MHZ crystal as a clock source and this is source we used above. The ATmega chip supports various other clock sources:

  1. Use an internal resonator inside the chip
  2. Use an external resonator
  3. Use a low frequency oscillator/crystal (frequency < 8MHZ, usually a watch 32KHZ cryatal)
  4. Use a high frequency oscillator/crystal (frequency >= 8MHZ) ← this is Arduino default

External crystals are more accurate than resonator and require no calibration. For an in-depth comparison see here.
If you decide to use a resonator, it is highly recommended that you run a calibration. Details can be found in the ATmega datassheet

For examples on how to use a resonator instead of a crystal, see some alternative designs: here, here and here.

When selecting a clock source, the ATmega chip in the circuit needs to be configured to be aware of that source. This is done by changing ‘fuse-bits’. We usually don’t need to do this manually, as the process of burning bootloader also sets the fuses for high frequency oscillator usage. If you plan to use a different clock source, see the next section for details.

About the Fuses

Fuse-bits control behaviour of the ATmega chip that usually you wouldn’t change. The most widely used feature is the clock-source. As explained above, there are several possibilities on how to supply clock signal to the chip. To get started on the topic, here’s a recommended reading list:

  1. Introduction and some do’s and don’ts: here
  2. Article focusing on clock sources: here
  3. In depth guide to fuse-bits: here
  4. Walkthrough with the AVR toolchain: here

Finally, a very handy fuse-bit calculator can be found here.

WARNING! Misconfiguration of the HIGH bits can cause ISP’s to stop working. Just in case you bricked your chip, it’s always good to know there’s a little piece of hardware to unbrick it: Fusebit Doctor.

Manual Method to Upload Sketches

The steps described to upload sketches can be done manually. The manual method uses the same tools as Arduio IDE uses in its underlying process. Rather than using the Arduino IDE configuration and buttons, these tools are called directly from the command-line interface. For guides see: an Instructable and this blog entry.

Please note that this method requires manual setting of fuses. This process should be done with care, as it can brick the chip.

Update (18/09/2010): For an extended usage example see the standalone coffee machine post.



Filed under howto, production, prototype

4 responses to “Programming Arduino with Arduino

  1. Pingback: Arduino – zastosowanie w działaniu

  2. adhambadr

    awesome guide worked for me on Arduino 1.0.1 and Arduino Mega 1280 from the very first trial thanks a lot for posting it.
    just a small note to new the Arduino IDEs (1.0+) before the Burn bootloader step one should select from Tools > Programmer > Arduino as ISP or you will get a usb_open error.
    Also for all the non-electronic-circuit-ninjas like me, i found the circuit picture here

    quite easier to follow than the eagle file sketch, but still the text here is much more better. kudos!

  3. hi!
    would it be possible to program another arduino via an arduino micro? as far as I know arduino micro doesn’t have a dedicated chip for ftdi like the Uno.


    • There shouldn’t be a problem. The Arduino Micro uses ATmega32u4 which has built-in USB communication, eliminating the need for the FTDI chip (similar to Arduino Leonardo).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s