SIK Keyboard Instrument a learn.sparkfun.com tutorial
Available online at: http://sfe.io/t502
Introduction
The SparkFun Inventor’s Kit (SIK) is a great starting place for learning about electronics, programming, and physical computing. We can combine some of the concepts found in the SIK Guide to make our own projects. In this tutorial, we will make a keyboard using parts found in the SIK.
We can use the Arduino to read the location of a touch on the soft potentiometer strip (“soft pot”) and use that information to play a particular note from the buzzer. While it may be small, we can divide the soft pot into 8 segments. That’s enough for a scale! In this case, we’ll use the C major scale.
Required Materials
From the SIK, you will need:
In addition, you will need a few tools:
- Scissors
- Masking tape
- Hobby knife
- Ruler
- Pen or Sharpie
- Cardboard (a small box is preferable)
Suggested Reading
Before continuing with this project, we suggest you be familiar with a few concepts:
Hardware Hookup
Prepare the Breadboard
Before connecting anything, we recommend separating the breadboard into its three parts. Use a pair of scissors to cut the adhesive backing. This part is optional, but it helps the breadboard fit into the project enclosure later.
Connect the Components
Once you are ready to connect the components, check out the Fritzing diagram below.
Polarized Components | Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction. |
Having a hard time seeing the circuit? Click on the Fritzing diagram to see a bigger image.
Once you are done, the soft pot should be sticking straight up from the breadboard.
Create the Keyboard
The “keyboard” is actually the soft potentiometer. We will divide up the soft pot into eight (8) segments, and the resistance we read with the Arduino from touching the soft pot will determine the key being pressed.
To create something that looks like a keyboard, place a piece of masking tape over the soft pot so it covers the entire length with some overhand (we will use the overhang to attach the soft pot to the box).
Place a ruler next to the touchable (silver) area on the soft pot. Starting from the breadboard end, mark every 6mm. Fill in or note that the first 6mm section is not to be used.
Starting from the breadboard side, write the note of each key in between the marks. We’ll use C, D, E, F, G, A, B, C.
Make the Project Box
While we can play our keyboard on the breadboard, it might be more fun to have it in a project box to add a little stability.
Peel the backing off the two pieces of the breadboard to which we attached components.
Attach the large breadboard piece to the side of the cardboard box with the soft pot lying flat on the box’s floor. Stick the smaller breadboard piece (the one with the buzzer) to the box’s floor opposite the soft pot. Stick the overhang tape on the soft pot to the floor of the box.
Fold a piece of tape over (into a loop), and stick it to the RedBoard.
Place the RedBoard in between the soft pot and top breadboard piece.
Use the scissors to remove the top of the box.
Use the hobby knife to cut a hole in the side of the box so you can pass the USB cable through for power.
Cut a large notch in the front of the box just above the keyboard. This allows for easier access to the keyboard.
The Code
To program the Arduino from your browser, select SparkFun RedBoard in the first drop-down menu in the window below, select the COM port associated with your RedBoard, and click Run on Arduino.
NOTE: For this example, we're hosting the code on codebender, which not only features code-sharing, but also allows anyone to upload and debug Arduino sketches from within a web browser. Plus, it works on lower-tech machines, like Chromebooks!
There are codebender plugins for Chrome, Chromium, and Firefox. Visit the codebender plugin page for help installing the codebender plugin.
If you would like to use the Arduino IDE instead, copy the following code into the editor and click the Upload button.
NOTE: If the Codebender embedded window did not show up, go here to see the code.
Code to Note
We combine the code from Part 10: Reading a Soft Potentiometer and Part 11: Using a Piezo Buzzer in the SIK Guide, and we recommend you read the “Code to Note” sections in each of those to understand how we are getting data from the soft pot as well as playing particular frequencies on the buzzer.
language:c
if ( (val > 10) && (val <= 160) )
{
return 'c';
}
...
We pass the findNote(int val)
function the analog-to-digital (ADC) value read from the soft pot. This information is stored in the val
parameter. If we touch the soft pot towards the breadboard end, it will produce a lower value than if we touched it at the other end. The ADC value on the Arduino can be between 0 and 1023 (inclusive). So, we would read a value close to 10 if we touched it on the breadboard end and a value close to 1023 on the other end.
Since we divided up the soft pot’s length into 6 mm segments, we also need to divide up the values we might receive from the soft pot. Let’s say we touched the soft pot on the third segment (the one we labeled “E”), and the ADC value was 296 as a result. The first two if
statements would return false, but the third one would be true, since 296 falls between 250 and 350. So, ‘e’ is returned as the note we pressed on the soft pot.
What You Should See
Once you have uploaded the code to the RedBoard, try lightly pressing on the soft potentiometer. You should hear some musical notes from the speaker!
Want to play a song? How about Twinkle, Twinkle, Little Star (‘C’ is the first ‘C’ on the left in this case):
C, C, G, G, A, A, G (hold)
F, F, E, E, D, D, C (hold)
What other songs can you play? Here are some ideas to get you started.
Troubleshooting
No Sound
Given the size and shape of the piezo buzzer it is easy to miss the right holes on the breadboard. Try double checking its placement. Additionally, check the wiring for the soft pot.
Can’t Press More than One Key
The soft potentiometer is incapable of detecting more than one press at a time. What happens if you try to press two keys at once?
Going Further
Taking the Next Steps
Now that you’ve built your keyboard and had a chance to play a song, we can look at taking the project a bit further.
- If you haven’t already, try playing another song.
- Right now, the keyboard is set up to play a C major scale. Change it so it plays an E major scale, and then play Twinkle, Twinkle, Little Star in the key of E major. (hint: see this notes to frequencies chart)
- Change the keyboard so that it has 12 notes (of your choosing) instead of 8.
Other Projects
Need more inspiration for other Arduino-based projects? See these tutorials:
Alphanumeric GPS Wall Clock
LED Cloud-Connected Cloud
learn.sparkfun.com |CC BY-SA 3.0 | SparkFun Electronics | Niwot, Colorado