Project tutorial
How to make music with an Arduino

How to make music with an Arduino © CC BY

In this video I explain how you can use an Arduino to make music using just a speaker and a resistor.

  • 6,647 views
  • 0 comments
  • 15 respects

Components and supplies

Apps and online services

About this project

In this video I explain how you can use an Arduino to make music using just a speaker and a resistor.

Like many similar projects, this one generates tones that match the notes of a piano. Unlike many of these projects, however, this one does not use a lookup table to obtain the frequencies. Instead, the tones are generated using a calculation. The calculation accepts a number from 1 through to 88. This number correlates to a key on an 88-key piano.

The Arduino library offers a tone() function which can be used to generate square-wave tones, but this function is unable to generate tones under 31Hz. An 88-key piano has a few notes with frequencies that fall below 31Hz. So, if you’d like to generate the full range of notes from an 88-key piano, the tone() function cannot be used. Instead, I will show you how to generate square-wave tones using a simple delay.

This project works at a microsecond level. A microsecond is one millionth of a second. This project makes extensive use of the Arduino library’s delayMicroseconds() function, but therein lies another minor issue, as it cannot reliably delay for more than a few thousand microseconds, which this project sometimes needs to do, so I will show you how to overcome this too.

One last thing about this project is that, despite the issues described above, the code for the project is really rather small, as the code just about fits onto a single screen (your resolution may vary!). To be fair, that is without any code comments, but a link to fully commented code is available in the video description.

I hope you enjoy this project!

How to install the Arduino IDE in Ubuntu

Code

Project code

Schematics

Wiring
Connect speaker to GND and pin 12 via a resistor.
Wiring2 eq7zwj0hnr

Comments

Similar projects you might like

Newbie Music Player

Project in progress by Michael Marinis

  • 3,702 views
  • 2 comments
  • 12 respects

Music Adaptive RGB LED with Arduino and Processing

Project in progress by ounane mohamed elfarouk

  • 2,953 views
  • 8 comments
  • 24 respects

Play Music With Your Remote

Project tutorial by Simone Adobs

  • 7,859 views
  • 21 comments
  • 29 respects

Control Music Volume and Ledbar Using Slider of 1Sheeld

Project tutorial by ahmed ismail

  • 1,464 views
  • 2 comments
  • 11 respects

New Electronic Music Instrument Design

Project showcase by cpeckmusic

  • 4,313 views
  • 0 comments
  • 16 respects

Add Lights and Spooky Music to Your Jack-O-Lantern

Project tutorial by aams86

  • 5,031 views
  • 0 comments
  • 19 respects
Add projectSign up / Login