Friday afternoon. I was so ready to go home from the studio, considering that I spent the night before singing karaoke with some of my classmates until 4AM. However, when I realized that four eager students were about to start this week’s lab, I figured my hangover would just have to be put on hold while joining them. This lab is about music in the same way karaoke is about music. It’s “music”. So stay with me while I redefine the concept of music. Or just let me ramble on without any readers.

Some of my very eager classmates in the process of inventing a musical instrument!
Parts we used: Arduino, potentiometer wired for breadboard connections, servo motor, variable resistor(s) of your own choosing, fixed resistors as needed to create voltage divider circuits, small speaker (Radio Shack model 273-092 or similar), wire stripper, multimeter, breadboard, hook up wire.
SERVO MOTOR
We connected the power from Arduino to the breadboard, and hooked up the servo motor and potentiometer to it like this:
This was the code we used to control the servo with the potentiometer:
// Controlling a servo position using a potentiometer (variable resistor)
#include
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pinvoid setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
By changing the mapping from this:
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 179)
to this:
val = map(val, 0, 1023, 0, 90); // scale it to use it with the servo (value between 0 and 90)
we’re able to make the potentiometer only control movement between 0 and 90 degrees instead of 180 degrees for the servo motor.
SPEAKER SETUP AND NOISE
We removed the servo motor, and connected the speaker to ground and to digital pin 8. Adding a 1K resistor kept us from overdriving the speaker. We downloaded the Tone library and installed it in our ~/Documents/Arduino/libraries folder, restarted the program, and then loaded this code onto our Arduino:
#include
Tone mytone; // declare a tone namevoid setup() {
// initialize the tone
mytone.begin(8);
}void loop() {
// read the analog input
int inVar = analogRead(0);
// map the input to a frequency range between 200 and 2000 Hz
int note = map(inVar, 0, 1023, 200, 2000);
// play the note
mytone.play(note);
}
Now we were able to control a tone (or noise, if you like) with the potentiometer. This is how it sounds. And looks. Yeah, it’s a video. 10 seconds of your precious time. I’m not sure it’s worth it, though. Just saying.
CREATE SOMETHING UNIQUE
By using a force sensor we can change the tone that the speaker is playing just by pressing the sensor in different ways. We combined code from our Analog input lab with the code we just used to get this to work:
#include
Tone mytone; // declare a tone name
int forcePin = 3; // force sensor connected to analog pin 3void setup() {
mytone.begin(8); // initialize the tone
}void loop() {
int inVar = analogRead(forcePin); // read forcePin
int note = map(inVar, 1, 1023, 200, 2000);
// map the input to a frequency range between 200 and 2000 Hz
mytone.play(note); // play the note
}
If you look at this unique invention and define it as a musical instrument, then that’s good. To be completely honest with you — I’d rather go play my organ.


