logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

Using Arduino with Parts and Sensors – Speakers Part 2

Device Plus Editorial Team
Published by Device Plus Editorial Team at January 26, 2016
Categories
  • Arduino
Tags
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:

  • Arduino (Arduino Uno R3 /Arduino Pro Mini)
    https://www.adafruit.com/products/50
    https://www.adafruit.com/products/2378
  • Breadboard https://www.adafruit.com/products/64
  • Piezoelectric speaker http://www.rapidonline.com/Audio-Visual/KEPO-KPR-G3010-6250-Piezo-Transducer-1-3-0-5-kHz-35mm-51-7406
  • Tactile switch http://www.rapidonline.com/electronic-components/diptronics-dts-62n-tactile-switch-6-x-6mm-height-5mm-78-0621

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

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.

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.

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.

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

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.

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.

Device Plus Editorial Team
Device Plus Editorial Team
Device Plus is for everyone who loves electronics and mechatronics.

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Using Arduino with Parts and Sensors – Speakers Part 1Using Arduino with Parts and Sensors – Speakers Part 1
  • Using Arduino with Parts and Sensors – Ultrasonic SensorUsing Arduino with Parts and Sensors – Ultrasonic Sensor
  • Using Arduino with Parts and Sensors – Accelerometer Part 2Using Arduino with Parts and Sensors – Accelerometer Part 2
  • Lightweight Arduino Library for ROHM Sensor Evaluation KitLightweight Arduino Library for ROHM Sensor Evaluation Kit
  • Make a Stevenson Screen with Arduino Part 2 – Display Numerical Values on an Electronic Scoreboard (7 segment LED)Make a Stevenson Screen with Arduino Part 2 – Display Numerical Values on an Electronic Scoreboard (7 segment LED)
  • Using Sensors with the Arduino: Create a Simple Device to Detect Movement and ReactUsing Sensors with the Arduino: Create a Simple Device to Detect Movement and React
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort