กลับไปหน้ารวมไฟล์
how-to-make-music-with-an-arduino-491912-en.md

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

Decoding Sheet Music: Making Music with Arduino

Making a random beep on an Arduino is trivial. Recreating the "Super Mario Bros" theme instantly recognizable by millions requires transcribing explicit musical constants. How to make music with an Arduino is the definitive guide to converting raw Piano Sheet Music concepts (Quarter Notes, BPM, Octaves) into rigid, mathematically flawless C++ array executions.

The Absolute <pitches.h> Translation Standard

You should never manually type tone(8, 261). It takes hours and no one can read it!

  1. The Arduino IDE has a massive, hidden master-file called pitches.h. It acts as a massive #define dictionary.
  2. It maps the word NOTE_C4 permanently to the integer 261.
  3. The Melody Array: You compose music visually!
#include "pitches.h"

int melody[] = {
  NOTE_E5, NOTE_E5, 0, NOTE_E5,
  0, NOTE_C5, NOTE_E5, 0,
  NOTE_G5, 0, 0,  0,
  NOTE_G4, 0, 0, 0
};
// Anyone reading the C++ code instantly recognizes the Mario Theme!

The Mathematical BPM Speed Loop

To play the array, you must calculate exactly how long a Quarter Note lasts in actual milliseconds.

  • You declare an array of Note Types! (4 = Quarter, 8 = Eighth). int noteDurations[] = { 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4 };
  • The for loop calculates the rigid execution speed:
for (int i = 0; i < 16; i++) {
  int duration = 1000 / noteDurations[i]; // E.g., 1000ms / 4 = 250 milliseconds!
  tone(8, melody[i], duration);
  delay(duration * 1.30); // Critical 30% rest delay to prevent notes from slurking together!
}
  • A higher number (1500 / noteDurations) makes the song slower! A lower number (800 / noteDurations) turns Mario into a speed-run glitch!

Synthesizer Core Parts

  • Arduino Uno/Nano (Standard operation).
  • Passive Piezo Buzzer.
  • A 100 Ohm Resistor to wire inline and protect the digital pin.
  • The specific downloading of the pitches.h tab directly into your Arduino sketch folder (Mandatory execution file!).

ข้อมูล Frontmatter ดั้งเดิม

apps:
  - "1x Arduino IDE"
author: "Code_and_Make"
category: "Audio & Sound"
components:
  - "1x Speaker: 0.25W, 8 ohms"
  - "1x Resistor 221 ohm"
  - "1x Arduino UNO"
description: "Foundational synthetics! Master the translation of printed musical stanzas and sheet-music tempo markings directly into structural C++ `int` arrays and dynamic `millis()` loop counters to construct complex orchestral hardware."
difficulty: "Easy"
documentationLinks: []
downloadableFiles:
  - "https://gist.github.com/codeandmake/1add672bc7957135ac4e212360acbc98"
  - "https://gist.github.com/codeandmake/1add672bc7957135ac4e212360acbc98"
encryptedPayload: "U2FsdGVkX1910EG+D5A2aSt7fUkAMECwMfVHyt7mqp+RXcat9Vgajv3WviMUltXuUjXFW2YpO/bnjvzNYu+j/gWIwJev+CCh8uKpoXtQ6p4="
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/how-to-make-music-with-an-arduino-491912_cover.png"
lang: "en"
likes: 22
passwordHash: "4685a5d46c23b19dbb64a0a77597688b87b243be287c23aab223b9f515641c46"
price: 435
seoDescription: "Learn how to make music with Arduino using a speaker and resistor. Simple step-by-step tutorial for DIY electronics projects."
tags:
  - "audio"
  - "music"
title: "How to make music with an Arduino"
tools: []
videoLinks:
  - "https://www.youtube.com/embed/Z1YvIFUIhLs"
views: 55452