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.
Expected time to complete: 60 minutes
Parts needed:
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/
Picture 2 Mozzi Website Examples
Now, let’s set up Mozzi for Aruino. The Mozzi installation process is as follows.
On Arduino, different libraries can be installed easily using the same tool.
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.
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.
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”.
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.
Figure 1 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* Example playing a sinewave at a set frequency, using Mozzi sonification library. Demonstrates the use of Oscil to play a wavetable. Circuit: Audio output on digital pin 9 on a Uno or similar, or DAC/A14 on Teensy 3.1, or check the README or http://sensorium.github.com/Mozzi/ Mozzi help/discussion/announcements: https://groups.google.com/forum/#!forum/mozzi-users Tim Barrass 2012, CC by-nc-sa. */ //#include <ADC.h> // Teensy 3.1 uncomment this line and install http://github.com/pedvide/ADC #include <MozziGuts.h> #include <Oscil.h> // oscillator template #include <tables/sin2048_int8.h> // sine table for oscillator // use: Oscil <table_size, update_rate> oscilName (wavetable), // look in .h file of table #included above Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA); // use #define for CONTROL_RATE, not a constant #define CONTROL_RATE 64 // powers of 2 please void setup(){ startMozzi(CONTROL_RATE); // set a control rate of 64 (powers of 2 please) aSin.setFreq(440); // set the frequency } void updateControl(){ // put changing controls in here } int updateAudio(){ return aSin.next(); // return an int signal centred around 0 } void loop(){ audioHook(); // required here } |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <MozziGuts.h> //Include the Mozzi library to be used #include <Oscil.h> //Include the oscillator template for producing sound #include <tables/sin2048_int8.h> //Read sin wave for use with oscillator Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA); //Format oscillator sign wave #define CONTROL_RATE 64 //Control rate void setup(){ startMozzi(CONTROL_RATE); //Set up control rate that will call updateControl aSin.setFreq(440); //Frequency to be produced } void updateControl(){ } int updateAudio(){ return aSin.next(); //Continue to produce sin wave } void loop(){ audioHook(); //Output sound } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <MozziGuts.h> //Include the Mozzi library to be used #include <Oscil.h> //Include the oscillator template for producing sound #include <tables/sin2048_int8.h> //Read sin wave for use with oscillator Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA); //Format oscillator sign wave #define CONTROL_RATE 64 //Control rate int pitch = 200; void setup(){ startMozzi(CONTROL_RATE); //Format control rate aSin.setFreq(pitch); //Output frequency } void updateControl(){ if(pitch < 400){ pitch++; } else{ pitch = 200; } aSin.setFreq(pitch); //Output frequency } int updateAudio(){ return aSin.next(); //Continue to produce sin wave } void loop(){ audioHook(); //Output sound } |
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.
Now that we have found our method for adjusting the pitch, let use it with a circuit. Prepare the following circuit:
Figure 4 Arduino Volume Variable Resistance Circuit
Picture 7 Arduino Volume Variable Resistance Circuit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <MozziGuts.h> //Include the Mozzi library to be used #include <Oscil.h> //Include the oscillator template for producing sound #include <tables/sin2048_int8.h> //Read sin wave for use with oscillator Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA); //Format oscillator sign wave #define CONTROL_RATE 64 //Control rate int pitch = 200; void setup(){ startMozzi(CONTROL_RATE); //Format control rate aSin.setFreq(pitch); //Output frequency } void updateControl(){ int pitch = mozziAnalogRead(0); //Input from 0-pin aSin.setFreq(pitch); //Output frequency } int updateAudio(){ return aSin.next(); //Continue to produce sin wave } void loop(){ audioHook(); //Output sound } |
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.
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.
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.