Thursday, 12 January 2012

The Arduino Air Drums Part 2

Doing things the way you see it, going by your own heart and soul, that is pure artistic integrity. Whatever the hair is six or sixty inches long, the eyes have make-up or not, the riffs are in 'E' or 'F' sharp, the amps are Marshall or not, all those things don't matter if you are doing it for the right reason, which to me means doing it for yourself! 

“What's the message in Metallica? There is no message,
 but if there was a message, it really should be look within yourself,
 don't listen to me,
 don't listen to James, don't listen to anybody,
look within yourself for the answers.”
Lars Ulrich
- this guy -

Although people hate Lars because he's not a great drummer and he sued Napster and can't play things right away and everything, you have to give it to the guy for being one of the most vocal drummer and speaking his mind out.
Anyway, I wanted to start the post with a cool quote and it kinda blew out. Sorry. :P 

The drums aren't complete without pedals, right? We need the bass pedal to play the bass and a hi-hat pedal to open and close the hi-hat. If you're unfamiliar with the awesome kit that is the drums, Google has THE picture. 

We followed mmidgal on his concept and used two slippers, one with a ADXL335 accelerometer and one with the photocell. 
[UPDATE: The photocell that we got wasn't very effective in changing it's resistance fast, so we decided to remove this part of the kit completely] 

So what we did was, we made a slit in our first slipper almost 3/4th the way in the front so as to insert the accelerometer. 

The accelerometer was connected to the USB connector
 the same way that we had done for the sticks. 

Now that we're done with all three of our elements, the easy part was hooking the circuit up. USB cables are awesome for the job, and pretty cheap as well! 

Now we move on to 

The Program 

For the impatient, you can find the code Here.

But I'd like to take a while and explain what we tried to do with the code and the problems that we ran into:

Firstly, a useful function to see how the Arduino is 'reporting' back changes given by the accelerometer is the analogRead(pinNum)
along with the Serial.println(var) function, both of these when used in one program displays the reading received by the Arduino program as illustrated here. The results are then printed on the Arduino's serial monitor. 

Using the above program we noticed that the value printed changes when the accelerometers are moved in their respective directions. So we figured our basic logic would be

if(valueReadBySensor > someThreshold) then return (a MIDI message)

and that's just repeated for all the other accelerometers.

One more important thing is that we could never get our MIDI to USB converter to work with our program, after days of searching, we chanced upon a brilliant little piece of software, that reads MIDI messages directly from the USB port without the use of any DIN connectors or anything!!

It's brilliant and so simple to use!! Just select the output port as Microsoft GS Wavesynth and you're good to go! We experimented with other softwares and virtual MIDI ports such as LoopBe and MIDIyoke but we could never get a sound out of Reaper or Audacity. 

Alright, so now you have your simple piece of code to play a note when the accelerometer moves an appropriate amount.

void loop()
  int threshold = 375;
  int reading = analogRead(A0);
  if(reading > threshold)
  noteOn(9, 35, 120);

[You will find the definitions for noteOn and everything in the code.]

This is great now, but what was happening was since there were a bunch of readings above the threshold that appeared as the drumstick was flicked, the S2MIDI software gave us a 'trrrrrrrrrrrrrk' sound. 

So, we thought delay() was a great function, which paused the function for a certain number of milliseconds. But this still was not good enough because we could never reach a compromise between a really low delay value and the 'trrrrrrk' sound.

After days and days of experimenting, we finally decided that the code mmidgal posted on his video was the best, although we made some modifications to make it better.

Dave Lombardo style. Well, almost. :P 

We used arrays along with the function millis() which really increased the response of the drum kit and allowed us to play Slayer songs. Almost.

A few things that we wanted to add were: 
  • A button on one of the sticks that when pressed, would play a pre-recorded drum roll.
  • A crash cymbal sound when the hi-hat stick is swung a bit harder [the code was also written, but it was still a bit fickle and we could never get it right, so that one line is commented out].
  • A more responsive bass pedal.
  • A ride sound when the stick is swung in the X direction. This we could never get right because it was happening even when the stick was swung in the Z direction.  

And that's it, so try it out and if you have any questions about the code, about the concept, criticisms on the blog or any feedback, really, please leave a post or send us an e-mail at

Sorry I couldn't post a demonstration video.. None of us are drummers, all of us are guitarists, actually, we'll make one as soon as we get our hands on a kickass drummer. :D
Thanks for reading and happy drumming!!!

- Bhargav

PS: If you have an interesting project or idea, or recommendations for our next project, do share it with us, we would love to hear it!

Wednesday, 11 January 2012

The Arduino Air Drums Part 1

The summer before, as all of us were getting excited to start our specific engineering courses, Vincent got us all excited about a prototyping board called the Arduino,
 using which, microprocessor programming is made really easy! They have their own simplified coding language based on C and the editing platform is up for free download. Read it all up on their website:

And one of the videos that caught our eye was mmidgal's Arduino Air Drums.

He had constructed a 3 piece drum kit with 4 sounds using a MIDI interface, 3 accelerometers, a Photocell, some USB ports, two garden rakes and a couple of slippers. All in an afternoon, according to Gizmodo. [which we SERIOUSLY doubt because it took us a little over a week]

Shyam came up with the idea a couple of weeks before our semester end exams, and I was a little skeptical at first, but then I remembered my dream of always wanting to play kickass drums. Because, lets face it, drums are cool. Maybe not as cool as guitars, but still, pretty cool.

So, on day 1, Shyam and I make our way to Malleshwaram, to meet at Akshay's house. We decided everything that we needed to get, and set our budget at around 4k and left to
SP Road. 

So, here comes the slightly technical bits: 
Stuff we bought were: 
1. The Arduino Uno
2. ADXL335 accelerometers     [x3]
3. USB cables      [x4] 
4. USB females    [x8]
5. 5 pin DIN connector 
6. A no-company-comes-without-a-box MIDI to USB converter for Rs. 8oo, because the M-Audio Uno that the mmidgal recommends is 2.7k. lol. 
7. A bunch of Photocells (they cost about Rs. 5 a piece, so we thought we'd build a keyboard later with them)

All these items we bought, solely on the recommendation of the video and we didn't realize that some of them might be not necessary. Like the DIN connector and the MIDI to USB converter cable. :|

Okay, before we continue, a little introduction into MIDI: 

MIDI (Musical Instrument Digital Interface) uses a form of serial to communicate at a data rate of 31250 bps with each byte made of 8 bits plus a stop and start bit. MIDI commands are most often three bytes in length but sometimes more or less. The Serial-MIDI converter currently only handles three byte MIDI commands.

The first byte is called the Status byte and contains both the type of MIDI command and the channel number. The high nibble of this byte tells the MIDI device what it is doing, such as Note Off, Note ON, Control change etc... The low nibble is the channel, it is used to differentiate between devices that are on the same MIDI bus and varies from 0 to 15.

The second byte is the Pitch byte and is simply the note being played, stopped, altered etc... This byte is in the range of zero to 127.

The third byte (when used with Note-On or Note-Off) is the velocity, which is a form of volume for the note being played. Imagine that you hit a drum softly, then the velocity would be a low number, but if you really whaled on it then the velocity would be higher. This byte is also in the range of zero to 127.

And a little bit about the ADXL335 accelerometers: 
 Breakout board for the 3 axis ADXL335 from Analog Devices. This is the latest in a long, proven line of analog sensors - the holy grail of accelerometers. The ADXL335 is a triple axis accelerometer with extremely low noise and power consumption - only 320uA! The sensor has a full sensing range of +/-3g.
There is no on-board regulation, provided power should be between 1.8 and 3.6VDC.
Board comes fully assembled and tested with external components installed. The included 0.1uF capacitors set the bandwidth of each axis to 50Hz.

So all that it is is a little chip with an IC and a few capacitors on it. There are 6 terminals;
Self Test (ST)

Which are pretty self explanatory, basically the value of the at the three output pins change when motion in their respective axis is carried out. And about the Self Test pin, the data sheet had this to say: 

The ST pin controls the self-test feature. When this pin is set to VS, an electrostatic force is exerted on the accelerometer beam. The resulting movement of the beam allows the user to test if the accelerometer is functional.The typical change in output is −1.08 g (corresponding to −325 mV) in the X-axis, +1.08 g (or +325 mV) on the Y-axis, and +1.83 g (or +550 mV) on the Z-axis. This ST pin can be left open-circuit or connected to  common (COM) in normal use. 

The acclerometer that we used were built by NSK Electronics, and the board said that that the supply voltage should be 5V. We rarely experienced any overheating problems and pretty faithful responses to movement. One gripe that we did have was when we moved the accelerometer in the Z direction, there was a change in signal coming from the YOUT pin which made it hard for us to program the ride sound. 

Okay that's all cool, but how the hell are we going to connect them to the Arduino?!? 

The cool thing about the Arduino Uno is that it has a set of analog and digital pins that can be used for either outputs or reading inputs! A very simple example is illustrated in the Arduino website: Analog Input Digital Output.

All we had to do was hook up the 5V pin and GND pin on the Arduino to two lines on our breadboard and use those to feed our Accelerometers. 
The readings from our Accelerometers have to be fed into the analog inputs on the Arduino to be read, so, we chose the Z axis as the one we're going to use for everything and hooked those up to the bread board and the Arduino. 

[Sorry about the messy diagrams, we're still learning how to use Fritzing.]

So now we have our inputs for the snare sound, the bass drum sound and the hi-hat sound.
Lets add a photocell in a voltage divider configuration with a 47k resistor to help us decide whether the hi-hat accelerometer is going to play closed hi-hat or open hi-hat.
Here is a neat instructable explaining how the voltage divider works.

So that's, basically our circuit. We made use of USB cables and connectors to help manage all the wires, otherwise it would have been a HUGE mess!
Be sure to remember the colours!

And be sure to label them too!

Secured the accelerometer to the drum sticks using lots
and lots of sticky tape. Lots. 

And buy plenty of different coloured wires, trust me, they
are super useful. 

Once you're done with the sticks, your circuit will look more or less like this: 
The sticks we used were Tama Rhythmic Fire size 7A sticks.

And hopefully you wouldn't look like: 

All that's left are the 'pedals' and programming, which I will be covering in the next blog post and we will hopefully be done with the demonstration video!!