Arduino Sound Lesson 1

Making Sound with Arduino!

By Adam Grabowski

Hey everybody! We're going to learn how
to make fun synthesized sounds with our trusty Arduino in this
lesson. First however, lets take some time to discuss how sound works
in general before we try to create it.

The sounds we hear are essentially
vibrations in the air that are detected by our ears. Our brain then
interprets these vibrations and we then perceive the sound. How do we
know which vibrations make our brain identify each different sound?
These vibrations are called waves and each wave has a frequency. The
higher the frequency, the higher the sound and the lower the
frequency, the lower the sound. To create different pitches using our
Arduino we simply need to change which frequencies we create.

Now lets begin actually making a sound.
For this code we will be able to make the pitch higher or lower by
turning the knob on a potentiometer. To begin, lets discuss this code
for our Arduino.

int speaker = 9;

int death = 0;

int tone = analogRead(death) + 1000;

void setup () {

pinMode (speaker, OUTPUT);

}

void loop ()

{

digitalWrite (speaker, HIGH);

delayMicroseconds (tone / 2);

digitalWrite (speaker, LOW);

delayMicroseconds (tone / 2);

tone = analogRead(death);

tone = map(tone, 0, 1023, 1000, 5000);

}

All this craziness starts out by
stating that the pin for our speaker output will be pin number 9 and
our analog input pin will be pin number 0. The other bit states that
our variable that I call “tone” is whatever value is read from
our pin 0 (which is called “death”). Moving on to our setup, we
now simply state that our pin 9 is an output. Nothing to exciting up
until this point but now lets move to the loop section. In order to
make the sound through our piezo, or speaker, we simply turn the pin
9 “HIGH” using the digitalWrite command. This works exactly the
same as an LED. “But Adam!” you might whine, “How will we make
different sounds if all we're doing is turning the speaker on and off
like an LED?” This part is where it gets kinda crazy. In order to
make different pitches we are basically going to be turning our
speaker on and off super fast. This turning on and off at incredibly
fast speeds with different amounts of delay in between is what gives
us different frequencies and therefore, different pitches.

As a quick side note a piezo is a
small circular device with positive and negative wires just like an
LED. The piezo is what will make our sound since it has the
capability to vibrate and create sound. Essentially it is is a very
small speaker like you would find in headphones.

Getting back to our code, we know up to
this point that our speaker is just getting turned on and off with
delay in between each. What determines the amount of delay is our
value from our analog input. This command is “delayMicroseconds”
which simply means that the time is in micro seconds instead of
seconds. This value is called “tone” and is actually half
whatever the analog input is because of the “/ 2” (this means
divide by 2) after our “tone” in the delay command. That's
basically all there is to this code other than the last command which
is the map command. What this states is that the values that we will
be receiving be between 0 and 1023 and we want them to be between
1000 and 5000.

Just in case we forgot, in order to
wire this fine piece 'o speaker to our bread board we are simply
going to have the negative side of the piezo (the black wire) hooked
to the ninth pin of the breadboard and the other wire going to
ground. Our potentiometer needs to have one of the leads on the side
to go to a ground with the other going to positive and the middle one
hooked to our first analog input pin on top of the actual Arduino.
Hook the pot to that pin with an alligator wire and be careful not to
touch any other pins or you'll get a short circuit (which is bad).
Hook it up and upload the code. Assuming that everything is in order,
you will be able to change the sound coming from the piezo by turning
the knob on the potentiometer.