Lab-Analog Output

Parts

  • breadboard
  • hook up wire
  • Arduino
  • potentiometer, wired for breadboard connections
  • servo motor
  • variable resistor(s) of  your own choosing
  • fixed resistors as needed to create voltage divider circuts
  • small speaker (Radio Shack model 273-092 or similar) or headphone jack (274-274 or similar)
  • wire stripper
  • multimeter

Connect Power from Arduino to Breadboard

  1. Hook up a red wire from the 5V output of the Arduino to one of the power rails on the breadboard
  2. Hook up a black wire from either ground (GND) connection on the Arduino to a ground rail on the breadboard
  3. Hook up power and ground across the breadboard so that the rails on both sides are live.

Servo and Sensor

  1. Connect a potentiometer or other analog sensor to the Arduino’s analog input pin 0, in the same manner that you did in the Analog Input lab.
  2. Connect a servo motor. The red and brown wires will attach to 5V power and ground respectively. The remaining wire, which may be orange or another color, should be connected to the Arduino’s digital pin 9.

Power for a single servo motor can come directly from your Arduino board. If you start using additional servos, you may need to source more current than the Arduino can provide, in which case you may need to set up additional power supplies. In any case, your whole system should share ground. Even when power is coming from different sources, a common ground will ensure that messages passed between components all share a reference point for zero volts.

Servo_Hookup

Programming

  1. Load the following software onto the Arduino board.
  2. Modify the software so that the analog input only maps to a portion of the servo range, or so that one turn of the potentiometer takes the servo through its entire range multiple times.

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott

#include <Servo.h>

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 pin

void 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
}

Speaker Setup

  1. Remove the servo motor from your breadboard.
  2. Connect one lead from the speaker or headphone jack to ground.
  3. Connect the other lead to a 1K resistor, and connect that resistor to digital pin 8

The 1K resistor keeps you from overdriving the speaker. Depending upon the speaker or headphone you use, it’s possible that a different resistor value will be better. Don’t go lower than 220 Ohms or so because if you have too little resistance it’s possible to cause a short circuit.

Tone Library

  1. Download the Tone library and install it in your ~/Documents/Arduino/libraries folder. You may need to create the “libraries” folder if you have not installed libraries before.
  2. Restart the program and check that the Tone library is showing up on the Import Library submenu of the Sketch menu.
  3. Load the following code onto your Arduino:

int tonePin = 8; // select the pin with the speaker attached
void setup() {
pinMode(tonePin,OUTPUT); // make the speaker pin an output
}
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
tone(tonePin,note);
}

Rock Out

  1. Make some annoying noise.

Singing and Moving Don’t Mix

The Servo library cannot currently be used in the same sketch as the Tone library because they both take advantage of the same underlying timer functions. Hopefully this will be addressed in future revisions. In the meantime, if you want to make a musical instrument that moves, you can manually access either the servo or melody using non-library code.

Create Something Unique

Using everything you know, create either an inventive musical instrument that you play with your body or a device that creates motion in a surprising manner. Think physically!