QC

DC piezo buzzer volume control

A new variation of the Arduino Fade sketch

Devices and components

Arduino Uno Rev3

DC piezo buzzer

Software and tools

Arduino IDE

Project description

Hardware Description:

Software overview:

Failure:

Addendum:

Modified fade sketch

1/*
2 Fade
3
4 This example shows how to fade an buzzer on pin XX
5 using the analogWrite() function.
6
7 The analogWrite() function uses PWM, so if
8 you want to change the pin you're using, be
9 sure to use another PWM capable pin. On most
10 Arduino, the PWM pins are identified with
11 a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
12
13 This example code is in the public domain.
14 */
15
16/*
17 * following cut from https://playground.arduino.cc/Main/TimerPWMCheatsheet
18How to adjust Arduino PWM frequencies by macegr in this forum post http://forum.arduino.cc/index.php?topic=16612#msg121031
19
20Pins 11 and 3: controlled by timer 2 in phase-correct PWM mode (cycle length = 510)
21
22Setting Divisor Frequency
230x01 1 31372.55
240x02 8 3921.16
250x03 32 980.39
260x04 64 490.20 <--DEFAULT
270x05 128 245.10
280x06 256 122.55
290x07 1024 30.64
30
31TCCR2B = (TCCR2B & 0b11111000) | <setting>;
32
33All frequencies are in Hz and assume a 16000000 Hz system clock.
34
35*/
36
37int buzzer = 11; // the PWM pin the buzzer is attached to
38int volume = 0; // how loud the buzzer is
39int fadeAmount = 5; // how many points to fade the buzzer volume by
40
41int duty_cycle = 127; // 0-255 so 50% = 127 approx
42
43int setting=0x01;
44
45// the setup routine runs once when you press reset:
46void setup() {
47
48 TCCR2B = TCCR2B & 0b11111000 | setting;//to adjust divider for timer
49
50 // declare buzzer pin to be an output:
51 pinMode(buzzer, OUTPUT);
52}
53
54// the loop routine runs over and over again forever:
55void loop() {
56 // set the volume of pin 9:
57 analogWrite(buzzer, volume);
58
59 // change the volume for next time through the loop:
60 volume = volume + fadeAmount;
61
62 // reverse the direction of the fading at the ends of the fade:
63 if (volume <= 0 || volume >= 255) {
64 fadeAmount = -fadeAmount;
65 }
66 // wait for 100 milliseconds to see the volume effect
67 delay(100);
68}
69




Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.

Easy remote control

Remotely control an LED with ease. Devices and components Arduino Nano 33 BLE with headers Box 525 Resistors precision 1% - 17 values Breadb...