Using Arduino with Parts and Sensors – Speakers Part 2

External appearance of instrument gadget

Last time, we used a tone function to produce a simple sound with Arduino. This time, we will take another step forward by using the Mozzi open source library  to program sound waves.

Today Electronic’s Recipe

Expected time to complete: 60 minutes

Parts needed:

What is Mozzi?

Mozzi Website

Picture 1 Mozzi Website

Mozzi is an open source electronic music synthesizer for Arduino. “Electronic music synthesizer” sounds complex. But, it is just a library that lets you program sound waves and play them using speakers. We were only able to produce a beeping noise using the tone function last time. With Mozzi, it is possible to produce electronic instruments sounds like a theremin or a synthesizer.

Mozzi

Reference video: Theremin Performance

On Mozzi website, you can find many sound examples with samples and source code. After listening to various samples on their page, it feels like we can make our own electronic instrument.

Mozzi Examples
http://sensorium.github.io/Mozzi/examples/

Mozzi Website Examples

Picture 2 Mozzi Website Examples

Setting Up Mozzi with Arduino

Now, let’s set up Mozzi for Aruino. The Mozzi installation process is as follows.

  1. Downloading the Mozzi library from the website
  2. Adding the Mozzi library to Arduino

On Arduino, different libraries can be installed easily using the same tool.

1. Downloading the Mozzi Library from Their Website

Mozzi Download Page

Picture 3 Mozzi Download

Download the Mozzi library from their website. To download, click the icon on the top-right of the page. You will be able to save a file named “Mozzi.zip”.
*The newest version at the time of was Mozzi-1.0.2.zip.

2. Adding the Mozzi Library to Arduino

Adding Mozzi Library to Arduino IDE

Picture 4 Adding the library to Arduino

After finishing the download of Mozzi.zip, start up Arduino and select “Sketch” > “Include Library” > “Add .Zip Library…” from the menu. A screen will show up asking you to select a file. Select the downloaded Mozzi.zip file.
Open “Sketch” > “Include Library” again and check to see if Mozzi has been added. If so, your installation is complete.

Adding Mozzi Library to Arduino IDE
Picture 5 The Mozzi library has been installed on Arduino

If you are unable to install the library from “Add Library…”, unzip the Mozzi.zip file and check the folder for “Mozzi”. There is a folder called “libraries” in the folder where Arduino is installed. Directly add the Mozzi file, restart Arduino and then select “Sketch” > “Include Library”.

Making Sound with Mozzi

Once you finish installing the library, let’s try making sound with Mozzi. To make a sound, Arduino must be connected to a speaker. This time we will try to use a cone speaker.
With Mozzi, output uses the pin 9. So connect the speaker to the GND and to the pin 9. Also, the speaker does not have +/- polarity, so either GND is okay. But for speakers with cables like ours, one side is black, so we recommend you connect that one to the black GND.

Circuit connecting Arduino and a speaker

Figure 1 Circuit connecting Arduino and a speaker

Circuit connecting Arduino and a speaker

Picture 6 Circuit connecting Arduino and a speaker

After preparing the circuit, we can execute the program. To begin, let’s try using the “Basic” sketch program listed among the samples.
On Arduino, list the 02 Sinewave program included in Basics. When clicking “show sketch” from the below URL, you well be able to open the program, so copy and paste it to Arduino. This program lets us produce the same sine wave (beep) that we made with the tone function.

Mozzi Examples Basics
http://sensorium.github.io/Mozzi/examples/#01.Basics

We made the same beeping sound as the tone function. We will use this program to explain Mozzi process.


Figure 2 Mozzi Process Flow

Mozzi process flow is different from the standard Arduino one. Sound production and control are handled in between. These are the updateControl and updateAudio functions.

Mozzi Program Explanation
Figure 3 Mozzi Program Explanation

We can broadly divide this program into 3 parts. First, the part in blue includes programs needed to use Mozzi and Mozzi’s internal sound waves.
Secondly, the green portion declares program parameters and the settings for the sine wave oscillator used when producing sound output.
Finally, as detailed in figure 2, the red part is for Mozzi’s process.

Try changing the red part of the program with the number 440. This will change the frequency and the pitch of the sound produced.
Now, how can change the sound dynamically? Try running the below program.

This program modifies to the red part.
updateControl will execute repeatedly at the designated control rate. It will use the aSin.setFreq() function to adjust the output frequency. Whenever updateControl is called, the pitch number is increased by 1 if it is at 400 or under and reset to 200 if it goes above 400.
The control rate lets you set the number of times updateControl is called in 1 second. If the control rate is increased, the sound will change faster.

Using Volume Variable Resistance to Change the Sound Level

Now that we have found our method for adjusting the pitch, let use it with a circuit. Prepare the following circuit:

Arduino Volume Variable Resistance Circuit

Figure 4 Arduino Volume Variable Resistance Circuit

Arduino Volume Variable Resistance Circuit

Picture 7 Arduino Volume Variable Resistance Circuit

We were able to take the place where the volume was changed in the previous program and change the volume variable resistance logical input value by connecting to the pin 0.

Making the Appearance More Instrument-Like

External appearance of instrument gadget

Picture 8 Making the appearance more instrument-like

Now that we’ve learnt a bit about how to use Mozzi, let’s work on the appearance a bit. This time we used volume variable resistance. We could also use the previously introduced photoreflector or ultrasonic sensor to produce sound in the manner of a theremin. Try combining different sensors to make your own original electronic instrument.

Summary

Mozzi is a library that let’s you manipulate sound in an easy way. Many people are developing for Arduino. As people share their open source libraries, Arduino grows, and there is a circle where more and more interesting things come out.
Next time we will use an SD card with Arduino to try storing and reading sensor variables.