Arduino: Programming atmega328 using USBasp and Arduino IDE on Mac OSX



IMG_9153I’ve been playing around with the Arduino for a while now and wanted an way to program an atmega through the Arduino IDE but without involving the board. After a long an gruesome process I finally found the best way of programming a bare atmega328 on Mac OSX. I’m certain this process will work for other AVR micro controllers as well. Before I go into details of how it’s done I’ll cover my intended goals for this project and let you decide if it’s worth it.

GOALS:

  1. Inexpensive and minimal parts
  2. Full continued use of the Arduino libraries and community
  3. SMD Atmega programming for smaller projects
  4. Easy setup and easy to use

Bill of Materials:

  • USBasp – Ebay – $6.95
  • AVR MCU – Price varies this tutorial specifically uses the ATMega328P TQFP-32 package and the SMT Breakout PCB from Adafruit
  • Jumper wires
  • LED
  • Breadboard
  • Optional: 16mhz oscillator and 2x 22pF capacitors (The ATMega328P has an internal 8mhz clock. It’s slower and noticeable when testing the blink example)

USBasp  is a USB in-circuit programmer for Atmel AVR controllers. It simply consists of an ATMega88 or an ATMega8 and a couple of passive components. The programmer uses a firmware-only USB driver, no special USB controller is needed. Schematics and firmware is available at fischle.de 

 Software:

I know there’s a few things to install but don’t get discouraged, each one is easy enough to install. Arduino IDE is the programming environment for Arduino. I’ll assume you are already familiar with IDE. AVRFuses is a simple fuse programmer for the AVR series of microcontrollers from Atmel. It is written to mimic the look, feel and utility of the AVR Studio fuse programmer. CrossPack is a development environment for Atmel’s AVR microcontrollers running on Apple’s Mac OS X, similar to AVR Studio on Windows.

Software Setup: Arduino IDE

Open the Arduino IDE and open ‘Preferences’ from the Arduino menu in the top left corner of the screen. You should see the window below.

Arduino IDE_Preferences

Make sure compilation is checked and click on the preferences.txt path to open the text file. We will be changing some settings and adding a setting to this file. But first be sure to close the Arduino IDE completely or the settings will not save.

Now change ‘export.delet_target_folder=true’ to false.

export.delete_target_folder=false

Add the below text to the bottom of the preferences.txt file. Be sure to change username to your username.

build.path=/Users/username/Documents/Arduino/build

That’s it for the Arduino IDE. All the files associated to your sketch will be placed into the build path, including the .hex file.  Note: AVRFuses will use the hex file to program the firmware to your microcontroller. An example to the hex file will show as blink.cpp.hex

Software Setup: CrossPack

This is just here for completeness. All you need to do with CrossPack is install it. We will be using this to use AVRDude in AVRFuses.

Software Setup: AVRFuses

Open AVRFuses and you will be presented with the following window.

AVRFuses_Preferences

You are currently looking at the preferences menu. First you will need to place the file path to AVRDude into the box. The path for CrossPack’s AVRDude is listed below and can be copy and pasted. Disregard the path in the picture that is the path for MacPorts AVRDude, which requires a little more to install. Next select usbasp as the programmer and everything else should already be at the defaults settings.

/usr/local/CrossPack-AVR/bin/avrdude

Click close and you should be presented with the AVRFuses GUI.

Screen Shot 2013-06-10 at 5.54.47 PM

Here you selected the device you’re programming and the file path of the hex file you will be using. But first we need to make the hex file. We’ll cover the process in the next section.

Lets Get Programming

Firstly we need to hook up USBasp to the microcontroller. The diagram below shows where you should connect the USBasp to the atmega328.

USBasp_pinout

 Once you’ve connected the two we need to check the fuses. Open AVRFuses and select the Fuses tab at the top of the window next to program. You should see something similar to below.

AVRFuses Fuses

Now click ‘Read’ and if you’re using a factory microcontroller you should see that the internal 8 MHz clock is setup. The below option will be selected.

Int. RC Osc. 8 MHz; Start-up time PWRDWN/RESET: 6 CK/14 CK

If you would rather use the 16 MHz external crystal then you would need to unselect the internal 8 MHz and select the below option and click ‘Program’.

Ext. Crystal Osc. 8.0-   MHz; Start-up time PWRDWN/RESET: 16K CK/14 CK + 65 ms

You will also want to make sure all you fuse settings look similar to the picture below. This will ensure you’re atmega328p functions without any issues. If you want to know about each fuse check out AVR Fuses Explained.

AVRFuses_Preferences 2

Once your fuses are setup, open the Arduino IDE and open the blink example. Click verify and the hex file will be saved in the build path you indicated earlier in the preference file.

/Users/username/Documents/Arduino/build

Arduino IDE_Click Verify

NoteIf you are using the internal 8 MHz clock be sure to drop the delay in the sketch down to 100 to see the LED blink.

Note. At this stage selecting a programmer or board will not change the outcome of programming the microcontroller.

If the sketch shows the LED connected to pin 13 of the Arduino you will need to connect it to pin 17 of the atmega328.

IMG_9153

In AVRFuses select the .hex file to be written to flash. Remember the blink.cpp.hex file will be located in the build path. Now click ‘Program’ and if all went well you should see a blinking LED.

4 thoughts on “Arduino: Programming atmega328 using USBasp and Arduino IDE on Mac OSX”

  1. Excellent writeup, very useful thank you! There appears to be some issues with the usbasp and recent versions of MacOSX but this method is a good workaround.

Leave a Reply