How to Make an Instrument with the Arduino

The Arduino allows you to go beyond playing a sound — you create your own instrument, similar to the Theremin. The Theremin, named after its inventor Léon Theremin, was one of the first electronic instruments, developed in the 1920s. It worked by detecting the electromagnetic field of the player’s hands to change signals: one hand for volume and the other for pitch.

The PitchFollower sketch

In this sketch, you find out how to make a budget Theremin using a piezo as a light sensor to control the pitch.

You need:

  • An Arduino Uno

  • A breadboard

  • A piezo

  • A light sensor

  • A 47k ohm resistor

  • Jump wires

This circuit has two separate halves, the piezo and the light sensor circuit. The piezo is wired as in the toneMelody sketch, with one wire to digital pin 8 and the other to GND.

image0.jpg

The light sensor is connected to analog 0 on one side and 5V on the other; the 4.7K resistor is connected between analog 0 and ground.

image1.jpg

Complete the circuit, and open the sketch by choosing File→Examples→02.Digital→tonePitchFollower.

/*
 Pitch follower
 Plays a pitch that changes based on a changing analog input
 circuit:
 * 8-ohm speaker on digital pin 8
 * photoresistor on analog 0 to 5V
 * 4.7K resistor on analog 0 to ground
 created 21 Jan 2010
 modified 9 Apr 2012
 by Tom Igoe
This example code is in the public domain.
 http://arduino.cc/en/Tutorial/Tone2
 */
void setup() {
 // initialize serial communications (for debugging only):
 Serial.begin(9600);
}
void loop() {
 // read the sensor:
 int sensorReading = analogRead(A0);
 // print the sensor reading so you know its range
 Serial.println(sensorReading);
 // map the pitch to the range of the analog input.
 // change the minimum and maximum input numbers below
 // depending on the range your sensor's giving:
 int thisPitch = map(sensorReading, 400, 1000, 100, 1000);
 // play the pitch:
 tone(8, thisPitch, 10);
 delay(1);  // delay in between reads for stability
}

After you have found the sketch, press the Compile button to check the code. Any syntax errors turn the Message box red when they are discovered, and you see an error message stating what is wrong.

If the sketch compiles correctly, click Upload to upload the sketch to your board. When it is done uploading, you should have a light sensor that will change the pitch of your buzzer. If you don’t hear any change, make sure that you turn a desk lamp on over your breadboard. This will help increase the difference when you cover the light sensor with your hand.

If nothing happens, you should double-check your wiring:

  • Make sure that you’re using the correct pin number for the inputs and outputs.

  • Check that your piezo is turned the correct way. Symbols may be hidden on the underside if they are not visible.

  • Check the connections on the breadboard. If the jump wires or components are not connected using the correct rows in the breadboard, they will not work.

The PitchFollower sketch breakdown

This sketch directly converts the readings from the light sensor to a frequency rather than requiring a lookup table. This means that you can slide between notes as well as choose them individually.

In setup, the serial port is opened to allow you to monitor the sensor readings as they come in.

void setup() {
 // initialize serial communications (for debugging only):
 Serial.begin(9600);
}

In the main loop, the light sensor is read from analog pin 0. This reading is also forwarded onto the serial monitor.

void loop() {
 // read the sensor:
 int sensorReading = analogRead(A0);
 // print the sensor reading so you know its range
 Serial.println(sensorReading);

To convert the sensor’s range to the range of frequencies that the buzzer can cover, you use the map function.

 // map the pitch to the range of the analog input.
 // change the minimum and maximum input numbers below
 // depending on the range your sensor's giving:
 int thisPitch = map(sensorReading, 400, 1000, 100, 1000);

The tone function then outputs the note with the mapped sensor value and a very short duration of 10 milliseconds. This duration serves to make the sound audible, but the real duration will be determined by how long you hold your hand over the sensor, as described previously.

 // play the pitch:
 tone(8, thisPitch, 10);

Finally, a tiny delay occurs at the end of the loop to improve the stability for the readings.

 delay(1);  // delay in between reads for stability
}

With this setup, you can quickly make an easy controller and maybe even form a traveling Theremin band with your friends.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.