# Tone Duty Cylce

**URL:** <https://community.particle.io/t/tone-duty-cylce/11126>\
**Category:** General\
**Created:** [April 7, 2015, 5:40pm UTC](https://community.particle.io/t/tone-duty-cylce/11126 "2015-04-07T17:40:41Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![wesner0019](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@wesner0019](https://community.particle.io/u/wesner0019)\
**Post date:** [December 6, 2016, 4:36pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/21 "2016-12-06T16:36:45Z")

</div>

@peekay, that sounds complicated! Thanks for looking into this!, I’m glad you know what you’re doing!

---

<div class="post-metadata">

**Author:** ![wesner0019](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@wesner0019](https://community.particle.io/u/wesner0019)\
**Post date:** [December 6, 2016, 8:32pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/22 "2016-12-06T20:32:28Z")

</div>

@peekay123, looking at the added functionality of using the duty cycle on analogwrite the below controls the volume of the buzzer.

This might be an intermediate use for the other tone libraries.

Do you know if changing the duty cycle would affect anything else?

```auto
#include "application.h"
#define BuzzerPin A7 //Buzzer for sound notification

int maxFreq = analogWriteMaxFrequency(BuzzerPin);

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);

void setup(){

  Serial.begin(9600);
  pinMode(BuzzerPin, OUTPUT); // sets the pin as output

  analogWriteResolution(BuzzerPin, 12); // sets analogWrite resolution to 12 bits

  analogWrite(BuzzerPin, 204, 2000); // 204/4095 = ~5% duty cycle
  delay(1000);
  analogWrite(BuzzerPin, 0, 2000); // 0/4095 = 0% duty cycle
  delay(1000);
  analogWrite(BuzzerPin, 2046, 2000); // 2046/4095 = ~50% duty cycle max volume
  delay(1000);
  analogWrite(BuzzerPin, 0, 2000); 

}
 void loop(){

 }

```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 6, 2016, 8:40pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/23 "2016-12-06T20:40:00Z")

</div>

@wesner0019, the duty cycle dictates the total “on” time for a given period set by the frequency. The volume is loudest at 50% duty cycle when using a single pin for driving the buzzer. The ToneAC library uses a “push-pull” method for achieving a simulated AC signal across the buzzer. Using two pins, one pin is driven low for while the other pin is driven high and vice versa over the duty cycle and frequency of the tone. This effectively doubles the amplitude/volume of the sound produced by the buzzer. In your code, changing the duty cycle will have an effect but it will not increase the volume.

---

<div class="post-metadata">

**Author:** ![wesner0019](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@wesner0019](https://community.particle.io/u/wesner0019)\
**Post date:** [December 6, 2016, 9:10pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/24 "2016-12-06T21:10:04Z")

</div>

@Thanks for the explanation, but in our certain scenario, our buzzer is too loud at the standard 50% duty cycle so we need a way to decrease it. The 50% duty cycle can be too annoying to some of our customers.

But the ToneAC library would be good to have to increase the volume for our older aged customer base.

---

<div class="post-metadata">

**Author:** ![wesner0019](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@wesner0019](https://community.particle.io/u/wesner0019)\
**Post date:** [June 21, 2017, 4:44pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/25 "2017-06-21T16:44:43Z")

</div>

@peekay123 Hi Im looking at the ToneAC library again for a new project.

I’m wondering if you ever had a chance to port it?

thanks

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [June 21, 2017, 5:15pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/26 "2017-06-21T17:15:35Z")

</div>

@wesner0019, no I haven’t ported it. However, looking at the code, it looks like a variant of Tone() with variable PWM duty cycle to vary the volume. The Tone library uses Timer Output Compare interrupts to reload the timer register, changing the frequency. I’d have to look if varying the duty cycle (thus the volume) is also possible.

---

<div class="post-metadata">

**Author:** ![wesner0019](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@wesner0019](https://community.particle.io/u/wesner0019)\
**Post date:** [June 21, 2017, 7:00pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/27 "2017-06-21T19:00:48Z")

</div>

Ok thanks!

---

<div class="post-metadata">

**Author:** ![Jerware](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jerware/32/1516_2.png) [@Jerware](https://community.particle.io/u/Jerware)\
**Post date:** [May 17, 2018, 4:32am UTC](https://community.particle.io/t/tone-duty-cylce/11126/28 "2018-05-17T04:32:14Z")

</div>

I was excited to find this thread but sad to see it fizzled. Would love to see one of these libraries ported by a savvy individual.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 17, 2018, 10:15am UTC](https://community.particle.io/t/tone-duty-cylce/11126/29 "2018-05-17T10:15:58Z")

</div>

What exactly are you looking for where you’d need this?  
Maybe there are also other ways to get your intended job done.

---

<div class="post-metadata">

**Author:** ![Jerware](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jerware/32/1516_2.png) [@Jerware](https://community.particle.io/u/Jerware)\
**Post date:** [May 17, 2018, 4:26pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/30 "2018-05-17T16:26:08Z")

</div>

I’m looking to dynamically adjust the volume of a piezo buzzer via software.

---

<div class="post-metadata">

**Author:** ![wesner0019](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@wesner0019](https://community.particle.io/u/wesner0019)\
**Post date:** [May 17, 2018, 5:23pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/31 "2018-05-17T17:23:45Z")

</div>

You can use this to decrease from the maximum volume. Set by the voltage of your buzzer. So if you use Tone right now that would be the maximum volume but the below will make the volume quieter. So its volume adjustment it just that you cant make it louder than what your piezo is currently at.

```auto
#include "application.h"
#define BuzzerPin A7 //Buzzer for sound notification

int maxFreq = analogWriteMaxFrequency(BuzzerPin);

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);

void setup(){

  Serial.begin(9600);
  pinMode(BuzzerPin, OUTPUT); // sets the pin as output

  analogWriteResolution(BuzzerPin, 12); // sets analogWrite resolution to 12 bits

  analogWrite(BuzzerPin, 204, 2000); // 204/4095 = ~5% duty cycle
  delay(1000);
  analogWrite(BuzzerPin, 0, 2000); // 0/4095 = 0% duty cycle
  delay(1000);
  analogWrite(BuzzerPin, 2046, 2000); // 2046/4095 = ~50% duty cycle max volume
  delay(1000);
  analogWrite(BuzzerPin, 0, 2000); 

}
 void loop(){

 }

```

---

<div class="post-metadata">

**Author:** ![Jerware](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jerware/32/1516_2.png) [@Jerware](https://community.particle.io/u/Jerware)\
**Post date:** [May 17, 2018, 6:35pm UTC](https://community.particle.io/t/tone-duty-cylce/11126/32 "2018-05-17T18:35:52Z")

</div>

Thanks! I had read in my searches that this method wouldn’t work, but I do hear a volume difference. In attempting a full sweep from 0-4095, it appears only values below 512 produce a noticeably softer tone, at least on my piezo. Still, I think this should do the trick!

[Previous page](https://community.particle.io/t/tone-duty-cylce/11126.md?page=1)
