Tone Duty Cylce

Can the TONE duty cycle be changed so that the volume could be adjusted? Right now I have a buzzer connected with NMOSFET and the gate attached to a tone pin. Would changing the duty cycle on the gate of the NMOSFET adjust volume?


When you change the duty cycle, if possible, the sound will change to a sharper sound. This due to the harmonical structure of the wave. To experience this connect your buzzer to a PWM output of the spark and write different values with analog write to the output. You will hear the tonal quality change of the sound but not the volume.

What you want is not so easy to accomplish with a micro controller without a real DAC, may by the upcoming photon can realize better sounds since it has a DAC.

1 Like

Thanks, How would the DAC be able to control to volume? I would plan on using this pin connected to an NMOSFET gate to allow for driving of a magnetic buzzer.

@wesner0019, you will not be able to control the volume of a buzzer that way. I believe most buzzers are fixed volume anyway. Can you provide data on the buzzer?

@peekay123, this is the style of buzzer i will be using for lower cost and high volume, but if you have other suggestions I’d be open to them.

@wesner0019, those types of buzzers are not designed for variable volume control. As @marcus pointed out, audio volume control is not like controlling the speed of a stepper where PWM works great. You could try to see if the volume of the buzzer goes down at a PWM frequency and duty cycle but at a lower voltage. If that works, then you would need a circuit which allows you to control the amplitude of the PWM going to the buzzer.

To get a true volume control you would need a speaker and amplifer with the ability to control its gain for volume. There are I2C controllable mono amplifer chips available on the market. :smile:


Thanks for the reply, its getting too complicated/expensive to implement so I leave out volume control.

1 Like

@peekay123, whats your thought on this library? Do you think this is possible for the P1?

@wesner0019, yup I think that can be ported using SparkIntervalTimer and some porting magic! :wink:


@peekay123 thats awesome, If I sent you a check for $$ do you think this is a library you could port over?

@peekay123, i saw this. Are there any limitations of the maximum frequency of the PWM on the P1 pins?

Volume2 is limited to certain pins:

This is because only two pins are driven by Timer0, which can do PWM at a frequency higher than your hearing range!

@wesner0019, the P1 pins which fall under direct control of a timer are only limited by the timer’s frequency. I would say supersonic 40KHz could be done easily for example.

1 Like

@wesner0019, hold on to your money for now. I will be looking at porting it soon. :grinning:

1 Like

@peekay123 sweet! Do you think this would be possible to select what timers you’d want to use. In my case i have my speaker on pin A7 on the P1 (TIM5_CH1).

Any pin usable by Tone() will work IMO.

1 Like

@peekay123, thanks also just wondering how long it might take to port over this library?

@wesner0019, I haven’t had to chance to really look at it yet. I’m looking at updating SparkIntervalTimer to bypass the HAL in order to reduce interrupt latency. I’ll be looking at the port soon though.


Cool Thanks for the update

Hi @peekay123, I’m sure you’ve got tons of projects going on, just checking on if you’ve been able to look at porting this library.

I have another product we’re developing and it also includes a buzzer. I’m at cross roads to see if this library will work for volume control of if I’ll have to try to use a pot, or since this is a new design I could also try the ToneAC library.

Do you know if this library has been ported too?

I really appreciate you’re help on this!

@wesner0019, my brain is hurting with all the libraries I want to port. The STM32 timer configuration is totally different than those of Arduino devices. Add to that the fact that the HAL isolates the user app from the hardware and that creates a storm in a teacup. Over the next few weeks I want to test a way to hook into the low level ISR table without upsetting the HAL. I was want to creat a new timer library, beyond SparkIntervalTimer, to give access to untapped STM32 timer capabilities. Once that is done, porting the ToneAC and other libraries will be a lot simpler. :wink:

1 Like