Introduction: Arduino Barrel Piano

About: klaatu barada nikto

Hello! That's the story of how I made my automatic barrel piano on the Arduino.

This project was done exclusively for fun and does not imply the real use =)

Step 1: Three Composition Demo.

  • "The Entertainer" by Scott Joplin.
  • "Hedwig's Theme" from the "Harry Potter" movie, arranged by Patrick Piesman.
  • "The Black Pearl" from the "Pirates of the Caribbean" movie, arranged by Klaus Badelt.

Step 2: A Bit of History

The idea of automatic musical instruments isn't new at all. People always tried to automate various musical instruments, including the piano. There were different automatic pianos from the mid-18th century until the invention of the gramophone, for example, a barrel piano (pic. 1), a player piano, also known as pianola (pic. 2) or a barrel organ (also known as a street organ or hand-organ). These devices were automated using a pneumatic or mechanical system. Pianola has a driven shaft, with protrusions located in a specific sequence, according to the notes of the musical piece. The protrusions of the shaft sequentially drive the mallets corresponding to the keys of the pianola and thus extract musical sounds. Inside of a barrel organ, there are sounding tubes, bellows, and a wooden or metal roller with spiked fists placed in several rows (pic. 3). Turning the knob, the organ-grinder could play 6-8 melodies recorded on the roller. In the late XIX - early XX centuries, more advanced tools appeared with keys controlled using perforated paper tapes.

Step 3: Reading Notes

The first task is to figure out whether the key is pressed or not.

Old musical instruments use rather complex mechanisms with shafts projections and valves. I can use Arduino and electronic sensors to get the presence signals of a particular sound; for example, optocoupler sensors also called line sensors. Optocouplers are used in the Arduino world for line racing. These sensors allow you to determine the white or black color of the surface below it.

Here is the idea (pic. 1). Let the line sensor represent one piano key. Place several white and black areas on a sheet of paper. Then, move this sheet relative to the stationary line sensor. If there is a white area of the sheet below the sensor, then the key is released. If a sensor detects a black area, then the key is pressed. At the moment when the key goes from the released state to the pressed state, a note begins to sound.

With multiple optocouplers at the same time, you can create a kind of a piano keyboard where each optocoupler corresponds to its note. By positioning black and white areas on the sheet in a certain way and moving the sheet at the desired speed, you can get the note sequence of a musical piece.

I made a keyboard consisting of 40 keys. For this, I used five Octoliner modules. The Octoliner module is a small array of optocoupler sensors. Each module has eight TCRT5000 optocoupler sensors, so one module can be used for 8 different piano keys. This module runs through the I2C bus, and it is easy to link it with Arduino.

I decided to place five modules next to each other to create a single vast linear array of 40 (5x8) optocoupler sensors. You can make a keyboard from fewer keys or make a full-size piano keyboard with 88 keys. I chose 40 because this is about half of all the keys on a real instrument.

Step 4: Arduino As a MIDI Device

To read signals from all line sensors, I use Arduino. But sensors can only inform the Arduino whether a key is pressed or released. To generate the sound of notes, I turned the Arudino into the MIDI device.

MIDI is a universal digital recording standard for exchanging data between various musical instruments. MIDI devices exchange messages which are encoded data in digital form. These messages contain information about the keys, volume, tonality with a precise time reference.

As a MIDI device, Arduino can translate sensor data about the keys pressed to the MIDI messages. Besides, I chose an Arduino board that can act as a USB HID device. By transmitting prepared MIDI messages via the USB port, the Arduino turns into a real electronic musical instrument, primarily an electronic piano keyboard. With the USB HID device emulation and MIDI messages, I'm able to get the sound from any computer synthesizer by merely connecting the Arduino to the USB port.

I chose the Arduino MKR Zero board. It can work as a USB HID device out of the box. This board is fast enough to process many piano keys, and a large amount of flash memory is perfect for storing presets of many musical compositions.

In addition to the components in the diagram, I also used a momentary button to switch presets of musical notes and a 16x2 sign-generating I2C display to show the current preset's name.

When I clarified all electronic components, I visualized the connecting an wiring scheme (pic. 1).

Step 5: Barrel Piano Design

At this step, I attached the bill of materials and components that I used. Also, I attached a 3D model of the barrel organ assembly. I won't go into detail and describe the entire building process. There are quite a lot of parts, so it is easier for you to take a look at the 3D model and the BOM list. To produce the parts, I used laser cutting, 3D printing, plexiglas, and the materials and mechanics that I had at home.

I divided the design into two main mechanisms and called them conditionally puller and straightener =). Both mechanisms are attached to the base plate. The straightener is attached to the base by the M3 spacers 65mm long and the puller by 3D printed corners.

Fasteners are not shown at the assemble diagrams but are in the table.

Purchased components links are at the BOM file. The 3D assembly flle is in parasolid format, so you can use almost any CAD software to open.

Step 6: Straightener Assembly

Straightener holds octoliner modules. It is used for reading notes and aligns the paper music sheet.

The music sheet is wide and may bend or tear during movement. Each note track on the sheet should be right under the corresponding optocoupler of the sensor. Therefore, the music sheet should be aligned before sensor reading. The paper sheet passes over the straightener base plate and is pressed by the cover plate. A small clearance of 1-2 mm should be between the base plate and the cover plate. On the sides of the base plate, two border plates restrict the incorrect movement of the sheet.

Five octoliners are fixed at the octoliner plate. This plate is installed at a certain height above the sheet and attached to the base plate by four spacers M3 6mm.

Step 7: Puller Assembly

The puller is designed to drag the music sheet after the straightener.

Four rubber wheels tightly clamp the music sheet on the shafts. When rotate the rubber wheels pull the sheet out of the straightener. The puller is fixed at the base plate by 3D-printed corners,M3 screws and M3 hex nuts.

In general, the puller (pic .1) consists of three plexiglass plates (puller plate 0, 1, 2) and three 6mm shafts(60, 440, 480mm length) between them. Shafts are fixed in plates with F626ZZ 6mm bearings. Bearings are fixed with 3D-printed 6mm bearing covers, M2 screws and hex nuts.

Shafts rotate by the handwheel via the belt drive. I used a simple belt drive with the 2GT belt without tension. This belt and its pulleys are trendy in hobby CNC machines and 3D printers, so they are easy to find and buy. The small pulley is the purchased one GT2 20 6 B6. The larger pulley is self-made, made of laser-cut plates (pic. 4). The larger pulley consists of 4 plexiglass plates (belt pulley plate, belt pulley plate cover) with a thickness of 2 and 4 mm and the metal 6mm shaft flange. I used a 6mm wide closed belt with 250 teeth - 250-2 GT-6.

The handwheel (pic. 2) consists of the handwheel plate, F625ZZ 5mm bearing, and 3D-printed handle. The handle is fastened to the bearing by the DIN 912 M3 30mm screw and M3 nut. The bearing is fixed at the handwheel plate with a 5mm bearing cover, M2 screws and hex nuts. The assembled handwheel attaches the 6mm shaft through the 6mm metal shaft flange.

Four rubber wheels that pull the sheet (pic. 3) are also composite. Each wheel consists of 4 plexiglass plates with a thickness of 3 and 2 mm (rubber wheel plate, rubber wheel cover plate) and the 6mm shaft flange. The rubber sealing ring is stretched on the wheel assembly. I used the O-ring for the groove with the 50mm inner diameter. You can use various suitable o-rings with diameters from 5.3mm to 5.8mm. For example, 50,00 x 5.30 according to DIN 3771 standard or P50A, according to JIS or AS568–329 according to AS568 standart.

Step 8: Shielding Sensors

There are a lot of IR sensors in the design, and they are very close to each other. When multiple sensors operate simultaneously, their IR signals are repeatedly reflected and intersect with each other, and therefore cause significant errors in the received data. You can avoid this by shielding each optocoupler on every sensor. For this, I 3D-printed small collars that fit on the TCRT5000 optocouplers.

Step 9: Transcribing Basics

Preparing the music sheet is probably the most tedious step in making a barrel piano, especially since I don't have any musical skills =).

So, a single octoliner module has eight optocouplers (channels) responsible for the read of 8 notes. Octoliner length is 80mm. Placing 5 modules next to each other, I get a full length of 400mm. I add another 5mm of free space at the edges. Thus, the total width of the music sheet is precisely 410mm.

The width of one track of the note equals the width of one optocoupler and is 5.8mm (pic. 1). There is some distance between optocouplers on the sensor - 4.2mm. The same distance is between note tracks on the music sheet.

As the source for barrel piano notes, I use real notes of musical compositions. Before creating notes, I strongly recommend you to get acquainted with the musical notation and the list of musical symbols.

Notes are divided according to the duration where the duration is the fraction of a beat. Notes can be whole, half, quarter, eighth, sixteenth, and other.

As a basis, I take the sixteenth note duration and assign 10mm height for it on a barrel piano sheet. The entire sheet of music can be lined with horizontal lines with an interval of 10mm since this is the minimum note step.

A black 10x5.8mm rectangle on a track produces the sound of a note with a sixteenth duration. So, a rectangle with a height of 20mm is the eighth duration, 40mm is the fourth, 80mm is the half, and 160mm is the whole.

In the MIDI format, each note of the piano keyboard has its unique number. These note numbers can be assigned to the octoliner channels.

How to transcribe the notes?

I show you an example of a simple melody that I found on the Internet (pic. 2). It is called "Rain rain go away" =) (c) www.teaching-children-music.com. I've formed several steps for myself to make the barrel piano sheets and here is the order:

  • Step one. Look at all the notes that are used in the musical piece and write down their MIDI numbers in ascending order. This melody uses only three notes of the first octave: E, G, and A. The E note of the first octave has MIDI number 64, G has number 67, and A has number 69.
  • Step two. Assign notes as well as MIDI numbers to the octoliner channels. For example, I assign the first octoliner channel with the 64 number, channel 2 with the 67 number, and third with 69.
  • Step three. Take a look at the beat duration and remember it. This melody is 2/4. It means that the length from one bar to another equals to two notes with a quarter duration. So, the height of one beat on the sheet is 40mm (2*80mm). The full melody has 4 beats, and I can place bars on the sheet.
  • Step four. Transcribe the music piece. Place rectangular black areas on the sheet according to the duration of the notes.
  • Step five. Prettify the sheet and remove the issues. For example, this melody has two identical eighth notes sound sequentially at the second, third, and fourth beat. On the transcribed piece, these two eighth notes merged into the single fourth note. Separate notes them by slightly shortening the duration of the first note of the pair. For this, reduce the height of the first rectangle, providing a gap.

Step 10: Transcribed Pieces

  • The width of the sheet is 410mm (calculated at the previous step). All the music sheet is too long to fit a single sheet. To fit all notes, I print them partly on several A1 sheets. Then cut the excess at the edges of the sheet to 410mm. Then I glue sheets together in one big patter.

In total, I transcribed three music compositions.

1. "Hedwig's Theme" from the "Harry Potter" movie, arranged by Patrick Piesman.

MIDI notes used:

  • 41, 42, 43, 45, 48, 49, 50, 52
  • 54, 55, 56, 58, 59, 60, 61, 62
  • 63, 64, 65, 66, 67, 68, 69, 70
  • 71, 72, 73, 74, 75, 76, 77, 78
  • 79, 80, 81, 82, 83, 84, 85, 86

2. "The Black Pearl" from the "Pirates of the Caribbean" movie, arranged by Klaus Badelt.

MIDI notes used:

  • 31, 33, 34, 36, 38, 40, 41, 43
  • 45, 46, 48, 50, 52, 53, 55, 57
  • 58, 59, 60, 61, 62, 63, 64, 65
  • 66, 67, 68, 69, 70, 71, 72, 73
  • 74, 76, 77, 79, 81, 82, 85, 86

3. Original "The Entertainer" by Scott Joplin.

MIDI notes used:

  • 43, 44, 45, 47, 48, 50, 51, 52
  • 53, 54, 55, 56, 57, 58, 59, 60
  • 62, 63, 64, 65, 67, 69, 71, 72
  • 74, 76, 77, 78, 79, 81, 83, 84
  • 86, 87, 88

Here, I attached original piano notes, my transcribed notes for the barrel piano, and an empty A1 lined CAD sheet that you may need to make your compositions.

Step 11: XOD and Libraries

To create the program for Arduino, I use the XOD visual programming environment. I often use XOD in my projects, and this one is no exception. Especially since all the libraries needed for the project are already made. If you want to follow the instruction or try to do a similar project using XOD, then take a look at these libraries:

amperka/octoliner

The library from the Octoliner manufacturers to work with the module. It contains all necessary quickstart nodes to work with sensors as well as some advanced features like the brightness/sensitivity tune of the optocouplers or I2C address change.

e/midi

e/usb-midi

e/serial-midi

These are awesome XOD user libraries. They allow you to work with the MIDI format in XOD. The e/midi library is to create custom MIDI messages easily. With the e/serial-midi/ and e/usb-midi/ libraries, you can exchange prepared MIDI messages accordingly via serial interface and the USB port of Arduino.

The entire project patch is located in the library, you can add it into your XOD workspace:

gabbapeople/barrel-organ/

Step 12: Programming. Main Patch.

The main patch barrel-organ starts with the initialization of sensors. To initialize the sensor devices, use the octoliner-device node from the amperka/octoliner library. Each device has its I2C address. You can change the I2C address of the octoliner using the example in the amperka/octoliner library. For each device, the set-brightness node sets the brightness value 1 of the IR emitters. Then the set-sensitivity node sets the sensitivity value 0,9 for the IR receivers. Octoliner devices are divided into separate buses DEV1, DEV2, DEV3, DEV4, DEV5. The usb-midi-device node creates the value of the custom MIDI type that is used to generate and send MIDI messages. On power, there is a delay of 1 second. After the initialization of all parameters, the gate node starts to pass continuous pulses.

The program has a "track" counter. Each track corresponds to a specific preset of musical notes. Presets are switched using the track-charger node and button. The text-lcd-i2c-16x2 sign-generating display shows which preset is currently enabled.

Next is the keyboard node. This node takes in the octoloner device buses and midi device bus as well as the current track value of the preset.

Step 13: Programming. Keyboard Node.

The keyboard node is very large, it is a heart of the device.

The keyboard node structure starts with the device buses and the bus of the current track.

Then there are five octoliner-read-channels nodes. One this node reads eight analog signals from eight optocouplers on an octoliner. The signals are read sequentially from each optocoupler and digitized using a simple formula. If a signal >= 0.7, the key is pressed; if not, it is released. In total, octoliner-read-channels nodes have 40 channels, and output 40 boolean values numbered from CH0 to CH7.

Next, there is a midi note number selection part. One channel corresponds to different notes when playing different musical sheets. Selecting a note for a particular channel depends on the current track. The note-switcher node selects the desired MIDI number depending on the number of a track. Graphically, the distribution of notes is organized by buses with different names. The xor nodes cut off unused buses for the current track.

The received signal of a specified midi note goes into the octave node. I made six octave nodes from 1 to 6. These nodes correspond to six octaves of the real piano. Each octave node consists of twelve input pins for twelve midi note numbers. For example for the fourth octave numbers are 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71. On any change at the input, the octave node generates and sends the MIDI message via the USB port.

Step 14: Sound Output.

A ready-made device can be connected to any synthesizer. For example, you can connect it to your computer running on Windows OS and use Synthesia app. Or connect it to a RPi and output audio using popular QjackCtl & Qsynth.