Using Arduino with Parts and Sensors – Speakers Part 1

Improved circuit

This time, we’ll examine “sound” using Arduino with speakers. Arduino comes with a “tone” function and it’s relatively easy to get started with. Let’s see how sound works with Arduino by creating a simple instrument gadget.

Today Electronic’s Recipe

Expected time to complete: 90 minutes

Parts needed:

Understanding How Speakers Work

This time, we’ll be dealing with speakers. But electronically speaking, what exactly is a speaker? Everyone has a general sense that electricity flows through them, causing vibrations that lead to the sound generation, but let’s look into the process of sound creation in more detail.

Types of Speakers

When it comes to classifying speakers, there are many types of shapes. But regarding how electric signals are processed, they can be classified as: dynamic, condenser, piezoelectric, discharge (ion), magnetic, and ribbon.

The piezoelectric type that we’ll be using is shown in figure 1.

Piezoelectric speaker mechanism

Figure 1 Piezoelectric speaker mechanism

On figure 2, you can see the general mechanism of a dynamic speaker. In both cases, by supplying a current in accordance with a signal of the sound to be output, the speaker causes vibrations in the air, and produces sound.

Dynamic speaker mechanism

Figure 2 Dynamic speaker mechanism

Difference Between Speaker and Microphone

We’ll be working with speakers this time. But, let’s take a step back first and consider how a microphone works. Roughly speaking, a mic is a type of speaker. A speaker converts current to vibrations in the air, and a mic takes vibrations in the air and converts them into current for use as an audio signal.

Let’s try an experiment.
The speaker that we’ll be using this time takes an electric current to excite the piezoelectric elements and cause a metal plate to vibrate thus producing sound. But with an LED connected to the speaker, try bending the speaker. By the way, speakers don’t have a positive/negative direction.

Bending a speaker to lit up an LED

Picture 1 Bending the speaker causes the LED to light up

By doing this, we can see that the LED lights up. Using the same mechanism as a microphone, we have proof that bending a piezoelectric speaker (in the case of a mic this is equal to receiving a vibration in the air) results in the creation of a current. The amount of current generated by bending a speaker is extremely small and short, so there isn’t much that we can do with it, but it’s an interesting experiment.

Operating a Piezoelectric Speaker Using Arduino

Let’s dive right in and create a tone using Arduino and a speaker. To make it easy, Arduino comes with a tone function designed to create sound.

Tone function

Tone (output pin number, output frequency, length);

Let’s connect a speaker to Arduino and upload a program to it.

Tone function circuit

Picture 2 Tone function circuit

Tone function circuit figure

Figure 3 Tone function circuit

A major scale plays repeatedly. Next, let’s try the following program. Let’s play a “Do” and a “Mi” together.

What was the result? It didn’t work as expected. The tone function, in accordance with the program, plays a “Do” and then immediately tries to play a “Mi” for the specified length in milliseconds, so the vibrations of the speaker stack up and noise is produced, contrary to what we expected. The Arduino tone function can only accept one tone at a time.

Well then, how can we create a chord? Let’s look into the mechanism for creating tones.

Notes? Chords?

"La" waveform

Figure 4 Waveform of “La”

Sounds vibrations in the air take the form of a wave. In the case of a simple wave, take for example a tuning fork that is used for tuning instruments. It produces a “La” that is 440Hz. Putting it simply, this causes 440 vibrations per second in the air. This is shown in the figure 5 below.

Well then, what happens when we play another tone along with this “La?”
Waveform of two tones

Figure 5 Waveforms for two tones

The red line in the graph is the newly added tone. If we had two speakers then they could each generate one of the tones. But with only one speaker, it’s necessary to combine the waveforms to create the tones. The result is shown in the figure 6 below.

Combining two tones

Figure 6 Combining two tones (green line)

The green line in the graph shows the wave of the tone that we need to generate to reproduce the two tones. When using one speaker, this method is required for combining the 2 tones.

We could create a program that calculates the waveforms to combine every notes. But to do this kind of calculations would be complicated.

Here is an example that uses a program to calculate the sound wave without using the tone function. You can give it a try if you’re interested.

Creating a Simple Instrument Gadget

Now, we understand a bit about generating tones with Arduino. So, let’s try to create a simple instrument gadget. Using the tone function and a tactile switch, let’s make a simple electric organ. It wouldn’t be good if we attached each of the seven keys to its own analog pin, so let’s set a different resistance value for each key (10K, 12K, 15K, 18K, 20K, 22K, 25K) and use an “if” statement in the program to judge the AnalogInput and determine which note of the scale it should reproduce.

analog input based on resistance value

Figure 7 Judging analog input based on resistance value (while pushing the tactile switch)

In this case, one AnalogInput pin will be sufficient.

Circuit for a single instrument gadget

Figure 8 Circuit for a simple instrument gadget

Circuit for a simple instrument gadget

Picture 3 Circuit for a simple instrument gadget

Let’s hack this a bit.

It worked well, but let’s see what else we can improve. Let’s use the photoreflector that we used previously to create something that will change the tone as we move our finger closer to it. For guitarists, this method would be similar to “choking.” By adding the value input from the photo-reflector we can change the value of the tone.

Improved circuit

Picture 4 Improved circuit


Next, we’ll delve further into sound creation by using a stereo jack with Arduino to produce tone through speakers. More than just the “beeps” that can be obtained using the tone function, we’ll use an open-source library called “Mozzi” to create a rich set of sounds like those found in synthesizers using Arduino alone for sound production.