Using with Arduino

Its super easy to use this stereo amplifier board with an Arduino thanks to the great Adafruit library. Once you've installed the library you can connect the TPA2016 via I2C your Arduino, it will work with any kind or flavor. If you're using a different kind of microcontroller, the library is a good reference to help you port the code over.

Download the library


First up, we'll download the Arduino library from the Arduino library manager.

Open up the Arduino library manager:

Search for the Adafruit TPA2016 library and install it

We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use

Run Test Sketch

After you've restarted, you should be able to load up the File->Examples->Adafruit_TPA2016->audioamp sketch
Wire up the sensor as shown, to I2C. You'll want to wire up an audio source using a pigtail cable or a audio jack as shown. Connect the GND pin to ground, VDD and I2C Vcc pin to 5V and connect SDA to your Arduino's SDA pin, SCL to SCL pin
  • On UNO/Duemilanove/etc, SDA == Analog 4, SCL == Analog 5
  • On Leonardo/Micro, SDA == Digital 2, SCL == Digital 3
  • On Mega/ADK/Due, SDA == Digital 20, SCL == Digital 21
Upload the sketch and open up the serial console at 9600 baud. You should see that the Arduino go through our test procedure, starting with increasing the gain from -28 up to 30 dB.
The test code will also toggle each of the speakers on and off (left channel, right channel) and set up the AGC parameters.
Pretty much if you can hear the gain increase in the beginning and then each speaker turn on & off then the test has completed successfully!

Now you can configure the amplifier settings on your own using the library procedures.

Adafruit TPA2016 Library reference

To use the library, you'll need to have something like these lines in the beginning of your code, they'll include the I2C Wire library and the Adafruit library as well. Then it will create the audio amplifier object. There's no way to change the I2C address on this device.
#include <Wire.h>
#include "Adafruit_TPA2016.h"

Adafruit_TPA2016 audioamp = Adafruit_TPA2016();
Then somewhere in your setup() function, start the amplifier interface with
audioamp.begin();
The amplifier starts 'on' by default and with 6dB gain so running begin() won't change that.

Channel Control

Now that you're talking to the amp, you can do stuff like turn on and off the right and left channels with
audioamp.enableChannel(rightchannel, leftchannel);
for example, to turn off the left channel and keep the right one on, use
audioamp.enableChannel(true, false);
Both channels are on by default

Gain Control

You can control the max gain that the amplifier will use, this is even with the AGC on. So if you select 20dB for example, the AGC won't automatically amplify any higher than 20dB.

You can set the gain with:
audioamp.setGain(gain);
Where gain is from -28 (dB) to 30 (dB)

Limiter Settings


The limiter is what helps avoid overdriving speakers if you have a fixed Wattage you want to stay under. By default it is On, you can turn it off with
audioamp.setLimitLevelOn();
and back off with
audioamp.setLimitLevelOff();
You can set the maximum voltage gain with
audioamp.setLimitLevel(limit);
where limit ranges from 0 (-6.5dBv) to 31 (9dBV) see page 22 of the Datasheet for more details.

AGC configuration settings

You can set up the AGC for how it will react to audio level changes. If you're an audio geek you can use the tables on page 23-24 of the datasheet to set up the AGC exactly how you like. If you aren't sure what settings to use, check page 19 of the datasheet which has some basic guidance.

You can start by setting the AGC compression ratio. This tells the amplifier how much to automatically tweak the gain to make loud and soft sounds about the same volume. For example, spoken word should have high compression since you want to have it all about the same volume. Adjustments are made by calling
audioamp.setAGCCompression(compression);
Where the compression setting is TPA2016_AGC_OFF (1:1 compression), TPA2016_AGC_2 (1:2 compression), TPA2016_AGC_4 (1:4 compression), or TPA2016_AGC_8 (1:8 compression).

For example, to set the attack, use:
audioamp.setAttackControl(attackvalue);
where attackvalue ranges from 0-31
To set the hold value, use:
audioamp.setHoldControl(holdvalue);
Where holdvalue is between 0-31
and for the AGC release, use:
audioamp.setReleaseControl(releasevalue);
Where releasevalue is also between 0-31

Pages 23-24 of the datasheet will tell you how to convert those values to ms/dB numbers.

This guide was first published on Feb 07, 2014. It was last updated on Feb 07, 2014.

This page (Arduino) was last updated on Oct 22, 2021.

Text editor powered by tinymce.