? How I won the CircuitPython Challenge with a Piano ?
About a month ago IoT Makers Israel published a challenge I really enjoyed. The challenge was sponsored by Adafruit and the goal was to make a fun project using an Adafruit board. I really love creative challenges! So, I tried to come up with an idea for a fun project while exploring the capabilities of my board.
If you are not familiar with CircuitPython - it is a version of the Python programming language designed to simplify experimenting and learning to program on low-cost microcontroller boards.
The board I used for the challenge is Adafruit Circuit Playground Express. It supports CircuitPython out of the box and includes many sensors, RGB LEDs, and a small speaker:
This board has 7 pads that can act as capacitive touch inputs and I decided to use them for the challenge.
I had many ideas and I even started making one:
Due to COVID19, every public place had been closed and my gym was one of them. I wanted to enjoy exercising at home, so I made a game with the board: I defined 4 colors for the NeoPixels and whenever the NeoPixels turned on, I had to press a certain capacitive touch pad according to the color. I connected pieces of aluminum foil to the touch pads using alligator clips and made a platform for each of them with its matching color:
I found many ways to exercise with this game, such as placing them at a distance and running to press the right color. But when I finished this project, I felt it wasn’t as exciting as I thought it would be and I knew I could do better. It did help me experiment with the capacitive touch pads and then it struck me –
Could I create a big piano like in the piano scene from the movie ‘Big’?
I was very excited about this new idea, but I had two problems: First, I don’t know how to play the piano. Second, CircuitPython represents notes as frequencies, and I didn't know how to convert between the two.
I found 2 solutions:
- Standalone version using the method explained in this blog post by Uri Shaked to convert note names into frequencies.
- Use the board as a MIDI controller, so it sends the notes to the computer, and the computer will synthesize the sound and play it on the computer's speakers.
I decided to try the second option and used Adafruit’s documentation to understand how to use the board as a MIDI controller.
I learned that I had to use an external service to achieve the desired functionality, because it turns out that simply connecting the board to the computer and sending MIDI messages does not make it play the actual sound.
‘MIDI messages between different USB devices are not automatically forwarded by the operating system. An application is required to forward or route the messages.’ (MIDI routing)
And as I mentioned before, the board has 7 capacitive touch pads, but I needed 12 notes to play the song from the movie, so I used a second board. I connected each board to a different computer, because I couldn’t make the boards work on the same computer at the same time.
The Code
Good programmers don't like reinventing the wheel, and luckily I found a code example that did exactly what I needed with just a few tweaks. The more challenging part was to figure out how to put everything together:
- I downloaded the Adafruit Libraries, extracted the library Zip file and copied a directory called 'adafruit_midi' onto the CPX CIRCUITPY drive in a directory called /lib.
- I copied their code from ‘cpx-expressive-midi-controller.py’ into the code.py file.
- I repeated the first 2 steps on my second board, and I made a small change in the code.py file - I set the value of the variable midi_note_C4 (line 53) to 48 instead of 60, because in the documentation they mentioned that the ‘jump’ between the octaves in the keyboard is 12.
- For the application part, I used Pianu, which gave me the ability to convert the MIDI messages from my board into piano sounds, and also visualize them on a virtual piano keyboard.
As a nice bonus, they also have tutorials for different songs and I found ‘Heart and Soul’, which is the song Tom Hanks played in the movie! So, it solved my other issue as well! ?
From Capacitive Pads to a Huge Floor Piano
The next step was to create a ‘body’ for the piano and connect it to the capacitive touch pads of the Circuit Playground Express boards.
I had a white foam board, a black marker and metal skewers. I used alligator clips to connect the capacitive touch pads to the metal skewers.
After measuring, painting and carefully inserting the skewers to the foam board, I noticed that I didn't even need to touch the metal skewers for the board to detect my presses. It was sensitive enough to detect a press through the foam board, which was great as it let me hide them inside the piano!
The capacitive touch pads were a bit too sensitive, so I had to make sure the alligator clips didn’t touch each other, and I placed the piano and the boards on a thick foam surface to keep them from affecting each other.
And that was it! I had a working DiY Piano ?
For those of you who are curious - The prize I won is PyPortal Pynt. Another great board from Adafruit, that I can’t wait to use ?
Thank you IoT Makers Israel for this great challenge!