Arduino PWM MIDI Synthesis with Mozzi

This project uses an off the shelf synthesis library to create complex sounds using pulse-width-modulation (PWM) on one of the Arduino’s output pins.

Warning! I strongly recommend using an old or second hand keyboard for your MIDI experiments.  I am not responsible for any damage to expensive instruments!

These are the key Arduino tutorials for the main concepts used in this project:

If you are new to Arduino, see the Getting Started pages.

Parts list

  • Arduino Uno
  • MIDI in or receive module (see: Arduino MIDI Interfaces)
  • 3.5 mm jack, socket or breakout board
  • Jumper wires as required to connect to the jack
  • Amplification

The Circuit

There is no circuit to speak of, but the basic connections are as follows:

  • Arduino pin 0 (RX) to the MIDI in shield/module (along with 5V and GND).
  • Arduino pin 9 to the tip of the 3.5mm jack
  • Arduino GND to the sleeve of the 3.5mm jack

The jack lead will go to your amplification.  Here is a photo of my setup. I have a 3.5mm socket breakout board that I can connect standard jumper wires to directly and I’m using my Mini USB-MIDI to MIDI and MIDI In-Out Half-Shield, powering both from my Arduino.

2020-08-12 19.28.36

The Code

The Mozzi library is a sophisticated library for synthesizing sounds on an Arduino like platform.  To download it, go to the GitHub page and select the green “code” button and “Download ZIP”.  This will create a mozzi-master.zip file in your download area.  Then you can go to the Arduino IDE and select Sketch -> Include Library -> Add .ZIP Library and find your mozzi-master.zip file and that should do it.

This project makes use of the Mozzi_MIDI_Input example directly – find it under Examples -> Mozzi -> 11.Communication

You can download this directly to the Arduino and just start making noises!

There are lots of examples provided with the library and when used with an Arduino Uno all output is sent to pin 9 making use of the Arduino’s hardware PWM capability, so feel free to go and experiment.

There are lots of tutorials about using Mozzi on the Mozzi website – have a look here:

I won’t go into the details of how PWM works right now (I might talk more about it in a future project), but if you want to know more about the mechanics, I can recommend the following:

  • Arduino Secrets of PWM Tutorial
  • “Arduino for Musicians” by Brent Edstrom – Chapter 9
  • “Arduino Music and Audio Projects” by Mike Cook – Chapter 12

These last two books are also excellent introductions to digital music synthesis in general and Brent Edstrom also has a section on Mozzi.

For now, all you really need to know is that by using the built-in hardware PWM capabilities of the Arduino directly, we can perform direct digital synthesis without all that mucking about with resistor ladders and so on in a very processor-efficient manner.

Closing Thoughts

This has been a very simple introduction to Mozzi.  There is so much this library can do. If you have a play and like it, you can donate to support the Mozzi project here:

I plan to work through some of the other examples at some point and maybe get into some bespoke synthesis too, perhaps even getting a bit more into the details of PWM – although Mozzi hides all the detail very well and just allows you to get on making music.

I’m also very tempted by the MozziByte board for the Arduino Pro mini…

Kevin

2 thoughts on “Arduino PWM MIDI Synthesis with Mozzi

  1. This was awesome. One thing though. When I have it connected to a sequencer, at a high tempo. The notes sound like one continues tone. Is there a way to modify the code to help separate the notes when at a higher tempo?

    Like

    1. The length of note will be governed by the length in the sequencer I’d imagine. Its hard to anticipate how long a note is required to play in the microcontroller end, unless it is always a fixed (short) note. There might be some tweaking of the envelope generator possible, I’m not sure, but I suspect getting your sequencer to output “staccato” notes would probably end up being a lot easier 🙂

      Of course, as this is monophonic it may be that the notes are overlapping in which case you might not get a clean restarting of the envelope. Basically if there are overlapping NoteOn events that might mush them together… I’ll have to have another look to see if that would just change pitch or restart the envelope generator – I can’t remember now!

      Kevin

      Like

Leave a comment