nochecksum / rpio-rtttl-piezo

NodeJS module to play Nokia-style ringtones on a piezo speaker connected to a Raspberry Pi hardware PWM pin.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rpio-rtttl-piezo

Plays Nokia ringtones on a piezo speaker over a Raspberry Pi's GPIO pin with PWM.

Compatible with Raspberry Pi Models A, B (revisions 1.0 and 2.0), A+, B+, 2, 3, Zero.

This script requires root privileges to access the hardware PWM clocks. See node-rpio in further reading below for more details. Hardware PWM is only available on the following pins:

  • Raspberry Pi 26-pin models: pin 12
  • Raspberry Pi 40-pin models: pins 12, 32, 33, 35

Install

$ npm install git+https://github.com/nochecksum/rpio-rtttl-piezo.git

Basic usage

You should always specify an output pin (using physical hardware numbering):

var piezo = require('rpio-rtttl-piezo');

piezo.play({
  pwmOutputPin: 32
});

Specify a ringtone

Use the RTTTL format to provide a melody for playback:

var piezo = require('rpio-rtttl-piezo');

piezo.play({
  pwmOutputPin: 32,
  rtttl: 'BTTF:d=16,o=5,b=200:4g.,p,4c.,p,2f#.,p,g.,p,a.,p,8g,p,8e,p,8c,p,4f#,p,g.,p,a.,p,8g.,p,8d.,p,8g.,p,8d.6,p,4d.6,p,4c#6,p,b.,p,c#.6,p,2d.6'
});

Repeat

Repeat the melody 4 times with a 250ms delay between repetitions:

var piezo = require('rpio-rtttl-piezo');

piezo.play({
  pwmOutputPin: 32,
  repeat: 4,
  repeatDelay: 250
});

Playback octave

Shift the melody up 3 octaves. This is useful to move a melody's frequency into the most efficient dynamic range (loudest) for your piezo speaker. Check your datasheet for details.

var piezo = require('rpio-rtttl-piezo');

piezo.play({
  pwmOutputPin: 32,
  rtttl: 'GSTQ:d=4,o=6,b=100:p,8f5,16p,8p,16p,8f.5,32p,p,e5,32p,8a5,16p,8p,16p,8a.5,a5',
  freqMultiplier: 3
});

Volume (duty cycle)

A note is played by generating a square wave with PWM. The duty cycle describes how long each period of the square wave should be active, thus how loud it will sound.

Good values are 2 (loudest), 8, 16, 32, 64.

var piezo = require('rpio-rtttl-piezo');

piezo.play({
  pwmOutputPin: 32,
  dutyCycle: 32
});

Further reading

About

NodeJS module to play Nokia-style ringtones on a piezo speaker connected to a Raspberry Pi hardware PWM pin.