TINKER PiEDESIGN IN INDIA

Play a melody with a buzzerTinkerPieCircuit tutorial 13 of 24 · Intermediate · 15 minutes

Press a button to play the notes of a scale on a buzzer using tone().

Open this circuit in TinkerPieCircuit (free, runs in your browser)

What you will learn

Parts

TinkerPie V1 board1 buzzer1 push button

Steps

  1. Wire the buzzer

    Click Buzzer. Wire its + leg to P12 and its − leg to GND.

  2. Wire the button

    Wire the button between P10 and GND, like the Button tutorial.

  3. List the notes

    A note is a frequency in hertz. 262 is middle C. The array holds eight notes of a scale.

  4. Play when pressed

    When the button is pressed, a loop plays each note for 200 ms with tone(P12, note).

  5. Listen

    Press ▶ Start (your browser needs this click to allow sound) and hold the button.

The code

function setup() {
  pinMode(P10, INPUT_PULLUP);
}

function loop() {
  if (digitalRead(P10) == LOW) {
    const notes = [262, 294, 330, 349, 392, 440, 494, 523];
    for (let i = 0; i < notes.length; i++) {
      tone(P12, notes[i]);
      delay(200);
    }
    noTone(P12);
  }
}

Copy this into the Code tab. It is Arduino-style JavaScript. The real board uses the same pin names (S1, P10…) in TinkerPie Studio.

How it works

A higher frequency is a higher note. The buzzer clicks on and off that many times each second.

Challenge: Play "Twinkle Twinkle Little Star": C C G G A A G. Change the speed of the tune.

← Make a servo sweep · All 24 circuit tutorials · Why LEDs need a resistor →