Book Image

Learning JavaScript Robotics

By : Kassandra Perch
Book Image

Learning JavaScript Robotics

By: Kassandra Perch

Overview of this book

Table of Contents (16 chapters)
Learning JavaScript Robotics
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using PWM pins with the Piezo element


Piezo elements can be really fun—you can use them to simply add music to your Johnny-Five projects. We're going to build a small project and play with some of the Johnny-Five utilities to make music with this fun little component.

Setting up the hardware

Wiring a Piezo is pretty straightforward—you need to determine the + and - sides first. Usually, the + side is marked on the top of the plastic casing of the Piezo, and one leg is longer—much like an LED, the longer leg denotes the + side. Finally, some Piezo buzzers come with lead wires that are red (the + side) and black (the - side).

Once you've determined the + side, wire it to pin 3, and wire the - side to GND, as shown in the following diagram:

A Piezo wiring diagram

Writing the script

The script is a little more complicated than our last few scripts; we need to create a Piezo element, which just requires a pin. However, a piece of music is more complicated than turning an LED on and off. Luckily, the...