Build a piano#
Using hardware.#
MAX98257A
8ohms,0.5W speaker https://www.adafruit.com/product/1890#tutorials
a lot of push buttons
from IPython.display import Video
Video("piano.mp4")
MAX98257A#
It takes standard I2S digital audio input and, not only decodes it into analog, but also amplifies it directly into a speaker. I2S (not to be confused with I2C) in a digital sound protocol that is used on circuit boards to pass audio data around. By changing the connection of the Gain pin of it you can acutally change its sound,the default is 9db, the documentation is https://learn.adafruit.com/adafruit-max98357-i2s-class-d-mono-amp/pinouts
Connect the circuit and try the example code#
I2S ID |
SCK pin |
WS pin |
SD pin |
---|---|---|---|
0 |
16 |
17 |
18 |
Pico |
MAX98357A |
---|---|
GP16 |
BCLK |
GP17 |
LCR |
GP18 |
Din |
VBUS |
Vin |
ground |
ground |

Don’t forget to connect the ground and Vin of MAX98357A. We use VBUS becasue we need high power supply when the sound go high.
build the circuit and run the example code to produce a 440Hz sine wave. (you will need to update your micropython to 1.19 above version to support I2S) miketeachman/micropython-i2s-examples
understand the code#
rate
is the speed of sample rate. It is the speed DAC can tranfer a number to an output voltage. You can also understand how many data points dac can give you in 1 second.
frequency
is the frequency of the sine wave we want. For example if we want a 440Hz sine wave, and the rate
is 22050. We have 22050 sample points in 1 second, and those points must form a 440 period of sine wave. Then sample points in each circle is 22050/440.
Class C amplifer#
MAX98357A contain a class C amplifer. using an oscilloscope to see the waveform with small and large horiaontal scale
I2S#
In the example code, entire sine table was writen inside a buffer. Then pico automatically sends those data through I2S (by using DAC). You may find if you add a 2ms sleeping period in the while loop, the sounds may not changing, acutally you have 1/TONE_FREQUENCY_IN_HZ free time each while loop to do other things. Try that.
Build a simple piano#
Build a piano with 12 keys. When the first key is push down, the speaker output 220 Hz sine wave. The next key is 220 * \( \sqrt[12]{2}\), So and so next. Write you own code and connect the circuit. You may get something like

(person tips )
Based on the provided example. You need to using for loop to initialize four list. First list stores the frequency value corresponding corresponding to each keys. The second list store 12 int value, their value is either 1 or 0 to represent if corresponding keys is pushing down. The third list is contain the sine table for each freqenecy. The forth store 12 Pin object.
Resonance Frequency#
It can be measured that the output of the amplifer has the same AC rms. Listen to sound of each key carefully and write down what do you find. Write about why you think that happened.
Interupt#
Learning how to use interpt with mircopython. A good tourial is: https://www.coderdojotc.org/micropython/advanced-labs/02-interrupt-handlers/ .Add another button double the frequency of each button.