Tuesday, November 22, 2011

How to run a bare ATMEGA328-PU with an internal oscilator (at 8Mhz)

Arduino Ecosystem rocks
Arduino is a great way to minimize the amount of variables when getting into microcontrollers. Flashing, cross-compilation, libraries, dongles are much easier(and cheaper!) when standardized.

I bought an expensive  arduino nano v3, I got comfortable enough with it to blink an led and do with pwm. Unfortunately arduinos(except lilypad) feature lame 5V voltage regulators which require 6.2V to do anything useful; though atmega chips will supposedly run at as low as 1.8V. End result was that the NanoV3 did not run properly when hooked up to my 5V power supply on the bike.

So in the in the interests of not building a new power supply and not using a board with a whole bunch of pointless components I bought some straight-up ATMEGA328-PUs and a USB flasher.

The rest of this post documents various gotchas.

Pinout on Sparkfun is mirrored!
Red stripe on the cable indicates top. However the pinout is mirrored. One is supposed to look at it with the stripe on top and holes facing away.

I wondered why seemingly identical chips were selling for a different price. P in 328P stands for picopower and as far as I can tell it allows one to turn off brownout protection. In my case brownout protection is something I want. However, fucking avrdude does not recognize ATMEGA328(without P) and bitches that one should pass -F when specifying -p atmega328p(there is no atmega328). This prevents Arduino IDE from functioning. In order to get a functioning IDE I needed to:
  1. Modify signature for atmega328p in avrdude.conf:

    - signature       = 0x1e 0x95 0x0F;
    + signature       = 0x1e 0x95 0x14;

    (Obviously the proper thing to do here is to add the new chip to avrdude)
  2. Use breadboard.zip from http://arduino.cc/en/Tutorial/ArduinoToBreadboard as an inspiration for the following boards.txt entry:

    usbtiny328.name=[usbtinyisp]ATmega328 8mhz




  3. Burn bootloader with tools->burn bootloader->UsbTinyIsp
  4. Assemble board as per http://arduino.cc/en/Tutorial/ArduinoToBreadboard. One does not need to hook up GND(pin 22) or AVCC(pin 20). Connect ATMEGA RESET(pin 1), VCC(pin 7), GND(pin 8), MISO, MOSI, SCK(pin 19) to the flasher.
  5. Check the pin mapping. Note that Arduino software pins do not match physical pins(this cost me a few hours).
  6. Proceed to use Arduino tools with a convenient $2.82 chip* + $15 programmer rather than an inconvenient/overpriced Arduino board.
     * I would've spent an extra $1 to get the ATMEGA328P had I known the many hours of agony it would've avoided.