You can also control a piezo buzzer from Arduino in a similar way as CircuitPython with Arduino's tone function.  There are actually quite a few resources and guides for using Arduino to play tones on a piezo, so this page will just highlight how to use a servo in Arduino in the same way as with CircuitPython from the previous page.  For more exploration of piezo and Arduino be sure to check out the entire Arduino Lesson 10: Making Sounds.

First make sure your piezo is wired to your board exactly as shown on the hardware page of this guide.  You'll also need the Arduino IDE installed and configured to upload to your board.  Remember Arduino sketches have to be written entirely up front and uploaded to the board--you can't interactively control servos like you can with CircuitPython.

With Arduino you use the tone function to play a 50% duty cycle (i.e. square wave) on any digital output.  The function needs to be told the pin and frequency of the tone as one of the parameters and will immediately start playback of the tone/signal.  To stop playback you just call the noTone function (providing only the pin).

Here's a complete Arduino sketch that uses the tone function to play a scale of notes just like the CircuitPython example on the previous page.  Upload this to your board:

#define PIEZO_PIN  5      // Pin connected to the piezo buzzer.

// Define list of tone frequencies to play.
int toneFreq[] = { 262,   // C4
                   294,   // D4
                   330,   // E4
                   349,   // F4
                   392,   // G4
                   440,   // A4
                   494 }; // B4
int toneCount = sizeof(toneFreq)/sizeof(int);

void setup() {
  // No setup necessary!

void loop() {
  // Loop up through all the tones from start to finish.
  for (int i=0; i < toneCount; ++i) {
    tone(PIEZO_PIN, toneFreq[i]);
    delay(500);  // Pause for half a second.
  // Loop down through all the tones from finish to start again.
  for (int i=toneCount-1; i >= 0; --i) {
    tone(PIEZO_PIN, toneFreq[i]);
  // Remember you can stop tone playback with the noTone function:
  // noTone(PIEZEO_PIN);

After uploading you should hear the piezo play back each tone in sequence up and down.  That's all there is to using a piezo buzzer with Arduino to play tones!

Last updated on 2018-01-05 at 02.35.59 PM Published on 2018-01-05 at 02.36.17 PM