Musical Instrument Assignment

You should now have learned how to connect switches (with a resistor) to digital input pins and variable resistors to the analog input pins of the Arduino. You should also have learned how to connect a speaker (with a resistor) to a digital output pin and play tones with different frequencies.

Here is another example sketch that is already available to you in the Arduino IDE:
(Select File...Examples...10.StarterKit...p06_LightTheremin)

I have added extra comments to explain things.

        /*Arduino Starter Kit example
 Project 6  - Light Theremin
 
 This sketch is written to accompany Project 6 in the Arduino Starter Kit
 
 Parts required:
 photoresistor and  10 kilohm resistor 
     connected from power to ground with the middle connected to analog input A0

 piezo speaker and 100 ohm resistor connected from digital pin 8 to ground
 
 Created 13 September 2012
 by Scott Fitzgerald
 
 This example code is part of the public domain 
*/

// variable to hold sensor value
int sensorValue;
// variable to calibrate low value (but starts at highest possible value)
int sensorLow = 1023;
// variable to calibrate high value (but starts with the lowest possible value)
int sensorHigh = 0;
// LED pin (you can use the built in LED on the Arduino board)
const int ledPin = 13;

void setup() {
  // Make the LED pin an output and turn it on
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

  // calibrate for the first five seconds after program runs
  // while the LED is on, keep changing the variable resistor over its full range
  while (millis() < 5000) {
    // record the maximum sensor value
    sensorValue = analogRead(A0);
    if (sensorValue > sensorHigh) {
      sensorHigh = sensorValue;
    }
    // record the minimum sensor value
    if (sensorValue < sensorLow) {
      sensorLow = sensorValue;
    }
  }
  // turn the LED off, signaling the end of the calibration period
  digitalWrite(ledPin, LOW);
}

void loop() {
  //read the input from A0 and store it in a variable
  sensorValue = analogRead(A0);

  // map the sensor values to a wide range of pitches from 50 Hz to 4000 Hz
  int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);

  // play the tone for 20 ms on pin 8
  tone(8, pitch, 20);

  // wait for a moment
  delay(10);
}
    

Try changing things in this sketch and make sure you understand how everything works. If you don't understand, ASK!

Now it's time to make your own musical instrument.

  1. Make a web page and describe what you would like your musical instrument to do:
    1. will it be controlled by switches or by resistors?
    2. how will the user control which note is played?
    3. how many different notes will it play?
    4. what will be the range of frequencies?
    5. how long will each note last for?
    6. will the user be able to stop it from making sounds?
  2. Draw a circuit diagram of what electronic components you will have to connect to the Arduino, and which pins you will be using. It might help to make notes about which color of wires you will use. Take a picture of your sketch or use a computer-drawn image and include it on your web site.
  3. Write a sketch to control the Arduino and turn it into your musical instrument.
  4. Demonstrate your musical instrument to your group and get their comments.
  5. Fix any problems that your team identified.
  6. Demonstrate your musical instrument to the teacher, copy and paste your sketch code into an e-mail to the teacher, and update your portfolio index page to link to your musical instrument web page.

Back to the main menu.