The Piezo class constructs objects that represent a single piezo component attached to the physical board.

Parameters

  • pin A Number or String address for the Piezo (+) pin (digital):

    var piezo = new five.Piezo(3);
    
  • options An object of property parameters.

    PropertyTypeValue/DescriptionDefaultRequired
    pinNumberDigital Pin.yes

Shape

Property NameDescriptionRead Only
boardA reference to the board object the Led is attached toNo
idA user definable id value. Defaults to nullNo
pinThe pin address that the Led is attached toNo
modeMode the piezo's pin is set to: output (1).Yes
isPlayingBoolean: is the piezo currently playing?Yes

Component Initialization

Basic

new five.Piezo({
  pin: 3
});

piezo diagram

i2C backpack

  var piezo = new five.Piezo({
    controller: "I2C_BACKPACK",
    pin: 3
  });

Firmware: https://github.com/rwaldron/johnny-five/blob/master/firmwares/piezo_i2c_backpack.ino

Usage

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {

  // Create a standard `piezo` instance on pin 3
  var piezo = new five.Piezo(3);

  // Plays a song
  piezo.play({
    // song is composed by an array of pairs of notes and beats
    // The first argument is the note (null means "no note")
    // The second argument is the length of time (beat) of the note (or non-note)
    song: [
      ["C4", 1 / 4],
      ["D4", 1 / 4],
      ["F4", 1 / 4],
      ["D4", 1 / 4],
      ["A4", 1 / 4],
      [null, 1 / 4],
      ["A4", 1],
      ["G4", 1],
      [null, 1 / 2],
      ["C4", 1 / 4],
      ["D4", 1 / 4],
      ["F4", 1 / 4],
      ["D4", 1 / 4],
      ["G4", 1 / 4],
      [null, 1 / 4],
      ["G4", 1],
      ["F4", 1],
      [null, 1 / 2]
    ],
    tempo: 100
  });
});

API

  • frequency(frequency, duration) Play tone at frequency (in Hz) for duration milliseconds.

    piezo.frequency(587, 1000); // Play note d5 for 1 second
    
  • play(tune, callback) Play a tune with an optional callback to invoke when the tune is done playing. The tune object contains a song (an array of the notes in the tune) and optional settings (e.g. tempo):

    piezo.play({
      tempo: 150, // Beats per minute, default 150
      song: [ // An array of notes that comprise the tune
        [ "c4", 1 ], // Each element is an array in which 
                     // [0] is the note to play and 
                     //[1] is the duration in "beats" (tempo, above)
        [ "e4", 2 ],
        [ "g4", 3 ],
        [ null, 4 ] // null indicates "no tone" for the beats indicated
      ]
    });
    

    The notes you can use are defined in lib/piezo.js as Piezo.Notes:

    Piezo.Notes = {
      "c4": 262,
      "c#4": 277,
      "d4": 294,
      "d#4": 311,
      "e4": 330,
      "f4": 349,
      "f#4": 370,
      "g4": 392,
      "g#4": 415,
      "a4": 440,
      "a#4": 466,
      "b4": 494,
      "c5": 523,
      "c#5": 554,
      "d5": 587,
      "d#5": 622,
      "e5": 659,
      "f5": 698,
      "f#5": 740,
      "g5": 784,
      "g#5": 831,
      "a5": 880,
      "a#5": 932,
      "b5": 988,
      "c6": 1047
    };
    

    You can also use frequencies directly if you'd like:

    piezo.play({
      song: [
        [ 698, 1 ], // Play frequency 698 for 1 beat
        [ 831, 2 ] // ...
      ]
    });
    
  • tone(tone, duration) Play a tone for duration ms. The tone value in this case is a computed duty cycle (in microseconds). This is a lower-level method than frequency (which does the translation from frequency to tone for you). Most of the time you likely want to use frequency.

  • noTone Stop tone playing from Piezo. Will immediately stop playing a tone (digitalWrite the output pin low) and clear any existing queued tone timers.

  • off Alias of noTone

Events

Piezo objects are output only and therefore do not emit any events.

Examples

Hi! The Johnny-Five community is building new projects every day. We made this newsletter to tell you about what's new, what's good, and what's next for Open Source robotics. Join us in exploring what we can make together.

Fork me on GitHub