Play melodies on Arduino with a PC speaker.
- VS Code with the PlatformIO extension installed.
- Arduino board (tested on Arduino Leonardo).
- PC speaker or any piezoelectric buzzer.
- Connect a PC speaker to GND and 12 pins of your Arduino.
- Connect your Arduino to a PC with a USB cable.
- Open the workspace in VS Code.
- Compose your own melody (see below) and put it inside
src/melody.h
file. - Build and upload a firmware using the PlaformIO project tasks.
Melody is loaded from the src/melody.h
header file. It contains the following lines:
#define BPM {bpm}
- defines BPM of your melody.#define LENGTH {length}
- defines amount of notes in your melody.#define NOTES {notes}
- defines notes that are used in your melody. Seesrc/pitches.h
for the reference.#define DURATIONS {durations}
- defines durations of corresponding notes. Use1
for a whole note,2
for a half note,4
for a quarter note and so forth.
You can use examples from the examples
directory to test a program.
Composing a melody from scratch may be a bit confusing, so it's recommended to use the ArWave Converter utility to convert single-track MIDI file to the melody header file.