MIDI Harp: ADC Settings

One challenging aspect of our project was configuration of the analog-to-digital converter (ADC) for maximum stability and playability. Taking inspiration from Atmel Software Framework example projects, various Atmel publications, and a vast chorus of AVR-forum participants, we included the following ADC initialization function in our final project:

//initialize free running ADC sweep
void adc_init(void)
	// Initialize configuration structures.
	struct adc_config         adc_conf;
	struct adc_channel_config adcch_conf;
	adc_read_configuration(&ADCB, &adc_conf);
	adcch_read_configuration(&ADCB, ADC_CH0, &adcch_conf);

	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_AREFB) ;
	adc_set_clock_rate(&adc_conf, 5000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_FREERUN_SWEEP, 3, 0);
	adc_set_callback(&ADCB, &adc_handler);
	adc_write_configuration(&ADCB, &adc_conf);


	adcch_set_input(&adcch_conf, ADCCH_POS_PIN1, ADCCH_NEG_NONE, 1);
	adcch_write_configuration(&ADCB, ADC_CH0, &adcch_conf);
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN2, ADCCH_NEG_NONE, 1);
	adcch_write_configuration(&ADCB, ADC_CH1, &adcch_conf);

	adcch_set_input(&adcch_conf, ADCCH_POS_PIN3, ADCCH_NEG_NONE, 1);
	adcch_write_configuration(&ADCB, ADC_CH2, &adcch_conf);

This code utilizes ASF to allow for human readable (and Intellisense-aided!) modification, without requiring the bit by bit setting of appropriate AVR registers. Our project was configured for a free-running, single-ended, 12-bit, interrupt-triggering sweep of 3 ADC channels, referenced against an external 2.5V source, at the modest frequency of 5 kHz. Once the ADC is enabled, each finished conversion calls a function that averages the most recent 15 values, and translates this result into a “state” (from which is derived a tone) and volume (from height within the trigger range).

MIDI Harp: Incorporating LUFA into an Atmel Project

LUFA is a USB communication library used extensively in the MIDI Harp project. We found incorporating LUFA with ASF libraries to be a bit of a chore, so below we have detailed the steps that will get you through modifying your project so you can get on our level.

The first step is to download the LUFA software pack for Atmel Studio 7, which can be found here: https://gallery.atmel.com/Products/Details/d1057c19-3815-487e-baa7-0001985c6901.

Once you have this fully installed, create a new temperature sensor example project for the A3BU (We started here as we knew this included everything for utilizing ADC).  Import the required LUFA modules, shown below, via ASF Wizard.

Final ASF Wizard

Now, ASF and LUFA employ conflicting numerical definitions to indicate the feature-set of various models of Atmel boards.  To correct this, you first need to go to your project’s properties by right clicking the orange project folder in the Solution Explorer, clicking properties, and going to the toolchain tab. Under AVR/GNU C Compiler->Symbols you will discover contradicting BOARD definitions from ASF and LUFA respectively.  We found it easiest to keep the definition used by Atmel and update the LUFA files accordingly.

Toolchain Symbols fixed

Two additional symbols must be created with the green + button as follows: F_USB=48000000UL and F_CPU=32000000UL

With BOARD=BOARD_A3BU_XPLAINED deleted and BOARD=XMEGA_A3BU_XPLAINED remaining, you will need to make a series of changes to the files associated with LUFA.  First, in the Solution Explorer, go to src/LUFA/LUFA/Common/ and open BoardTypes.h.  Two lines have to be commented here to correct the conflicting definitions.  First, comment line 160, “#define BOARD_A3BU_XPLAINED 31”.  This removes LUFA’s numerical definition for the board definition we are no longer using.  Then, comment line 148, “#define BOARD_EVK1101 27”.  This conflicts with Atmel because XMEGA_A3BU_XPLAINED is defined as 27, and gets mixed up with this other board definition from LUFA.  LUFA files then attempt to set up the project for the EVK1101 board instead of A3BU.

BoardTypes Fixed

Next, go into Solution Explorer src/LUFA/LUFA/Drivers/Board/ and open Buttons.h, Dataflash.h, and LEDs.h. (There should also be a folder called XMEGA. If this is not in the folder with the above files, create the LUFA example project “MIDI Device Demo (Class Driver APIs) – XMEGA Architecture”, and copy this folder into this location.)  Within each of these files, find a line that looks similar to #elif (BOARD == BOARD_A3BU_XPLAINED) and change BOARD_A3BU_XPLAINED to XMEGA_A3BU_XPLAINED as shown below.

Buttons fixed

Once each of these files have been changed, you may close these files.  Now go to src/config/LUFAConfig.h and make the changes shown below to the XMEGA section starting at line 91:

LUFAConfig Fixed

These will initially all be commented out.  Uncomment the lines shown and add the values shown to the right (lines 106, 113,116, 118, and 120).

Finally, you will need to copy the files Descriptors.h, Descriptors.c, and MIDI.h from the src folder of the MIDI example project mentioned above, and put them into the src folder for your project. There are several functions which can be found in MIDI.c from this example project that must be used to make the USB connection and send MIDI signals.  Copying these files allows the necessary functions to work properly.  Then make sure to include these files in the Solution Explorer so your project knows where to find them.

src Folder with added files

You should copy code sections into your project from the MIDI demo project’s MIDI.c file that initialize and facilitate USB communication, including one to manage clock sources and speeds, while removing a conflicting initialization of ASF’s system clock management module.  After that, you’re good to go!  We’ll leave the rest of the coding to you.  Have fun with your MIDI harp!

MIDI Harp: Schematics


Above, you can find the wiring diagram for our MIDI harp.  Surprisingly simple, considering how awesome the result is.  The infrared sensors are Sony GP2Y0A21YK0F which have a range up to 80cm.  For the foot pedals, we used two basic sustain pedals such as would be used with a digital piano/keyboard.  These function like your basic switch, with easy operation by foot.   The 2.5V Vref was created by using another USB cord for 5V power, then incorperating a voltage divider to get a 2.5V output.  Any Zener diode with a 3.3V cutoff will function.  These are used to prevent accidental over-voltage outputs from the IR sensors from damaging the A3BU boards inputs.

One notable mistake we made was ordering surface mount zeners. Below you can see our shoddy soldering involved with fixing this mistake. Always be aware of what you’re buying, folks!


Mellow Mushroom – About our MIDI harp

MIDI harp full

It may look a bit messy here, but this bad boy is inspired.  We based our idea off of the laser harp MIDI instrument.  Although we weren’t able to use real lasers due to power constraints and safety issues (pff safety…) (Instructor note: one demerit for flippancy), we found that infrared distance sensors get the job done, with the added feature of allowing multiple notes per “string”!  Basically, a MIDI instrument sends a digital signal rather than an audio signal.  The instrument is plugged into a computer via USB, and through one of many MIDI software programs, the digital signal is converted to musical data which can be output as a real sound by the computer.  The MIDI software is able to control the sound output, allowing us to produce everything from harp sounds, to synths, to drums all with our nifty little gadget.  But, be not fooled, ye mortals, this contraption was no walk in the park. Read on to find out more about the ups and downs and struggles of Mellow Mushroom and the MIDI harp.