# Wearable Piezo Tones with Flora

## Build it

http://youtu.be/K_RT7hYFLB4

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](../../../../close-encounters-hat/prepare-sensor-and-piezo-buzzer)

![](https://cdn-learn.adafruit.com/assets/assets/000/003/701/medium800/flora-piezo-materials.jpg?1396800821)

For this quick project you'll need:

- [Flora main board](https://www.adafruit.com/products/659)
- [Flora RGB Smart Neo Pixel](https://www.adafruit.com/products/1060)
- [Battery holder](http://www.adafruit.com/products/727)
- [Piezo transducer](http://adafruit.com/products/160)
- [JST extension cable](http://adafruit.com/products/1131)
- [Magnetic pin back](http://adafruit.com/products/1170)  

As for tools, you'll need a [USB cable](http://adafruit.com/products/260), [soldering iron](http://adafruit.com/category/8_84 "Link: http://adafruit.com/category/8\_84") and [flush diagonal cutters](http://adafruit.com/products/152).  
  
Helpful tutorials:  

- [Learn how to solder](http://learn.adafruit.com/adafruit-guide-excellent-soldering "Link: http://learn.adafruit.com/adafruit-guide-excellent-soldering")
- [Getting Started with FLORA](http://learn.adafruit.com/getting-started-with-flora/)  

Insert the legs of the piezo into holes marked D9 (or any PWM-capable pin) and GND on the Flora.

![flora-piezo-insert.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/702/medium640/flora-piezo-insert.jpg?1396800841)

Solder the leads of the piezo and clip off the extra wire with flush snips.

![flora-piezo-solder.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/703/medium640/flora-piezo-solder.jpg?1396800857)

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.

![flora-piezo-magnetic-back.jpg](https://cdn-learn.adafruit.com/assets/assets/000/003/704/medium640/flora-piezo-magnetic-back.jpg?1396800878)

# Wearable Piezo Tones with Flora

## Program it

Program your Flora with the following code. It is a modified version of [Tiago Gala's code](http://therandombit.wordpress.com/2011/11/21/arduino-piezo-speaker-super-mario/) 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:

```auto
#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](http://www.arduino.cc/en/Tutorial/PlayMelody) page.

# Wearable Piezo Tones with Flora

## Wear it!

![](https://cdn-learn.adafruit.com/assets/assets/000/003/705/medium800/flora-piezo-main.jpg?1396800897)

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!


## Featured Products

### FLORA - Wearable electronic platform: Arduino-compatible

[FLORA - Wearable electronic platform: Arduino-compatible](https://www.adafruit.com/product/659)
FLORA is Adafruit's fully-featured wearable electronics platform. It's a round, sewable, Arduino-compatible microcontroller designed to empower amazing wearables projects.FLORA comes with Adafruit's support, [tutorials and...](http://learn.adafruit.com/category/flora)

In Stock
[Buy Now](https://www.adafruit.com/product/659)
[Related Guides to the Product](https://learn.adafruit.com/products/659/guides)
### Piezo Buzzer

[Piezo Buzzer](https://www.adafruit.com/product/160)
Piezo buzzers are used for making beeps, tones and alerts. This one is petite but loud! Drive it with 3-30V peak-to-peak square wave. To use, connect one pin to ground (either one) and the other pin to a square wave out from a timer or microcontroller. For the loudest tones, stay around 4 KHz,...

In Stock
[Buy Now](https://www.adafruit.com/product/160)
[Related Guides to the Product](https://learn.adafruit.com/products/160/guides)
### 3 x AAA Battery Holder with On/Off Switch and 2-Pin JST

[3 x AAA Battery Holder with On/Off Switch and 2-Pin JST](https://www.adafruit.com/product/727)
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries and have an on-off switch. That makes...

In Stock
[Buy Now](https://www.adafruit.com/product/727)
[Related Guides to the Product](https://learn.adafruit.com/products/727/guides)
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

Out of Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
### JST-PH Battery Extension Cable - 500mm

[JST-PH Battery Extension Cable - 500mm](https://www.adafruit.com/product/1131)
By popular demand, we now have a handy extension cord for all of our JST PH-terminated battery packs (such as our LiIon/LiPoly and 3xAAA holders). One end has a JST-PH compatible socket, and the other end has a matching plug. Between the two, 500mm of color coded wire. Handy for wearable...

In Stock
[Buy Now](https://www.adafruit.com/product/1131)
[Related Guides to the Product](https://learn.adafruit.com/products/1131/guides)
### Magnetic Pin Back

[Magnetic Pin Back](https://www.adafruit.com/product/1170)
These magnetic pin backs have two pieces: a metal bar with adhesive strip and a plastic piece with two strong rare-earth magnets. Affix the metal bar to your FLORA projects-- it's just the right size! Then attach your FLORA project to any garment without poking holes in your clothes. The...

In Stock
[Buy Now](https://www.adafruit.com/product/1170)
[Related Guides to the Product](https://learn.adafruit.com/products/1170/guides)
### Getting Started with Adafruit FLORA

[Getting Started with Adafruit FLORA](https://www.adafruit.com/product/1839)
 **Making Wearables with an Arduino-Compatible Electronics Platform**  
  
This book introduces readers to building wearable electronics projects using Adafruit's tiny FLORA board: at 4.4 grams, and only 1.75 inches in diameter, and featuring Arduino compatibility, it's the...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1839)
[Related Guides to the Product](https://learn.adafruit.com/products/1839/guides)
### Getting Started with Adafruit FLORA Book Pack

[Getting Started with Adafruit FLORA Book Pack](https://www.adafruit.com/product/2404)
Pickup a copy of&nbsp;[Getting Started with Adafruit FLORA](https://www.adafruit.com/products/1839)&nbsp;and then hit the ground running with everything that you need to become an [Adafruit FLORA](https://www.adafruit.com/products/659) supreme being!

This pack is...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/2404)
[Related Guides to the Product](https://learn.adafruit.com/products/2404/guides)

## Related Guides

- [LED Stego Flex Spike Hoodie](https://learn.adafruit.com/led-stego-flex-spike-hoodie.md)
- [Adafruit Flora Bluefruit LE](https://learn.adafruit.com/adafruit-flora-bluefruit-le.md)
- [EEG Costume Cap](https://learn.adafruit.com/eeg-costume-cap.md)
- [GPS Logging Dog Harness](https://learn.adafruit.com/gps-logging-dog-harness.md)
- [Flora Snaps](https://learn.adafruit.com/flora-snaps.md)
- [Chameleon Scarf](https://learn.adafruit.com/chameleon-scarf.md)
- [VU Meter Baseball Hat](https://learn.adafruit.com/vu-meter-baseball-hat.md)
- [Heart Rate Badge](https://learn.adafruit.com/heart-rate-badge.md)
- [Light-Up Angler Fish Embroidery](https://learn.adafruit.com/light-up-angler-fish-embroidery.md)
- [Sparkle Skirt](https://learn.adafruit.com/sparkle-skirt.md)
- [Pac Man Pixel Suspenders](https://learn.adafruit.com/pac-man-pixel-suspenders.md)
- [STEAM-Punk Goggles](https://learn.adafruit.com/steam-punk-goggles.md)
- [Washing Wearable Electronics](https://learn.adafruit.com/washing-wearable-electronics.md)
- [Wires and Connections](https://learn.adafruit.com/wires-and-connections.md)
- [Colorful Creations with the New JLCPCB PCB Service](https://learn.adafruit.com/colorful-creations-with-the-new-jlcpcb-pcb-service.md)
