Introduction: Electronic Music Box Powered by Arduino (sort Of)

Picture of Electronic Music Box Powered by Arduino (sort Of)
Music is the most universal means of expression. Regardless cultural language or age the idea conveyed though music would not differ much. It is safe to say everyone loves music, one type or another. When one’s favourite piece is played there is a overwhelming desire to get involved and turn on one’s Air Guitar.
Not being familiar with any type of musical instrument myself. Moving my hand insanely in the air and imagining my virtual tone lines up perfectly along the music was the best I could do.
Why not put my engineering degree to use. Design something able to play music without any training, minimal music knowledge but still get that person highly involved.
Music box seems like a good start. The music is actually ‘played’ in real time, unlike mp3 music player which merely convert a string of data to time varying voltages. However the music storage method has to be changed, a drum with pins poking out does not allow much customization. If the music is stored on file we are back to the mp3 player, but if the music is on a piece of paper…..Now we are talking.
Hence the Electronic Music Box was conceived.
What you will need:
2 proto boards or PCBs
LM7805 5V regulator
ATmega328 or Atmega168 microcontroller
16 MHz crystal with 2x22pF cap
Or forget about the two above if you use an Arduino
3 X LM324 opamp
14 X IR LEDs
14 X IR photodiodes
10K potentiometer
50k potentiometer
3.5mm headphone jack
MCP4911 10bits DAC
20 ways Right angle male heading connector
20 ways Female header connector
Various capacitors and resistors
Optional, mechanical:
6V motor with gearbox ~ 30RPM
Rollers, more on that later

Youtube link here

Step 1: Preliminary Design

Picture of Preliminary Design

How is the music stored on a piece of paper?
The simplest way is to divide a piece of paper, a certain width, into equal width columns and each represents a musical note of some sort. Kind of like musical notes on sheet music.
Notes are marked by pen and the duration of the note, or break, is controlled by the length of that mark.
The paper with music notes should look something like the photo below.
How does the unit read this paper?
A simple vision would be useful. Infrared, less likely to be distorted by our normal lighting is a great solution. Like visible light the dark mark on the sheet absorbs IR light while the blank passes most of IR through.
With this in mind the only thing we need to do to differentiate the two is a level comparison on the IR photodiodes.
How do we generate musical tone?
Since is fully electronics, the best tone generation method is that employed by mp3 players. Using a fast DAC or digital to analog converter to create the right voltage at a given moment, we can get a pretty smooth analog tone.
This method also allows any kind of waveform to be generated. It opens the possibility of polyphony.

Step 2: Circuit Theory

Picture of Circuit Theory

The actual circuit design is not complex at all, only a lot of repetition. My design is split into two parts: Infrared sensor array and the main board.
The IR gate array is pretty simple. It’s just made up of a roll of 14 IR LEDs with a roll of 14 IR photodiodes facing each other. Why 14? Well it covers 2 Octaves and that is all I can manage to fit on that protoboard.
The IR sensors array provides 14 voltage levels according to the sheet and the circuit has to identify the marked notes. Thus we will need voltage comparators and that is where LM324 comes in. Strictly speaking LM324 is operational amplifier not voltage comparator but it can do the job just fine.
The choice of this IC is not based on its performance. Single supply, from 3V, having 4 amplifiers in one package is the main driving reason for the choice. Low power and low cost are also great bonus.
After the LM324 arrays convert the 14 IR gate signals to the simple HIGH or LOW digital signals. Then microcontroller takes over and does what it does best – calculation.
Based on the incoming signal, the microcontroller looks up on the waveform tables stored in its flash and grab the right voltage level appropriating to the moment in time. Since everything is software achieving polyphony is fairly easy. It is simply addition of all the voltage data.
The micro then passes this information to MCP4911 10bits DAC for the analog signal generation.

Step 3: Construction - IR Gate Arrays

Picture of Construction - IR Gate Arrays

This is a very simple part of the build but absolutely essential. The spacing between the IR LEDs must be kept constant. I am using a proto board with a standard pad spacing of 2.5mm, thus each LED is 5mm apart. A slot of 75mm is milled between the LEDs and photodiodes. This slot will allow the music sheet to pass through and be read. This board will be connected to the main board via header connectors. A row of 20 way right angle male header, not those straight ones in the photo, should be soldered and connected to the LEDs and photodiodes as shown in the circuit diagram. There is a reason I didn’t specify the value for the resistor R1 – R14. The value of these resistors will be our tuning points later on, at the moment use 100Ω for every one of them.

Step 4: Construction - Main Circuit Board

Picture of Construction - Main Circuit Board

Building the main board has no drama. You can choose to have an ATmega328/168 with Arduino bootloader on the main PCB, like the one I have. Or you can build the circuit without the microcontroller and its crystal to suit the Arduino board of your choice.
I have included the EAGLE files of the project, schematic and PCB layout is all done using the free version. You can make any change to the design as you want.

Tune the circuit
Since these cheap IR LEDs and photodiode are not made to the tightest tolerance, the variation will be significant enough to cause some false detection.
To correct this you will need a multimeter. Connect the IR gate arrays to the main board and power the system up through the power jack socket J1. Use your multimeter to check the voltage across the resistors R7 – R20. Their voltage should be more or less the same. If one is higher than the majority by about 100mV the resistor value for the according IR LED should be increase to offset the difference. If one is lower, then lower the corresponding resistor value should equalize it.
With the IR gate arrays tuned, now pay attention to the potentiometer for voltage comparators. In my design there is only one such pot, all the non inverting inputs of every opamp is wired to this potentiometer. This potentiometer should be tuned so that the opamp outputs LOW with a piece of white paper in the IR gates and when a marked piece of paper is presented the output should be HIGH.
If you have done all these correctly, your circuit should be ready. It is time for the most important bit of this project.

Step 5: Code

Here is where the magic happens. The code is not complex but lengthy with fair amount of repetition. Large portion of the code has rough comments describing the function of a section of code.
Quite a bit of the IO controls in the code are not written in standard Arduino code rather AVR C code. The reason is - speed.
What appears to be a single line in Arduino code, for example digitalRead(), it is actually a library call made by the compiler, which would take the processor up to 5 microseconds to do a simply digital pin read command. And that is far too slow for reliable operation where timing is essential.
On the other hand the low level AVR C code is translated into only a few lines of assembler. The same action would clock in at less than 80ns instruction time. Less time waiting for the input data gives the processor more time the process sound wave, much more reliable performance.
The code runs through every 45 microsecond, giving us 22.2kSPS. An integer variable is assigned to each music note, which increment every cycle until that note is no longer registered by the IR gates. The corresponding voltage level is pulled from the wave table based on its variable. After it’s been through the calculations the data is passed through the SPI bus to the DAC to be converted.
I have done some alterations to the code recently to expend the octave range in which the system covers. Now the most right side IR sensor PD14 is designed to flags the up shift of the entire sheet by an octave. For example if you want to get note C6 you will mark the sheet on “C5” and “Up”.

Step 6: Put It All Together

Picture of Put It All Together
Plug the IR gates into the main board. Print out the attached PDF file below. Cut the paper to size, join multiple strips if you see fit. Mark the note strip according to the music you wish to play.
Plug an earphone or speak with amplifier into the 3mm audio jack. Power up the system, feed the paper into the slot on the IR gate and slowly draw the paper through. You should be able to hear the melody you created from an instrument you made. The volume can be adjusted by turning the big potentiometer R1.

Mechanical feeder

This is not necessary for the operation of the project. But it does enhance the experience a lot. It will guarantee a steady speed at which the note sheet is fed through the sensor.
This part of the design is where your artistry shines. I am not a mechanical engineer and have limited mechanical design experience. I did just manage to make mine mechanically stable. Since everyone’s construction would be different my structural design may not fit all.
You may need:
  • A low RPM geared motor
  • Some sort of tubing that fits the output shaft of the motor as roller
  • Some fasteners
I picked up my motor from Jaycar Electronics. It is rated at 6V 30RPM, right voltage and slow enough for our application. For the rollers I used two large diameter ink pen refill (8mm * 80mm). You can use any tubing that has inner diameter large enough to fit on the motor shaft.
My pen refill is made out of plastic, too slippery to pull the paper properly. So I added a layer of heat shrink tubing to give it a bit of rubbery feel. I loosely secure another piece of the same pen refill tube right next to the motorised one to apply pressure and enhance grip. Both side of tubing is coupled to the main roller via elastic plastic and spring, as shown in the photo. You can salvage roller from an old printer to make this part perform better and look professional.
Everything is mounted on the board using pieces of plastic cut outs, bolts and nuts. The motor is wire to the 5V rail of the main board. Of cause you can wire the motor to a adjustable voltage regulator to control the speed at which the paper is drawn.
Enjoy your music.

Comments

JorgeS184 (author)2017-02-06

Hi! love it, I´ve just discovered it and I have a question. Will it work with other colors apart from black? I know color darkness affects the sound but how exactly?

Thanks!

h2osteam (author)JorgeS1842017-02-06

Hi Jorge

The colour of the note is only for the IR sensor to detect them. I guess any colour that absorb IR light enough for the circuit to pick up would work, lets say dark blue. As to colour changes tone, it doesn't. Either the machine 'sees' the note or not.

cjhhao1 (author)2015-01-14

Hello, I love your work, but there is a problem. I would like to ask MCP4911 10bits DAC is not necessary to use it? MCP4921 can not replace it? Want to give me an answer as soon as possible, thank you.

h2osteam (author)cjhhao12015-01-14

Hi there
I'm not 100% sure about your question. I'll answer it as to my understanding.
You will need a DAC, doesn't have to be the same part. Any DAC with SPI input can work with little to no alternation on the code.
The arduino atmega328 I'd used don't have enough spare IOs to generate analog signal by itself.

morini made it! (author)2014-11-01

I made it by ARM7 and photocells and a USB powered programmer

h2osteam (author)morini2014-11-04

Wow this is brilliant. way more professional looking than mine. Good work

martellyer (author)2013-12-05

Hi h2osteam,

I've been trying to create the music box powered by an Arduino but have run into a few problems. I was hoping you'd be able to answer my questions.

First of all, how is the music box powered? Where is the input voltage source to run the arduino and light up the IR LED's?

Secondly, where does the right angle male header fit into the IR array? From the pictures it seems that is a straight male header that is being used.

Finally, where do we use the 10K and 50K potentiometers? It seems that the main board just has one 10K potentiometer.

h2osteam (author)martellyer2013-12-05

Hi martellyer I am glad you are trying to build one for yourself. As to all your questions, they are actually all answered in my article. to make things easier, take a look at the images attached. 
Feel free to ask
Cheers

agis68 (author)2013-11-19

excellent instructable...

skepticaljay (author)2013-11-18

Very cool idea. Good Job.

AJMansfield (author)2013-11-17

The one complaint I have is that the different parts to Canon in D are actually supposed to be played together, in a round. The cello line is also supposed to be repeated continuously throughout the entire song. While I understand that doing that is likely beyond the capacity of the device, it still doesn't sound good having it mutilated like this.

A way to remedy this without adding additional hardware: Replace one of the tracks with a 'loop' indicator; it records all the notes played while the loop bit is set, and then plays them back repeatedly (in addition to further notes) until the recording is replaced by a new section.

h2osteam (author)AJMansfield2013-11-17

You are right. This canon does sound weird without the repeating loop. Like you said the repeating part is beyond the note range the device covers.
Also It is more of a novelty item than a real instrument.

stubbsonic (author)2013-11-16

Very cool.

A possible variation on this would be to have a MIDI version where instead of generating tones, it would send MIDI note on's & offs to a MIDI output (just saw one here a few days ago). There could even be a band that does an octave switch for more range.

h2osteam (author)stubbsonic2013-11-16

You can do that, it will actually be simpler in the code. accurate timing will no longer be essential.
But that kinda defeat the idea of a stand alone system. Actually you can replace the IR sensors with a keyboard and play music

stubbsonic (author)h2osteam2013-11-16

Understood. However the angle of hand-drawing something (besides notation) that a robot then plays back is kind of cool MIDI or not.

longwinters (author)2013-11-16

Great project I can appreciate the quantity of work here, when I think of feeding a paper strip I think cash register or calculator paper feed, it's ready made reliable and you could make quite a long tune.

h2osteam (author)longwinters2013-11-16

I had the similar idea using the thermal paper and a receipt printer.

TSJWang (author)2013-11-16

I love this! I want to try putting in a scantron to see what music a quiz makes.

h2osteam (author)TSJWang2013-11-16

Or you can put the music through a scantron see how many mark you get

Jestin_Cubetech (author)2013-11-15

great idea

About This Instructable

33,053views

347favorites

License:

More by h2osteam:Electronic Music Box Powered by Arduino (sort of)Electronic Music BoxClap and let there be light
Add instructable to: