Speakers (make some noise)

Speaker Basics

Speakers are one of the few parts in our kit that are likely familiar. They are excellent noise makers. Their set up is simple and just like in the movies, they add a lot of dimension to a piece. We are not going to get into how they work here — though if you have done a sound course you may already know. For the curious, this explanation is pretty good.

Find your speaker in the small white cardboard box in your kit.

Speaker Representations

Here is the speaker symbol. The terminals are represented by the lines off the back of the body.

speaker symbol

Let’s explore.

Parts required
  • breadboard
  • speaker
  • fixed resistor (red body)
  • capacitor (largest one)
  • dupont wires
  • Arduino
The Circuit

In non-covid times you should solder wires to the speaker contacts. For remote learning and for a quick test, you can slide a dupont wire into the contact opening OR connect to them with alligator clips.

The circuit is made up of three components in series. A capacitor, a fixed resistor and the speaker.

Speaker
Simple 8ohm speaker.

Capacitor
We have two flavours of capacitor in our kits. For this circuit you need one of the large electrolytic caps. They look like garbage cans with legs. Find the largest capacitor it will be marked 50v100uF.

These large caps have a silver line near one leg — that silver line needs to face ground (like an LED).

Caps are the chameleons of the electronics world. They shift function in circuits the way chameleons change colors. In this circuit the cap protects the speaker constant voltage signals.

Resistor
The resistor for this circuit is the red bodied 130 ohm resistor (Br, Or, Br — really those are the colors though it is hard to tell).

The red bodied resistor in our kit can dissipate more power (V*I) than the other resistors. Note that the red in this case is not a code the way color bands are — they are just unique in our kit.

In this circuit the resistor is limiting current.

Note
I have built this circuit with only the cap for years. The resistor will reduce volume slightly — but the cap actually boosts it. You can build it with only one or the other. I still suggest both.

For my experiments I slipped male dupont wires into the opening in the contacts. If you use alligator wires clip to the resistor legs. You can even use fixed wire and bend small loops to hook the speaker. One we return to campus — come to the makerSpace and solder some wires onto your speaker for better performance.

COMMAND :: tone()

There are two versions of the tone() command:

tone(pin, frequency);
tone(pin, frequency, duration);

If you do not use duration you need to explicitly turn a tone off with:

noTone();

where:
pin = 2,4-10,12,13 (not 0,1,3,11)
frequency in hertz. >31Hz to limit of hearing? yours, not mine.
duration optional In milliseconds (same unit as delay).

The lines of code above mean:

Create a tone (PWM signal with 50% duty cycle) at frequency on pin X.

If used, continue the tone for duration milliseconds.

Add Code
int speakerPin = 8;
int noteDuration = 25;

int LOW_TONE = 100;
int HIGH_TONE = 1200;
int TONE_STEP = 100;

void setup() {

}

void loop() {

  for (int freq = LOW_TONE; freq <= HIGH_TONE; freq += TONE_STEP) { 
    tone(speakerPin, freq);    // create tone  
    delay(noteDuration);            // feel the tone wash over you
    noTone(speakerPin);             // stop the tone.     
  }

}

Get the code on tangible github.

What to expect.

If all is connected properly the code above with generate a series of tones that increase in frequency (pitch) then repeat. To me it sounds sort of like an alarm in an 8 bit game. Maybe is a victory song?

Once you have that running try changing the pitch limits in the for loop. And maybe modulate noteDuration.

Then grab the speaker_melody code from github. Upload it with the same circuit. Do you recognize the song?

How does this work?

Speakers make sound by converting electrical energy into motion — kind of like a motor. But the electricity moves the speaker elements back and forth (a lot like a solenoid — but way faster).

To make a speaker move it needs AC signals. We know from analogOutput explorations that the Arduino Uno is a digital device that approximates AC with PWM (~pins, analogWrite()).

Tone is a fast version of analogWrite() with a wider range of possible frequencies.

Going further

How speakers work — copy of link from above
Arduino tone docs
Melody tut. Code by Tom Igoe. Tom is a prof at ITP and advisor to our program.