Introduction: Simple MIDI Buzzer

About: I build projects using arduino nano / esp32 / arduino micro and also arduino mkr1010

Are you nostalgic of the good old pc speaker? This tutorial is for you!

We will learn how to control a buzzer using a MIDI keyboard and even compose music with it!

Last but not least we will see how to make an installer that will install a python application and also the drivers for an Arduino Nano so you can share your Arduino projects with anyone !

  • First we will see how to upload our code on an Arduino
  • Then we will learn how to wire a buzzer
  • Afterwards we will use our buzzer with a MIDI keyboard and a music software
  • Finally I will show you how to make an installer for your Arduino projects!

As always, all documentations/code are available in french/english at http://github.com/pigetArduino/ubuzzer
Everything is under really permissive licence (CC-A/MIT) so you can reuse it in your own tutorials.

Warning: This application is an alpha, and could not works correctly.

Step 1: Upload Arduino Code

First we need to upload the code on our Arduino.
We will not use the standard tone library, we will use toneAC instead that will allow us to control our buzzer more precisely.

  • Download the arduino code/librairies : http://ubuzzer.madnerd.org
  • Copy them into your sketch folder (Documents/arduino)
  • Upload ubuzzer.ino

Board : Arduino Nano / Processor : Atmega328

We will use the code I made to control Arduino with a python application, it is by far not optimize but easy to use.
Check out my instructables on this subject for more information:
https://www.instructables.com/id/Simple-RGB-Notific...
https://www.instructables.com/id/UTest-Make-USB-Dev...

Here is how toneAC works, it is similar to the tone library but as you can see, it allow to control volume and to use the buzzer without interrupting your sketch.

toneAC( frequency [, volume [, length [, background ]]] ) - Play a note.

  • frequency - Play the specified frequency indefinitely, turn off with toneAC().
  • volume - [optional] Set a volume level. (default: 10, range: 0 to 10 [0 = off])
  • length - [optional] Set the length to play in milliseconds. (default: 0 [forever], range: 0 to 2^32-1)
  • background - [optional] Play note in background or pause till finished? (default: false, values: true/false)

toneAC() - Stop output.
noToneAC() - Same as toneAC().

Step 2: Wiring

Our arduino is ready, let's wire our buzzer.

Here is the list of components you will need:

  • Arduino nano CH340G: 2€
  • 10 Buzzer : 1.80€ (1 Buzzer:0.18€)
  • Resistor pack 400pcs (3€) (1 resistor: 0.0071€)
  • 5 pcs stripboard (1.18€) (1 stripboard : 0.24€ )

Total : 7.98€ (2.43€)

  • Pin 10 : RESISTOR (100Ohm) --- Buzzer +
  • Pin 9 : Buzzer -

You can also add a led next to the buzzer, so it will light up when a note is played.
Just add a led (before the resistor)

  • Pin 10 : RESISTOR (100Ohm) --- Led + / Buzzer +
  • Pin 9 : Led - / Buzzer -

Don't forget to isolate the circuit from the arduino (see video)
You can either use tape/hot glue/Blu-Tack (patafix)

3D printed case

As for my others instructables, we will use an all purpose case for arduino nano projects made by Olivier Sarrailh : https://github.com/pigetArduino/ubuzzer/tree/mast...

Keep in made, you won't be able to properly close the case as the buzzer takes too much place, but you should still be able to close it

Step 3: Install Ubuzzer

Our device is ready to be used, we will see how to install our application that will translate each notes played on your computer into frequency sent with serial on our Arduino.

If ubuzzer doesn't works, check that

The installer is bundled with Arduino nano clone (CH340) drivers and will install it if necessary.

Once ubuzzer has started, you can use it with a midi keyboard, just plug it in and ubuzzer will automatically detected your midi device, your arduino will also be automatically connected.

You can also test your buzzer with the Arduino Serial Monitor:
Baudrate : 115200 / No Line Ending

  • UBuzzer --------> Check if this is the right device and returns OK
  • X -------> Where X is the frequency to play continuously
  • OFF -------> Stop sound

Step 4: Play Midi Files

You don't have a midi keyboard ? Don't worry I got you covered!
We will use LMMS (a free music software) to control it!

LoopMidi

First we need to use a software to create a virtual midi port, we are going to use loopMidi for this

LMMS

We will use LMMS to control our buzzer but you can use any music software that manages midi out to do so.

  • Download LMMS : https://lmms.io/download/#windows
  • On TripleOscillator change the volume to 0
  • Click on the gear next to TripleOscillator
  • Click on Midi and choose your virtual midi port (Loopmidi by default)

Examples are available in apps/ubuzzer/lmms , you can also import midi files.
Keep in mind the buzzer can only play one note at a time.

You can learn how to use LMMS with this short tutorial : https://www.instructables.com/id/Make-NES-Music-Wit...

Step 5: Make Your Own Arduino Installer

In my previous instructables we have seen how to make a portable application in python to control Arduino.
https://www.instructables.com/id/Simple-RGB-Notific...
https://www.instructables.com/id/UTest-Make-USB-Dev...

Now we will see how to make an installer for your arduino projects.
Source code is available in apps/ubuzzer

Install Python 3 / pyserial

We will need python 3 and pyserial to control our Arduino.

pip install pyserial

Install rtmidi2

In order to make our Arduino understands MIDI we will use rtmidi2 for python,
this library will allow us to easily makes instruments with Arduino.

You will need Visual Studio to compile rtmidi2.

  • Download rtmidi2
  • Download Visual Studio Community
  • Install Visual Studio Community and Select
    • Programming Languages
      • Visual C++Common Tools for Visual C++
    • Python Tools for Visual Studio
  • In the command line go to rtmidi2 folder
  • Type
pip install cython
python setup.py install

Start ubuzzer

We have all the dependencies to use ubuzzer.py, let's check if it works:

  • Open a command prompt (Windows key + cmd)
  • Go to the source code (apps/ubuzzer/)
  • Type:
python ubuzzer.py

Compile ubuzzer

We can ,now, compile ubuzzer as a windows application.
You will need pyinstaller

pip install pyinstaller

Compile python software using compile.bat or

pyinstaller --noconsole --icon=ubuzzer.ico ubuzzer.py

Make an installer with inno setup compiler

We have our application ready to be bundled in an installer, we will use inno setup compiler to do this.

;CHANGE PATHS before compiling!
#define APP "Y:\arduino\ubuzzer\apps\ubuzzer\dist\ubuzzer\"
#define DRIVERS "c:\nano"

in [RUN], we install the drivers silently

[Run]
;Silent install for arduino nano ch340g drivers Filename: {app}\nano\setup.exe; Parameters: "/s"; WorkingDir: {app}\nano; Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent