Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Matha Goram
Published © GPL3+

Piezo Beeps Buzzers

This note examines the simplest way to generate sound using piezo-electric buzzers (active/passive) with an Arduino board microcomputer.

BeginnerFull instructions provided30 minutes2,567
Piezo Beeps Buzzers

Things used in this project

Hardware components

ELEGOO UNO R3 Board ATmega328P ATMEGA16U2 with USB Cable
ELEGOO UNO R3 Board ATmega328P ATMEGA16U2 with USB Cable
×1
Elegoo Active Buzzer
×1
Elegoo Passive Buzzer
×1
ELEGOO 130pcs Solderless Flexible Breadboard Jumper Wires 4 Different Lengths Male To Male
ELEGOO 130pcs Solderless Flexible Breadboard Jumper Wires 4 Different Lengths Male To Male
×1
Sunfounder baseplate
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Test assembly for active buzzer with Arduino

Assembly layout for a simple test exercise using an active buzzer with Arduino UNO.

Test assembly for passive buzzer with Arduino

Assembly layout for a simple test exercise using a passive buzzer with Arduino UNO.

Schematics

Active Buzzer Schematic

Schematic diagram to indicate connections between Arduino microcomputer and an active buzzer

Passive Buzzer Schematic

Schematic diagram to indicate the connections between Arduino microcomputer and a passive buzzer

Code

ElegooBuzzer-03.ino

Arduino
Generate sound using a piezo-electric passive buzzer
/*
 * ElegooBuzzer-03.ino
 * A basic set to test a passive buzzer
 * 2018-10-12
 * armw
 * v0.1
 * © 2018 <reza@parkcircus.org> All Rights Reserved
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.  
 * 
 * Reference:
 * https://www.arduino.cc/en/Tutorial/ToneMelody
 */

#include "pitches.h"

#define pinBuzzer 9   // board pin reference number

struct notes {
  int note;           // using definition in pitches.h
  int duration;       // milliseconds for correponding note?
                      // 4 = quarter note, 8 = eighth note, etc.:
} melody[] =
{
  {NOTE_E7, 12},  {NOTE_E7, 12},  {0, 12},        {NOTE_E7, 12},
  {0, 12},        {NOTE_C7, 12},  {NOTE_E7, 12},  {0, 12},
  {NOTE_G7, 12},  {0,  12},       {0,  12},       {0,  12},
  {NOTE_G6, 12},  {0, 12},        {0, 12},        {0, 12},
 
  {NOTE_C7, 12},  {0, 12},        {0, 12},        {NOTE_G6, 12},
  {0, 12},        {0, 12},        {NOTE_E6, 12},  {0, 12},
  {0, 12},        {NOTE_A6, 12},  {0, 12},        {NOTE_B6, 12},
  {0, 12},        {NOTE_AS6, 12}, {NOTE_A6, 12},  {0, 12},
 
  {NOTE_G6, 9},   {NOTE_E7, 9},   {NOTE_G7, 9},
  {NOTE_A7, 12},  {0, 12},        {NOTE_F7, 12},  {NOTE_G7, 12},
  {0, 12},        {NOTE_E7, 12},  {0, 12},        {NOTE_C7, 12},
  {NOTE_D7, 12},  {NOTE_B6, 12},  {0, 12},        {0, 12},
 
  {NOTE_C7, 12},  {0, 12},        {0, 12},        {NOTE_G6, 12},
  {0, 12},        {0, 12},        {NOTE_E6, 12},  {0, 12},
  {0, 12},        {NOTE_A6, 12},  {0, 12},        {NOTE_B6, 12},
  {0, 12},        {NOTE_AS6, 12}, {NOTE_A6, 12},  {0, 12},
 
  {NOTE_G6, 9},   {NOTE_E7, 9},   {NOTE_G7, 9},
  {NOTE_A7, 12},  {0, 12},        {NOTE_F7, 12},  {NOTE_G7, 12},
  {0, 12},        {NOTE_E7, 12},  {0, 12},        {NOTE_C7, 12},
  {NOTE_D7, 12},  {NOTE_B6, 12},  {0, 12},        {0, 12}
};

bool played = false;

void setup() {
  pinMode(pinBuzzer, OUTPUT);
  played = false;
}

void loop() {
  int noteDuration = 0, pauseBetweenNotes = 0;
  if (!played)
  {
    played = !played;
                          // iterate over the notes of the melody
    for (int currentNote = 0; currentNote < sizeof(melody)/sizeof(melody[0]); currentNote++)
    {
                          // to calculate the note duration,
                          // take one second divided by the note type
                          //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      noteDuration = 1000 / melody[currentNote].duration;
      tone(pinBuzzer, melody[currentNote].note, noteDuration);
                          // to distinguish the notes, set a minimum time between them.
                          // the note's duration + 30% seems to work well:
      pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
                          // stop the tone playing:
      noTone(pinBuzzer);
    }
  }
}

ElegooBuzzer-06.ino

Arduino
A basic example to test a piezo-electric active buzzer
/*
 * ElegooBuzzer-06.ino
 * A basic set to test an active buzzer
 * 2018-10-12
 * armw
 * v0.1
 * © 2018 <reza@parkcircus.org> All Rights Reserved
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.  
 * 
 * Reference:
 */

#define pinBuzzer A0          // analog pin for active buzzer

bool beeped = false;          // iteration variable

void setup()
{
  pinMode(pinBuzzer, OUTPUT); // housekeeping
  beeped = false;             // redundant initialization
}

void beep(int pinAnalog, byte intensity, int duration)
{
  analogWrite(pinAnalog, intensity); // the lower values of intensity, < 10, may have inconsistent effect
  delay(duration);            // duration, ms
  analogWrite(pinAnalog, LOW);// turn off signal
}

void loop()
{
  if (!beeped)                  // itz bin dun?
  {
    beeped = !beeped;           // only one cycle, please!
    for (byte i = LOW + 1; i < HIGH; i++) // a nominal iteration to assess intensity on emanating tone
    {
      beep(pinBuzzer, i, i * 1000); // unleash the signal
      delay(5000);              // hold for 5 secs
    }
  }
}

Credits

Matha Goram
27 projects • 22 followers
Working with discrete electronic components for a very long time but still suffering from the occasional dry soldering results.
Contact

Comments

Please log in or sign up to comment.