user1499: 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.

user4143: 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.

user85890: 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.

1499: <p>I am following this guide to produce tones with the arduino:
<a href="https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody" rel="nofollow noreferrer">https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody</a></p>
<p>This uses the <code>tone()</code> 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.</p>
<p>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.</p>
 4143: <p>The <code>tone()</code> function generates a 50% duty cycle square wave using a timer and interrupts. 50% duty cycle is basically full volume.</p>
<p>It's possible to reduce the volume by reducing the duty cycle, but the <code>tone()</code> 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.</p>
<p>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 &quot;attenuator&quot; before it to change the voltate of the signal going to the op-amp.</p>
<p>You need the op-amp as a buffer because the speaker will mess with the potentiometer otherwise.</p>
 85890: <p>There is a <a href="https://hackaday.io/project/11957-10-bit-component-less-volume-control-for-arduino" rel="nofollow noreferrer">library</a> from LixieLabs that implements an audio-based PWM at super high frequencies that allows you to change the tone volume without additional hardware.</p>
