Beginner
...


Learning Objectives

  • Emphasis: Equal
  • Hardware: Potentiometer
  • Software: Arrays

Materials

  • Arduino Uno
  • Breadboard and wires
  • 10 kohm slide or rotary potentiometer
  • 8 ohm small speaker
  • 100 ohm resistor
Reference Files

Breadboard Layout

  1. Potentiometer to breadboard: A potentiometer is a variable resistor. Depending on where the slide is, the resistance can be anywhere from 0 to 10kohm. We've used a rotary potentiometer in the diagram to save space, but the slide type will make it look more like a harmonica. There are three legs on the potentiometer - one for power, one for ground, and one that acts as a voltage divider with only some resistance. Connect the divider leg (it should be separated from the others) to pin A0 on the Arduino and the other two to power and ground.
  2. Speaker: The speaker, like the RGB LED in the dimmer project, needs to be connected to the PWM pins to work properly. Connect pin 9 on the Arduino to the speaker and then through the 100-ohm resistor to ground.
  3. You're done! The set-up for this is pretty basic, so you're already finished! Plug your Arduino into the USB port on your computer.
Reference Schematic
...

...

Software

  1. Getting started: Create a new sketch and initalize variables to store your potentiometer and speaker pins. In the setup method, set your potentiometer to be input and the speaker to be output.
  2. Using pitches: To play notes on your speaker, you're going to need to use the tone library, which takes in note frequencies as an argument. As you can imagine, it's difficult to remember the different frequences of every single note you want to use. Instead, we're going to import a file that already has the frequencies defined for us. Copy-paste the pitches.h file and save it in the same folder as your harmonica sketch so your program can access it. Then, in your main sketch, write #include 'pitches.h' at the top. Now you can write NOTE_C4 instead of remembering that the exact frequency is 262 Hz.
  3. Choose your notes: For this program, we're not going to slide between all the available notes, or you'll get overwhelmed. Instead, we'll just choose 9 to store an octave scale. At the top of your program, declare an array to store your chosen notes: int notes[] = {NOTE_C4...NOTE_C5} (make sure you actually write out all the notes). This will save us from having to use a gigantic if-else block later.
  4. Read and convert potentiometer input: If you did the dimmer project, you already have practice with reading in analog values and converting them to analog output. The potentiometer creates a voltage divider just like the photoresistor did, so we'll get voltage outputs between half voltage (512) and full voltage (1023). Instead of converting these values to 0-255, we'll use 0-8 so we can easily get the corresponding note out of our array. You can even do it all in one line: int value = map(analogRead(potPin), 512, 1023, 0, 8).
  5. Output to speaker: Use the tone method to output your value to the speaker. We won't include a duration because we want the harmonica to be continually playing, so just write tone(speakerPin, notes[value]). This will get the correct note from your array and give the corresponding frequency from pitches.h to the speaker.
  6. Running the program: Upload your program and slide the potentiometer back and forth to see what happens! If the speaker is too loud or too soft, try changing the resistor value - higher = softer, lower = louder. Different size speakers need different resistances, so mess around until you find the one that works for you.
  7. Experiment: You can change the notes, add more or fewer, put in a song, or try using duration so your harmonica isn't always on. You can also try adding a switch so you can change the octave or turn the harmonica off.
Reference Code

The Finished Product




<-- Previous: Dim the Lights Next: Motion Detector -->