How To Make Music With An Arduino

Hackster.ioLearning Hardware Community. Projects× Log inSign up ProjectsProjectsChannelsChannelsNewsContestsEventsVideos ×

Embed the widget on your own site

Add the following snippet to your HTML:

How to make music with an Arduino

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

Read up about this project on Hackster.io

How to make music with an Arduino

Code_and_Make35 38,531
  • Overview
  • Things
  • Story
  • Schematics
  • Code
  • Credits
  • Comments(10)
Code_and_MakeCode_and_MakePublished © CC BYHow to make music with an Arduino

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

BeginnerFull instructions provided38,531How to make music with an Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
  • Buy from Newark
  • Buy from store.arduino.cc
  • Buy from Adafruit
  • Buy from Arduino Store
  • Buy from CPC
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1
Resistor 221 ohm
Resistor 221 ohm
150 - 220 Ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

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

Read more

Schematics

Wiring

Connect speaker to GND and pin 12 via a resistor.

Code

Project code

Credits

Code_and_Make

Code_and_Make

1 project • 9 followersFollow ContactContact

Comments

Related channels and tags
  • Arduino
  • Music
  • Music
  • audio
  • music

Tag » Arduino 8 Ohm Speaker Tutorial