Sunday, April 17, 2011

Breaduino

In order to use the Arduino IC on a real board, we need to learn how to use it.
There's a common type of Arduino configuration for this called Breaduino (derived for Arduino on a Breadboard).

What I want to use is the minimal configuration, in order to avoid buying an external crystal oscillator, knowing that it is possible to use the IC's internal oscillator.

These oscillators are what sets the pace for a circuit. The faster the oscillator can go, the faster can the IC make calculations.

My Arduino is a Duemilanove an has an ATMega328p IC. In the datasheet it is stated that we can use an external oscillator up to 20 MHz (for Arduino we should use one at 16 MHz). The internal oscillator runs at 8 Mhz, but these frequencies can be divided by 8 using configuration (I'll write more about it in the end).

Once again the Arduino site has very good references and tutorials:
- Arduino To Breadboard
- Standalone
- Disabling Auto Reset (You'll need this one, even not being mentioned on the above tutorials)

If you look for the 'Minimal Circuit (Eliminating the External Clock)' sections you'll find an illustrated guide on how to configure the Arduino IDE and to connect the IC to the breadboard for both burning the bootloader and programming the IC.

After reading all of these and much more, I still couldn't burn a bootloader for using the internal oscillator on the ATMega328. Because of that I read and learned about understanding the configurations of the IDE, programming the ATMega by command line using AVRdude, and setting the fuses (configuration of the IC) in order to configure its behavior.

So why couldn't I burn the bootloader using the Minimal Circuit?
All my ATMega came preloaded with the regular Arduino Bootloader for use with the Arduino Board. That way they are configured to work with an external 16 MHz crystal.
So, because of that, the ATMega that I was trying to burn a bootloader into wasn't working. It needed the external crystal as configured for me to be able to burn a different bootloader.

Some of the errors I've seen because of this (both on Arduino IDE and running avrdude by command line):
- avrdude: Yikes! Invalid device signature;
- avrdude: Device signature = 0x00000000;
- avrdude: Expected signature for ATMEGA328P is 1E 95 0F;
- avrdude: stk500_getsync(): not in sync: resp=0x00;
- avrdude: stk500_getsync(): not in sync: resp=0x15;

Well, I had to buy a 16 MHz crystal after all.

To sum it up, what I had to do was:
1) In the \hardware\arduino\boards.txt add the breadboard settings below
##############################################################

atmega328bb.name=ATmega328 on a breadboard (8 MHz internal clock)

atmega328bb.upload.protocol=stk500
atmega328bb.upload.maximum_size=30720
atmega328bb.upload.speed=57600

atmega328bb.bootloader.low_fuses=0xE2
atmega328bb.bootloader.high_fuses=0xD8
atmega328bb.bootloader.extended_fuses=0x07
atmega328bb.bootloader.path=atmega
atmega328bb.bootloader.file=ATmegaBOOT_168_pro_8MHz.hex
atmega328bb.bootloader.unlock_bits=0x3F
atmega328bb.bootloader.lock_bits=0x0F

atmega328bb.build.mcu=atmega328p
atmega328bb.build.f_cpu=8000000L
atmega328bb.build.core=arduino
Note that these settings are not the same as downloaded with breadboard.zip from the Arduino site;

2) On Arduino IDE, load the ArduinoISP sketch of the Examples tab to the Arduino Board;

3) Set the hardware as in the images on the ArduinoToBreadboard tutorial (with the external crystal). Also you will need to disable the Auto-Reset feature if your board has it (and Duemilanove does have it);

4) (Optional) I read the comments on the ArduinoISP sketch and it talks about some pins you can use to light up some status LEDs. I used them to help me understand what was happening;

5) Upload the ArduinoISP sketch (confirm that you have the correct board configured in the IDE - in my case 'Arduino Duemilanove or Nano w/ ATMega328');

6) Make sure once again that all hardware is well connected.

7) Select the ATMega328 on a breadboard (8MHz internal clock) and burn the bootloader 'w/ Arduino as ISP'. It will take a while.

8) Now that the bootloader is set, you can upload a sketch. Don't forget to remove the ATMega from the Arduino Board, so that the ATMega on the breadboard is the one used. Also, don't forget to connect only the VCC, GND, RX and TX pins (one of my mistakes was to leave the crystal connected and because of that I couldn't upload sketches);

[edit]
A tip about setting extended_fuses (efuse):
This fuse controls the brown out detector and it can influence the correct working of the controller if you use less than 5V to power it.
There are only 4 possible values for this fuse so the controller  might only read those 2 bits in the full 2 bytes reserved for the fuse.
If you can't burn a bootloader because the efuse doesn't match the expected efuse (like 0xFF != 0x07) it means that that kind of controller expects that bits 2 to 7 are the opposite of what you set (remember that they won't be really needed), so you might need to adjust the value for the fuse accordingly.

So here are the values you need with bits 2 to 7 ON and OFF:
OFF(1)       ON(0)       Meaning
0xFF   <->   0x07    =   Brown-out detection disabled
0xFE   <->   0x06    =   Brown-out detection at VCC=1.8V
0xFD   <->   0x05    =   Brown-out detection at VCC=2.7V (Arduino 5V default)
0xFC   <->   0x04    =   Brown-out detection at VCC=4.3V

1 comment:

  1. GOD BLESS YOU SIR SERGIO! YOU ARE SO FUCKING GENIUS! Thanks for you code. Shit!!! :) I've been staying up late for a couple of days because of this bootloader shit. Thanks!!!!! Ill bookmark this page!

    regards,
    Glenn Posadas
    from the Philippines :)

    ReplyDelete