Introduction: 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:
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
Step 1: 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.
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
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.
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
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.
Attachments
Step 4: 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.
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”.
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”.
Attachments
Step 6: 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.
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:
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.
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
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.