When it comes to music or any audio which is reproduced by any electronical device, we usually never think about the whole process which stays behind the transformation of our audio files into a hearable audio signal. Everything starts with audio files, which contains a digital representation of sound. Digital sounds are essentially a sequence of binary data that represents the amplitude of the sound wave at different points in time. However, in order for humans to actually hear this sound, the digital data needs to be converted into an analog signal that can be played through a speaker or headphones. This conversion process involves several steps, including decoding the digital data, converting it to an analog signal, amplifying it, and finally, playing it through a transducer like a speaker or headphone driver. The process is complex, but it is essential for making digital audio files hearable to human listeners. In this response, we will explore the steps involved into the amplification part, which is obtained through the so-called audio amplifiers. In particular we will see how a specific TPA2016 IC can be used to simply create a powerful and versatile amplifier.
Audio amplifier, what are they?
An audio amplifier is an electronic device designed to increase the strength of an audio signal. They are an essential part of sound systems, from small home systems to large concerts.
An audio amplifier works by taking in input a low-power audio signal and increases its voltage, current and therefore the overall power, to a level that can drive a speaker (Image 1).
They come in different types, for example solid state or tubes and can be classified based on their power, class, number of channels or other features. Audio amplifiers are crucial in delivering high-quality sound with clarity, dynamics, and tonal accuracy, making them a vital part of the audio industry (Image 2).
TPA2016 amplifier is a class D, so we will here see in more details what this is about.
Class D amplifier working principle
Class D amplifiers are a type of audio amplifier that use pulse-width modulation (PWM) to amplify an audio signal. Unlike traditional analog amplifiers, which use linear amplification to increase the amplitude of the audio signal, Class D amplifiers switch the audio signal on and off rapidly to create a digital pulse train. These pulses are then filtered to remove the high-frequency components and produce an amplified audio signal that can be sent to a speaker. Here follows a block schema of a classical class D amplifier: (Image 3)
The input signal is compared through a voltage comparator with a triangular wave generator. The generated output is then a sort of PWM square wave, where the wave high level period is directly proportional to the amplitude of the input signal. This operation is basically a modulation (Triangular wave frequency is much higher than the audio signal).
The PMW square wave is then amplified through a switching amplifier, which can amplify the PWM signal with a very high efficiency. The final output amplified signal is then filtered using a low pass filter, to obtain again the original audio signal, but amplified.
Class D amplifiers are known for their high efficiency, making them ideal for portable and battery-powered applications. They also produce less heat and are smaller in size compared to traditional analog amplifiers, which makes them suitable for space-constrained designs. However, they can be more complex and have higher distortion than analog amplifiers, which can affect the overall sound quality. Despite this, Class D amplifiers have become increasingly popular in the audio industry due to their efficiency, power, and compact size.
The TPA2016D2 IC
The TPA2016D2 (here called simply TPA2016) is a class-D audio amplifier IC (integrated circuit) designed by Texas Instruments. It is a highly efficient stereo amplifier that can deliver up to 2.8 watts of power for channel into a 4-ohm speaker, or 1.7 Watts per channel with an 8-ohm speaker with a supply voltage of 5V. The TPA2016 includes features like gain control, shutdown and thermal protection to ensure high-quality sound and reliable performance.
The TPA2016 is commonly used in portable audio devices, such as smartphones, tablets, and portable speakers, where its low power consumption and high efficiency are especially advantageous. Its compact size, ease of use, and advanced features have made it a popular choice among audio designers looking for a high-quality, cost-effective solution.
One of the key points of the TPA2016 is the I2C interface, which allows to do multiple runtime configurations using a microcontroller (An STM32 in our case).
I bought my TPA2016 sample from Adafruit: https://www.adafruit.com/product/1712. This comes already soldered into a small evaluation board, making prototyping very easy.
From theory to practice: how to implement a simple volume controlled amplifier
After some introduction, let’s go practical. The TPA2016 has many regulations and configurations which can be controlled directly through I2C, but in this example we will consider mainly the gain configuration. The gain is actually one of the most important parameters of an amplifier, which basically says how much times the input signal will be amplified by the amplifier. In other terms this can be considered as a number which multiplied to the input signal gives the output signal. TPA2016 does not have a specific volume regulation functionality, so we will act on the gain to obtain a similar effect (Image 4).
How to implement a volume control using a gain control
From the above image, extracted from the Texas Instrument datasheet, is possible to see the gain management. The gain is correlating directly the input signal to the output signal.
Here follows the link to the datasheet, where more detailed information can be found: https://www.ti.com/lit/ds/symlink/tpa2016d2.pdf?ts=1679235041811&ref_url=https%253A%252F%252Fwww.google.com%252F
The TPA2016D2 is almost an “automatic” amplifier, containing specific features to automate any process involved in the overall audio quality, ensuring always the best audio quality in any condition.
Features like the automatic gain control or the compressor, allows to have always the best audio quality in any condition and with any audio input. Of course, all this automation comes with a cost, which translates in reduced flexibility and a reduced possibility of manual intervention.
This, in fact, has a direct impact also on the volume control: this amplifier does not have a real volume control, but only a gain control, which is strongly influenced by the automatic gain control and the compressor. In order to achieve something comparable to a volume control, we have to disable some of these automations and in particular:
- Disable the compressor completely (and therefore the AGC)
- Reduce to the minimum the AGC release time, to improve gain changes reactiveness.
Now, that has been decided that we will act on the gain configuration to adjust the volume, we would need some sort of actuator, meaning, some physical lever which will allow to change this value at runtime in an easy way.
I2C Encoder from DuPPa
After some internet research, I found this very powerful I2C encoders produced by a company named DuPPa. Here follows the link: https://www.duppa.net/shop/i2cencoder-v2-1-with-soldered-accessory/ (Image 5).
These encoders are very easy to use and the product comes already with a lot of documentation which is available on the website. Moreover, Arduino libraries are available on GitHub.
For our use case, we are interested on detecting the rotations of the lever in order to change the amplifier gain, but the encoder is capable of handling also lever clicks.
As we can see from an extract of the register set of the encoder (Image 6), the current position can be read from the CVAL register, so it will be enough to read this register to know the exact encoder position.
How to setup the encoder
The ranges between which, the CVAL register value can be changed are also configurable, through register CMAX and CMIN and in our case, we will configure them to work in a range from 0 to 16.
Register ISTEP will be set in our example to 1, so that every single movement will just change the CVAL register of one unit.
An interesting feature is the Interrupt pin which can be used to generate a physical interrupt every time an event is detected (rotation, click, etc..). The Interrupt pin can be configured through the INTCONF register. Every time an interrupt will be detected, the current CVAL value will be read and used to program the amplifier’s gain.
The amplifier has a gain range from 0 to 32 (with AGC disabled) and therefore it will be simply enough to multiply by two the value of the encoder and then set it into the gain register of the TPA2016.
Here follows a block schematic (Image 7) of the whole idea and the real electrical schematic of the prototype (Image 8).
Software
You can now download the whole project from GitHub here: https://github.com/EmbeddedEspresso/amplifier_encoder. This is an STM32CubeIDE project, so you can compile it and start debugging it over the STM32.
The software is organized in several state machines, one for every specific handling, like for example I2C or UART debug communication. Here follows a block schematic of the software and a brief description of every handler (Image 9).
All the state machines are initialized and executed from the main, as shown in the following code snippet of the main.c file:
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
MX_USART2_UART_Init();
MX_TIM1_Init();
I2cHdlrInit();
EncHdlrInit();
AmpHdlrInit();
DebugHdlrInit();
UartDebugHdlrInit(&huart2);
/* Infinite loop */
while (1)
{
I2cHdlrRun();
EncHdlrRun();
UartDebugHdlrRun();
DebugHdlrRun();
AmpHdlrRun();
}
All software modules are state machine based, as previously mentioned, designed to offer a re-entrant and non-blocking implementation.
Execution time of every “Run” function call is the smallest possible, since all the states are small and designed to not incur in any waiting loop mechanism (e.g., non-blocking hardware access).
Encoder and Amplifier I2C driver
The I2C driver has been completely rewritten, therefore the standard generated STM HAL driver has not been used. Reason for that was to obtain a non-blocking implementation, which could exploit polling as much as possible.
The I2C has a complete polling implementation, where every hardware access is handled through a specific switch case state, allowing a non blocking re-entrant implementation; switch case handling allows the driver to be executed concurrently to other software modules.
Peculiarity of this driver is that it can handle multiple I2C peripherals with the same code at the same time (Image 10), so it can be called in parallel by different handler. This is then allowing to handle the encoder and the amplifier requests in parallel.
The same functions of the driver are accessed at the same time by the two application handlers but using two different index. This allows to keep the communication channels separated and isolated.
Encoder Handler
The Encoder Handler, is the software component which takes care of configuring and interacting with the encoder. Once the handler completes the encoder initialization, it keeps measuring the interrupt pin for any physical interrupts: the encoder INT pin is here used to keep trace if an event occurred on the encoder (e.g., rotation, click, etc…)
As soon as an interrupt is generated, the encoder status register is read to detect which event was generated. If the detected event was a rotation, the CVAL register is read to detect the current position. This value will be used to configure the amplifier’s gain into the Amplifier handler, through the API AmpHdlrSetGain.
Amplifier Handler
The Amplifier Handler, is the software component which takes care of configuring and interacting with the TPA2016 chip. The handler performs a basic initialization which basically disable the audio compression, the AGC, reduce to the minimum the AGC release time and then it waits for any set gain request from the encoder handler.
As soon as a change in the gain is requested, the new value is then programmed into the TPA2016, adjusting then the current audio volume.
Let’s now test the project
Preliminary hardware setup
In order to start our tests, first of all, we need to build the circuit as shown on Image xx. An audio input can be provided by any smartphone which has a jack audio interface or by any mp3 reader. Every channel of the amplifier works separately, therefore it’s even possible to supply the signal to only one channel, the right or the left. The output instead can be connected to any classical loudspeaker (not pre-amplified). The loudspeaker can be either of 4 ohm or 8 ohm, since the amplifier will adjust the output load without the need of changing any setting. The same consideration here is valid also for the output, meaning that also a single loudspeaker can be connected for our test, ensuring it is connected to the same channel for which an audio input signal was provided. Provide then also the supply voltage, which can be of 5 volts for the amplifier IC, to ensure good performances.
Software setup
Now we can just connect the STM32 evaluation board to a PC through the USB port and use a serial port terminal to test our code. In this example, I will use Putty, which can be downloaded from here. Once opened, the tool will show the following configuration window (Image 11).
Once the correct parameters are selected, like the right COM port or the right speed, we can click Open to start the serial port session.
Open the STM32CubeIde and open the project previously downloaded from GitHub.
Start then the project from Run->Debug.
Let’s start then our audio player. If the circuit has been correctly built, it should be now possible to hear the music already directly in the loudspeaker. Let’s then rotate the encoder to adapt the volume. As soon as the encoder will be rotated, the music volume will be adjusted accordingly and this will be also showed in the terminal, as per the following picture:
Conclusion
In conclusion, the TPA2016 class D audio amplifier is a great option for those looking for a simple and cost-effective solution for their audio projects. The amplifier was easy to mount and configure, which made the setup process a breeze.
However, it should be noted that the TPA2016 does not have a built-in volume control, which may be a disadvantage for some applications. Nonetheless, the amplifier’s standard configuration can be adjusted to achieve the desired volume level.
Overall, the TPA2016 proved to be a reliable and efficient audio amplifier that can meet the needs of various projects while keeping costs low. Its ease of use and competitive price make it a viable option for hobbyists and professionals alike.
Thanks… I’ve been using the Duppa I2C rotary encoder boards in several of my Arduino projects, and I’m about to port them to an STM32 board. I wondered how I might handle the I2C library – and you have given me hope that it’s possible.