Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Melody Player with Piezo Speaker

View and Edit Project In Cirkit DesignerImage of Arduino UNO Melody Player with Piezo Speaker

Circuit Documentation

Summary

This circuit consists of an Arduino UNO microcontroller and a Piezo Speaker. The Arduino UNO is programmed to play a melody through the Piezo Speaker. The speaker is connected to the Arduino's ground and digital pin 9.

Component List

Arduino UNO

Piezo Speaker

Wiring Details

Arduino UNO

Piezo Speaker

Code Documentation

// Define the notes of the melody
#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_G4  392
#define NOTE_A4  440
#define NOTE_B4  494
#define NOTE_C5  523

// Define the melody and note durations
int melody[] = {
  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};

int noteDurations[] = {
  8, 8, 8, 8, 8, 8, 8, 8  // Faster note durations
};

void setup() {
  // Initialize pin D9 as an output
  pinMode(9, OUTPUT);
}

void loop() {
  // Iterate over the notes of the melody
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    // Calculate the note duration
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(9, melody[thisNote], noteDuration);

    // Pause for the note's duration plus 10% to distinguish between notes
    int pauseBetweenNotes = noteDuration * 1.10;
    delay(pauseBetweenNotes);

    // Stop the tone
    noTone(9);
  }

  // Add a short delay before repeating the melody
  delay(500);
}

This code defines a simple melody and plays it through the Piezo Speaker connected to pin D9 of the Arduino UNO. The setup function initializes pin D9 as an output, and the loop function iterates over the notes of the melody, playing each note for its specified duration.