Tag Archives: Leonardo

Rescue guide for your Adafruit ATmega32u4 breakout board

The ATmega32u4 breakout board is a beta product. The hardware is essentially perfect, but the firmware is wonky. At least once, I’ve convinced myself that mine was broken, but I eventually figured it out. Here’s how to get yours back in shape.

Things required to get all the way through these steps:

  • The board
  • A USB cable to connect the board to your computer
  • A working copy of avrdude. If you have the Arduino IDE on your system, then it’s buried deep inside the IDE.
  • Possibly an in-system programmer. These instructions will work with any USBTiny-compatible programmer.

Before we get going, a warning: the fuses and the bootloader must match, because the fuses tell the chip where the bootloader is. If you have the Leonardo bootloader on your board, beware! That one uses a different fuse combination (I think FCD5C3) from the Adafruit one (FCD0C3). Moreover, you need the ISP to change the fuses; an ATmega32u4 unfortunately can’t change its own fuses. It’s completely possible for you to flash the wrong bootloader to the board, leaving it in a zombie state, needing the ISP to fix it. This means that these steps might take you from a sort-of working board (e.g., an early Leonardo bootloader) to one that’s completely broken (the Adafruit bootloader with the wrong fuses), and if you don’t have an ISP to set the fuses, you’ll be stuck. TL;DR: if you have a board that mostly works, and don’t have an ISP, then stop now.

If your board is showing up as a serial device, we can learn a little from it directly, without the ISP. Run avrdude -c avr109 -p m32u4 -v -U lfuse:r:-:i -U hfuse:r:-:i -U efuse:r:-:i -P /dev/tty.usb_path_to_the_device_serial_port. A bunch of information should get spit out, ending with something like this:

avrdude: safemode: lfuse reads as FC
avrdude: safemode: hfuse reads as D0
avrdude: safemode: efuse reads as C3
avrdude: safemode: Fuses OK

If you get an error, it’s possible your board is running the Leonardo bootloader, so you’ll need to ask differently. Instead of avr109 in the previous command, tryarduino.

Now you want to figure out what those fuses mean. Visit Frank Zhao’s AVR Fuse Calculator  (in fact, you might want to set that as your home page) and type the three into the “Current settings” fields at the bottom of the page. Apply values, then go back up to the top of the page and see what the “Boot flash size” dropdown is set to.

If the size is 2048, this is GOOD because it matches the size of the known-good 2K-word (4096-byte) Adafruit build of the LUFA CDC bootloader. If this size is anything else (probably 512, for the 1024-byte Leonardo bootloader), then stop unless you have an ISP because you need an ISP to change the fuses. If the size is not 2048 but you do have an ISP, proceed to change the fuses.

To change the fuses, plug your board into your ISP and run avrdude -v -c usbtiny -p m32u4 -U lfuse:w:0xFC:m -U hfuse:w:0xD0:m -U efuse:w:0xC3:m. Again, skip this step if your boot flash size is already 2048. (Note that these fuse values came from the Adafruit BootloaderCDC makefile, but the F3 verifies as a C3 because not all 8 bits of the extended fuse are writable. Check out the fuse calculator to see for yourself.)

Next, flash the one true bootloader that today is known to work: BootloaderCDC.hex. Run either of these commands, depending on whether your board is connected to the ISP or directly to your machine:

Directly connected: avrdude -c avr109 -p m32u4 -P /dev/tty.usb_path_to_the_board_serial_port -U flash:w:BootloaderCDC.hex

Connected to your ISP: avrdude -c usbtiny -p m32u4 -U flash:w:BootloaderCDC.hex

Note that, unlike in the previous step, I am not telling you to substitute arduino foravr109 in this step. That’s because doing so would kill your board and require the ISP to rescue it. If you wanted to follow that step, it means you misunderstood the earlier steps about current bootloader size.

By this point, you should have BootloaderCDC on your board, and fuses for a 2048-word bootloader. Plug the board via USB into your computer and press the reset button. You should see the pulsing green light as well as a new serial port on your computer. All should be well.

Bootloading an ATmega32u4 with Arduino

fr_988_size880

A month or two ago I ordered some of Atmel’s ATmega32u4 microcontrollers to play around with. I was excited to get them, but at the time I was really busy at work, so I quickly forgot about them. Last Friday I received some TQFP44 breakout boards and decided to spend some time this past weekend getting the Arduino bootloader and some example sketches up and running on the ATmega32u4. Turns out, it was pretty easy.

 

 

 

One of the big features of the ATmega32u4 is that is has a built-in USB interface. This means that it’s super simple to program (just connect a USB cable and you’re good to go). Now in order to upload Arduino sketches to it, I first had to burn the Arduino bootloader to it. Since I don’t have a programmer, I used an Arduino Uno.

 

The breakout boards for the ATmega32u4 I got from one of my favourite online shops;Dipmicro. I soldered the microcontroller using my hot air station. I could have used my soldering iron, but that’s less fun.

 

Once that was done, I uploaded the ArduinoISP sketch to my Uno. Then I put the 0.1uF capacitor between RESET and GND. I added the 32u4 to my breadboard and put a 1uF decoupling capacitor between pin 6 and GND. Then I added the 16MHz crystal and 22pF ceramic caps, wired the appropriate SPI pins, and wired all of the GND and VCC pins. Here’s a diagram of the wiring:

Screen-Shot-2013-06-03-at-9.01.06-PM

 

I believe that some of the VCC and GND connections are also made internally on the 32u4 but it doesn’t hurt.

Once it was all wired up, I plugged my Uno back into my laptop, opened the Arduino IDE, chose “Arduino Leonardo” from the list of boards, and hit “Burn Bootloader.” After about 20 seconds, it was finished. Once

Now, in order to test it, I removed my Arduino Uno. What I had now was a very basic Arduino Leonardo clone. I added an LED on pin 25 (digital pin 4), and a switch from the RESET pin (13) to GND. If you haven’t used an Arduino Leonardo before, they are a little tricky for loading sketches – you have to hold the reset button, hit Upload in the Arduino IDE, and then release the button. If you get an error, just try again.

Lastly, I added the USB connections. I used a stripped USB cable, which had four wires:

  • Black – GND
  • Red – 5V
  • Green – D+
  • White – D-

The GND and 5V, I connected to the power rails, and the data lines I connected to pins 3 and 4 through a 22ohm resistor. Pin 3 is D- (white) and pin 4 is D+ (green):

Screen-Shot-2013-06-03-at-10.52.25-PM

 

With the wiring done, I plugged the USB cable into my laptop and switched back over to the Arduino IDE. I opened the Blink example, changed the LED pin to pin 4, held down the reset button, hit Upload, and let go of the reset button.

A few seconds later, my LED started blinking. Success.

 

Adafruit ATMega32u4 as Arduino

Leonardo

You can use the Adafruit ATMega32u4 breakout board  as an Arduino Leonardo by flashing the bootloader in Arduino 1.0.1 to the board. To follow this process, you use another Arduino, like the Arduino Duemilanove, as a programmer.

  1. Connect the Arduino Duemilanove to your computer’s USB port
  2. Open the Arduino 1.0.1 environment
  3. Select File > Examples > ArduinoISP and the IDE will open a new window with the Arduino sketch that makes the Duemilanove programmer.
  4. Select the board type you use as programmer, in this case a Duemilanove: Tools > Boards > Arduino Duemilanove w/ATMega328.
  5. Select the serial port your Arduino (the programmer) is using (/dev/ttyUSB0 in my case)
  6. Click the Upload icon to load the ArduinoISP sketch onto the Duemilanove

Now the Duemilanove is a bootloader programmer!
Next, to load the bootloader onto the ATMeag32u4 board, follow these steps:

Connect the Arduino’s pins to the breakout board’s pins.

 Arduino Breakout
 10  RESET
 11  MOSI
 12  MISO
 13  SCLK

Add three LED’s (through 150ohm resistors) to these pins for status:

 9  Pulse – (yellow) indicates that the developer is working
 8  Error – (red) lights when an error occurs
 7  Programming – (green) communicating with the breakout board

 

  • Note: If you have an Arduino UNO, put a 10uF capacitor between RESET and GND pins, to disable autoreset function.

Now that the breakout board is connected, you are ready to burn the bootloader.

  1. Select the serial port (of which board?) in the Arduino IDE: Tools > Serial Port > (Arduino Serial Port)
  2. Select the Arduino Leonardo as the board: Tools > Board > Arduino Leonardo
  3. Make the Arduino Duemilanove the programmer by selecting Tools > Programmer > Arduino as ISP
  4. Select Tools > Burn Bootloader to load the bootloader onto your breakout board.
  5. The LED’s will blink to indicate status. It takes about a minute.
  6. Now disconnect everything.
  7. Plug the breakout board directly into your computer.