Build it

A piezo transducer can play a tone when supplied with a square wave signal. Using one of Flora's PWM pins, we can supply a square wave of varying frequency and duration to play melodies-- a wearable music machine!

You can also adapt this tutorial for Gemma, check out our singing hat tutorial: https://learn.adafruit.com/close-encounters-hat/prepare-sensor-and-piezo-buzzer

Insert the legs of the piezo into holes marked D9 (or any PWM-capable pin) and GND on the Flora.
Solder the leads of the piezo and clip off the extra wire with flush snips.
Peel off the adhesive protection and affix the metal bar from the magnetic pin back to your Flora. Now you can easily affix it to your clothes! Use a JST extension cable to lengthen your battery pack's wires so you can tuck the power pack in your pocket.

Program it

Program your Flora with the following code. It is a modified version of Tiago Gala's code to play the Super Mario Bros theme on the piezo, hence some of the Portuguese variable names. It will play the theme once upon startup:

Download: file
#define toneC    1911
#define toneC1    1804
#define toneD    1703
#define toneEb    1607
#define toneE    1517
#define toneF    1432
#define toneF1    1352
#define toneG    1276
#define toneAb    1204
#define toneA    1136
#define toneBb    1073
#define toneB    1012
#define tonec       955
#define tonec1      902
#define toned       851
#define toneeb      803
#define tonee       758
#define tonef       716
#define tonef1      676
#define toneg       638
#define toneab      602
#define tonea       568
#define tonebb      536
#define toneb       506

#define tonep       0 

int speaker = 9; 
long vel = 20000;
boolean hasplayed = false;
 
void setup() {
  pinMode(speaker, OUTPUT);
  delay(2000);
}

int melod[] = {tonec, toneG, toneE, toneA, toneB, toneBb, toneA, toneG, tonee, toneg, tonea, tonef, toneg, tonee, tonec, toned, toneB};
int ritmo[] = {18, 18, 18, 12, 12, 6, 12, 8, 8, 8, 12, 6, 12, 12, 6, 6, 6};
 
void loop() {
  if (hasplayed == true){ return;}
  for (int i=0; i<17; i++) {
    int tom = melod[i];
    int tempo = ritmo[i];
 
    long tvalue = tempo * vel;

    tocar(tom, tvalue);

    delayMicroseconds(1000);
  }	    delay(1000);

  hasplayed = true;
}
 
void tocar(int tom, long tempo_value) {
  long tempo_gasto = 0;
  while (tempo_gasto < tempo_value) {
    digitalWrite(speaker, HIGH);
    delayMicroseconds(tom / 2);
 
    digitalWrite(speaker, LOW);
    delayMicroseconds(tom/2);	 
    tempo_gasto += tom;
  }
}
For more examples/info on using a piezo to make melodies, check out the Arduino Play Melody page.

Wear it!

Place the magnetic part of the pinback inside your garment and affix the Flora to it on the outside. Tuck the battery pack in your pocket and enjoy your melody-making accessory!
This guide was first published on Jan 23, 2013. It was last updated on Jan 23, 2013.