I2S Player (Part 4) : Adding volume control

In this article we add in volume control and go through the principles of changing the volume of digital samples. Video covering everything just below, source code/circuit even further below! If you’ve not seen the previous episodes then these are essential reading/watching. This was the last one – or use the menus above.

PARTS LIST (AFFILIATE LINKS)
ESP32 : https://amzn.to/3kb02n8 ESP32 – Pack of three : https://amzn.to/2XfIRqH I2S Decoder : MAX98357A : https://amzn.to/3fkHEnU SD Card Reader : https://amzn.to/2Q9Fb5V 3W Speaker : https://amzn.to/2XeRP7i 5 piece 10K potentiometers : https://amzn.to/3hvgrRG Selection box of potentiometers : https://amzn.to/3ld1Lsx Breadboards : https://amzn.to/30fWibZ Wires : https://amzn.to/3k4PKoC Pins and sockets : https://amzn.to/39O7a3K (I2S board doesn’t come with them)

CIRCUIT DIAGRAM

PRINCIPLES
How do we reduce volume, remember this is digital data, so if we reduce the size of the numbers we will reduce the volume. Simply multiplying the numbers by say 0.5 would halve the volume, times by 1 and it would remain the same, times by 1.5 and it would go louder etc. etc. In code we now know we’ll just use a decimal number somewhere probably in the range 0 to 2 would give a volume range between zero – silence and twice as loud. We just need a way for the use to change this number. And it’s that simple, all we need now is some sort of input for our ESP32 to use to set the volume. So Let’s look at the options.

INPUTS
You could have two buttons, a volume up and a volume down, looking at which is pressed in code in order to then manipulate the volume variable. Or you could have a dial that you turn that sets the volume. I’m going to show the dial version. Here it is, a simple potentiometer, the value of this is 10K but you could use 100K or 1 meg, it makes no difference to how this will work. The only thing I would say would be don’t go lower than 10K as the lower you go the more current will be used needlessly.

The reason it doesn’t matter what the value is, is because we are going to wire this as a potential divider, as you can see in the circuit diagram (and on the video if you’ve watched it) there are 3 connections, if we wire one side to ground, one side to Vcc then the middle will out out a voltage that is proportional to the position of the dial, cool, eh!

We can measure voltages on virtually all micro-controllers and the ESP32 is no exception, on fact it has many Analog to Digital Converters or ADC’s available and we can use most pins to access them. In my example just for convenience of my breadboard layout I’m going to use pin 14.

THE CODE
Wav Player With Volume

ROUND-UP
That wraps up this article, next time I’ll look at mixing more than one sound together, i.e. playing more than one sound at the same time -at least I think that’s my plan.

3 Comments

  1. Very nice. Demos worked, which can always be a challenge; thanks for the details. I understand a lot more now than I wanted to know.

  2. Hi, Great work on the I2S, not many good tutorials out there.
    Right now I am struggling with using multi channel ADC. I am using I2s to sample 1 channel and i want to read potmeters on several other ADC pins….can’t get it to work. I know I2s can be configured to readout several channels but can’t get it to work? Any experience with that?

Comments are closed.