Friday, August 23, 2013

Enabling USB on Arduino MIDI Keyboard

Considering all modern MIDI controllers have ability to
communicate via USB you might think it will be quite nice to connect MIDI
keyboard we built directly to USB port on your PC? That’s exactly what we are
going to do. Below are the end-to-end steps I took to make it work. It might help someone? May
be you?

This instructions were tested on Arduino UNO and should work
with Arduino MEGA (not tested, please let me know if you managed to make it
work?)

To achieve this we are going to use HIDUINO firmware that makes
Arduino UNO board to appear as USB HID (MIDI) device.

The important thing to understand is that Arduino has two
chipsets ATMEGA8u2 (or 16u2) and main chipset ATMEGA328 (or ATMEGA2560 in case
of Arduino MEGA). Purpose of ATMEGA8u2 is to bridge communication between USB port
and ATMEGA328. Originally it appears as USB Com port device (or USB modem on
the MAC) you might have noticed that while selecting Arduino as device on COM
port in Arduino IDE. Flashing HIDUINO firmware on to ATMEGA8u2 will make it act
as a USB Midi device. ATMEGA8u2 will effectively turn serial messages from
ATMEGA328 chip to MIDI messages sent over USB.

Below are the hardware/software setup and actual steps you
need to perform to program the Arduino.

ISP Programmer

One of the cheapest and tested ISP programmers is USBTinyISP
available for about $15 from ebay or similar price on the net. There are other
options but this one worked well for me…

If you are using MAC you do not have to install drivers - it
should work right away.

If you using Win7 / XP there are drivers and resources on
the net explaining how to install it. Personally I’ve installed it on Windows 8
and had to do couple of tweaks to make it work. Below is the guide that helped
me. The catch with Windows 8 is that you have to installed unsigned drivers and
Win8 does not quite like it…

Identify the chipset

You will need to take note of the chipset installed on Arduino
Uno board as steps/firmware will vary based on the version of the chipset used
by the board. As wording on the chipset is quite small the best way to read it
is to take a picture. Phone camera worked well… After you took the photo zoom
in and it should be clear enough to read (wow I just realised my phone has 8
megapixel camera!).

Program the MIDI Keyboard sketch

Sketches cannot be bootloaded onto an Arduino while a
HIDUINO firmware is loaded on the 8u2. That’s why you will have to program the
MIDI Keyboard sketch before programming the HIDUINO firmware.

Usually you will have to switch between the default
usbserial and HIDUINO firmwares regularly during development in order to
program modified sketch onto Arduino. Further we will see how to revert back to
original firmware.

Programm HIDUINO Firmware

Programming part is quite easy.
All you need to do is to make sure you have firmware located in the folder you
executing the avrdude from, arduino is connected to ISP and execute below
command. While command executed you will observe similar output to what I got
during successful programming of my device:

At this point you can
disconnect USBTinyISP ribbon cable and connect USB directly to Arduion board.
The device should appear as new USB MIDI device on your computer. All you favourite
Musical software should recognize the device as HIDUINO midi. Yay! :)

Going back to original firmware

If you would like to get back
original firmware (could be in order to program new sketch on Arduino) you can
program original usbserial firmware by following below steps.

You should be able to find original
firmware from Arduino IDE folder: arduino-1.0.5\hardware\arduino\firmwares\atmegaxxu2\UNO-dfu_and_usbserial_combined.hex

Below is the command that you
will need to execute and example of the successful on the console.

Please note for 16u2 you will
need to use following firmware: Arduino-COMBINED-dfu-usbserial-atmega16u2-Uno-Rev3.hex
available from the Arduino IDE folder the command will change to reflect the
firmware:

2 comments:

hi, thanks for this great tutorial. I have a a question, since we are flashing the ATMEGA8u2 chip we are no longer able to upload the sketches via the usb interface. So my question is: would it be posible to use the arduino ICSP to upload the sketches so that we don´t have to be flashing the ATMEGA8u2 everytime we update the sketch?