electronut Programming & Embedded Systems - by Mahesh Venkitachalam

Installing Arduino Bootloader on an ATmega32u4

The Atmel ATmega32u4 gained popularity with its use in the Arduino Leonardo, due to the built-in USB support, which made an additional chip unnessary for that purpose. I’ve had a couple of ATmega32u4s in storage for a while, so I decided to try and solder the chip and make an Arduino Leonardo compatible board out of it. Both Adafruit and Sparkfun have ATmega32u4 breakout boards that they sell, and I have used their designs as a reference. Why bother? Because if you are developing an AVR based board with USB, this chip is worth looking at, especially since you can use the existing Leonardo bootloader.

##Soldering

The first step of course, is to solder the darn chip. I used a QFP 44 breadboard adapter, and started by tinning the pads.



ATmega32u4


Next, I soldered the chip. Diagonal corner pins first, to fix the chip on the board. Used plenty of flux, and a magnifier loupe (getting old!).



ATmega32u4

After a close inspection, checking for shorts, etc., I assembled the circuit on a breadboard. The schematic can be found here.



ATmega32u4


For the USB connection, I cut an old USB cable. The color scheme for the wires is shown below. But I (painfully) discovered that D+ and D- are switched some times.



USB

##Bootloader

After setting up the breadboard, I used an ISP programmer (Sparkfun, based on usbtiny) and avrdude to first check the fuses on the chip - to see if the connections are alive, basically:

$ avrdude -c usbtiny -p m32u4 -U lfuse:r:low_fuse_val.hex:h -U hfuse:r:high_fuse_val.hex:h

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9587
avrdude: reading lfuse memory:

Reading | ################################################## | 100% 0.00s

avrdude: writing output file "low_fuse_val.hex"
avrdude: reading hfuse memory:

Reading | ################################################## | 100% 0.00s

avrdude: writing output file "high_fuse_val.hex"

avrdude: safemode: Fuses OK (H:F3, E:99, L:5E)

avrdude done.  Thank you.


Next, I got the Sparkfun bootloader and downloaded it to the chip as follows:

$ avrdude -c usbtiny -p m32u4 -U flash:w:Caterina-promicro16.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "Caterina-promicro16.hex"
avrdude: input file Caterina-promicro16.hex auto detected as Intel Hex
avrdude: writing flash (32762 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 32762 bytes of flash written
avrdude: verifying flash memory against Caterina-promicro16.hex:
avrdude: load data flash data from input file Caterina-promicro16.hex:
avrdude: input file Caterina-promicro16.hex auto detected as Intel Hex
avrdude: input file Caterina-promicro16.hex contains 32762 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 32762 bytes of flash verified

avrdude: safemode: Fuses OK (H:F3, E:99, L:5E)

avrdude done.  Thank you.


Now, after unhooking the ISP programmer and connecting the USB cable to my computer (running OS X), I got:

# before plugging in USB
$ ls /dev/tty.*
/dev/tty.Bluetooth-Incoming-Port        /dev/tty.MaheshsJawbone-SPPDev
/dev/tty.Bluetooth-Modem

# after plugging in USB
$ ls /dev/tty.*
/dev/tty.Bluetooth-Incoming-Port        /dev/tty.MaheshsJawbone-SPPDev
/dev/tty.Bluetooth-Modem                        /dev/tty.usbmodem1411


Yay - it’s alive! Now I opened up the Arduino IDE, selected Arduino Leonardo as the board and the above serial port. I put an LED on D2 and wrote a blink sketch which also printed debug statements on Serial. To upload the code to the board, I had to hit upload, and then set RESET of the chip to GND momentarily. And it just worked.

##Acknowledgements

Thanks to Ayush Sagar for pointing out to me that the D+/D- wire colors in USB are unreliable!

##References

  1. Atmel ATmega32u4 data sheet.
  2. Adafruit ATmega32u4 breakout board documentation.
  3. Sparkfun ATmega32u4 breakout board documentation.

If you liked this article, please consider supporting my efforts by purchasing my book.

Python Playground, published by No Starch Press, USA, is a collection of imaginative programming projects that will inspire you to use Python to make art and music, build simulations of real-world phenomena, and interact with hardware like the Arduino and Raspberry Pi. Do check it out!