-1

I am following this guide to produce tones with the arduino: https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody

This uses the tone() builtin function. The connections is that one pin of the speaker got to one digital pin of the arduino, while the other goes to ground.

Is there a way to perform volume control (perhaps with a potentiometer) on this setup? What worries me is that this is digital information (PWM) that goes to the speaker, and not an analog signal that can be adjusted.

3
  • one option: github.com/teckel12/arduino-toneac/blob/master/README.creole
    – Juraj
    Commented Jun 16, 2022 at 17:50
  • @Juraj Thanks! I used this but unfortunately the sound quality is not as good as the original tone() function. In addition to that, when the sound was at the lowest, i could hear i clicking sound when every not was heard. However, i went back to the original tone() function and that clicking sound existed, however it was not as easily identified because the tone() is louder that the toneAC. Commented Jun 23, 2022 at 17:54
  • @Juraj I asked a new question about it here: arduino.stackexchange.com/questions/89993/… Commented Jun 23, 2022 at 17:59

2 Answers 2

3

There is a library from LixieLabs that implements an audio-based PWM at super high frequencies that allows you to change the tone volume without additional hardware.

1
2

The tone() function generates a 50% duty cycle square wave using a timer and interrupts. 50% duty cycle is basically full volume.

It's possible to reduce the volume by reducing the duty cycle, but the tone() function does not support it. It is of course possible to change the software PWM generation to change the duty cycle (I wouldn't know how, so don't ask me), but I'll leave that as an academic exersize for you to experiment with should you wish.

An alternative is to do it in hardware. You can use an op-amp as buffer (or speaker driver) and a couple of resistors (or a potentiometer) as an "attenuator" before it to change the voltate of the signal going to the op-amp.

You need the op-amp as a buffer because the speaker will mess with the potentiometer otherwise.

2
  • Thank you! So is it possible to fix this with hardware alone? I will definately try this. I have another question that maybe you may know. At the beggining of each note, a clicking sound is heard and i don't know why, or if it is possible to mitigate it. Perhaps with an analog filter? I created a question here: arduino.stackexchange.com/questions/89993/…. Thank you so much! Commented Jun 23, 2022 at 18:02
  • Is this how the connections to the system should be? imgur.com/a/tgK0hZ2 Did i understand your advice correctly? Commented Jun 23, 2022 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.