In this project you build a tiny synth that can play four notes: C, D, E, and F. One interesting thing to think about is that your microcontroller only has about 13 pins it can use as input/output. So what do you do if you need to build something that requires you to read input from more than 13 sources—say, an 88-key electric keyboard, for example? Well, I’m sure there are a whole bunch of ways to solve the limited number of input pins problem, but one of the techniques you learn about in this project is called a “resistor ladder.”

Although our keyboard takes input from four different buttons that each play a different note, all the buttons are hooked into the same analog input pin. So how can we use a resistor to inform our single analog pin which button is being pressed? By connecting different-strength resistors to each button, the total voltage read by our analog pin will change depending on which button is pressed. If we press a button with a strong resistor, we’ll get a low voltage reading; a button with a very low resistor will give a high voltage. We can then “bucket” the voltage levels that our pin reads into corresponding buttons and their associated notes to be played.

This project also used a piezo in the same way as the last project, but this time we only played four discrete frequencies: C, D, E, and F. I still haven’t found the “correct” way to play a frequency on the piezo, as I think some of the code that runs to read the analog pin may be offsetting the delay between toggling the pin and, therefore, the frequency. However, by inserting a loop with the delay so that the logic for reading the analog pin runs less frequently, I managed to get a better tone. I used my phone’s tuner app and confirmed I was getting the right notes.