Project tutorial
DC piezo buzzer volume control

DC piezo buzzer volume control © GPL3+

A novel variant of the Arduino Fade sketch

  • 978 views
  • 2 comments
  • 0 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
DC piezo buzzer
The type with an in built driver circuit
×1

Apps and online services

About this project

I quite often use a DC piezo buzzer for the sound element of my circuits I find these buzzers give a much louder sound level than can be obtained simply by using the tone() command to drive a standard piezo element.

In some circumstances I find the sound level too loud, so experimentally I tried using PWM to see if I could alter the sound level. My initial tests were somewhat discouraging, I found I could alter the sound volume but the tone output seemed distorted.

I wondered if I could get a less distorted sound by altering the PWM frequency, so using the Arduino PWM frequency cheat sheet [1] I worked my way through the possible pin and frequency combinations eventually settling on the frequency of 31372.55Hz which is available on both pins 9 & 10 (timer1) and 3 & 11 (timer 2) I found this frequency to give the cleanest sound.

Hardware description:

The circuit consists of an Arduino UNO and a DC piezo sounder element.

The ground end of the DC piezo sounder is connected to ground, the positive to PWM pin 11 which provides the drive signal.

Software overview:

99% of the code consists is the Fade sketch which you can find in the Arduino IDE under File->Examples->01.Basics->Fade.

I altered the PWM pin and added in code to set the PWM frequency. I renamed some of the variables for clarity.

Failure:

Buoyed by my success I attempted to control the brightness of a 5 volt flashing LED. I tried a variety of PWM frequency’s and altered the step delay to 3 seconds thinking there may be a capacitor that needed charging within the device. I had no success in getting this to work.

Addendum:

If you want to use PWM to drive a standard piezo element I found that maximum volume could be achieved by altering the PWM frequency to one that closely matches the resonant frequency of the piezo element.

References:

[1] https://playground.arduino.cc/Main/TimerPWMCheatsheet (Arduino PWM cheat sheet, checked 27/March/2020)

Code

Modified fade sketchC/C++
/*
 Fade

 This example shows how to fade an buzzer on pin XX
 using the analogWrite() function.

 The analogWrite() function uses PWM, so if
 you want to change the pin you're using, be
 sure to use another PWM capable pin. On most
 Arduino, the PWM pins are identified with 
 a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

 This example code is in the public domain.
 */

/*
 * following cut from https://playground.arduino.cc/Main/TimerPWMCheatsheet  
How to adjust Arduino PWM frequencies by macegr in this forum post http://forum.arduino.cc/index.php?topic=16612#msg121031

Pins 11 and 3: controlled by timer 2 in phase-correct PWM mode (cycle length = 510)

Setting   Divisor   Frequency
0x01      1       31372.55
0x02      8       3921.16
0x03      32      980.39
0x04      64      490.20   <--DEFAULT
0x05      128     245.10
0x06      256     122.55
0x07      1024    30.64

TCCR2B = (TCCR2B & 0b11111000) | <setting>;

All frequencies are in Hz and assume a 16000000 Hz system clock.

*/

int buzzer = 11;           // the PWM pin the buzzer is attached to
int volume = 0;          // how loud the buzzer is
int fadeAmount = 5;     // how many points to fade the buzzer volume by

int duty_cycle = 127;         // 0-255 so 50% = 127 approx

int setting=0x01;

// the setup routine runs once when you press reset:
void setup() {

    TCCR2B = TCCR2B & 0b11111000 | setting;//to adjust divider for timer
    
  // declare buzzer pin to be an output:
  pinMode(buzzer, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the volume of pin 9:
  analogWrite(buzzer, volume);

  // change the volume for next time through the loop:
  volume = volume + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (volume <= 0 || volume >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 100 milliseconds to see the volume effect
  delay(100);
}

Comments

Similar projects you might like

DC Motor speed control and measurement

Project tutorial by ambhatt

  • 7,291 views
  • 2 comments
  • 9 respects

Tone() with 8-bit Volume Control - No Extra Components!

Project tutorial by Connor Nishijima

  • 16,399 views
  • 1 comment
  • 14 respects

Control Music Volume and Ledbar Using Slider of 1Sheeld

Project tutorial by ahmed ismail

  • 2,119 views
  • 2 comments
  • 14 respects

Control an LED with the Remote Control

Project showcase by Nicholas_N

  • 12,274 views
  • 7 comments
  • 18 respects

Control the Speed of Brushless DC Motor Using Bluetooth

Project tutorial by mohitmehlawat

  • 11,048 views
  • 3 comments
  • 20 respects
Add projectSign up / Login