It's easy to use the TPA2016 breakout with Python or CircuitPython and the Adafruit CircuitPython TPA2016 module.  This module allows you to easily write Python code that controls gain, power and more using the breakout.

You can use this breakout with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up a TPA2016 to your board exactly as shown on the previous pages for Arduino. This breakout uses I2C.

Here's an example of wiring a Feather M0 to the sensor with I2C:

  • Board 3V to breakout VDD
  • Board 3V to breakout I2C VDD
  • Board GND to breakout GND
  • Board SCL to breakout SCL
  • Board SDA to breakout SDA
  • Breakout R+/R- to audio input R+/R-
  • Breakout L+/L- to audio input L+/L-
  • Breakout screw terminal R+/R- to Speaker R+/R-
  • Breakout screw terminal L+/L- to Speaker L+/L-

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi 3.3V to breakout VDD
  • Pi 3.3V to breakout I2C VDD
  • Pi GND to breakout GND
  • Pi SCL to breakout SCL
  • Pi SDA to breakout SDA
  • Breakout R+/R- to audio input R+/R-
  • Breakout L+/L- to audio input L+/L-
  • Breakout screw terminal R+/R- to Speaker R+/R-
  • Breakout screw terminal L+/L- to Speaker L+/L-

CircuitPython Installation of TPA2016 Library

You'll need to install the Adafruit CircuitPython TPA2016 library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_tpa2016.mpy
  • adafruit_bus_device
  • adafruit_register

Before continuing make sure your board's lib folder or root filesystem has the adafruit_tpa2016.mpy, adafruit_bus_device, and adafruit_register files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of TPA2016 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-tpa2016

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the breakout we'll initialize it, and set the gain and more from the board's Python REPL.

Since you're using an I2C connection run the following code to import the necessary modules and initialize the I2C connection with the sensor:

import busio
import board
import adafruit_tpa2016

i2c = busio.I2C(board.SCL, board.SDA)
tpa = adafruit_tpa2016.TPA2016(i2c)

Now you're ready to manipulate sound with the breakout using any of these properties:

  • fixed_gain - The fixed gain of the amplifier in dB. If compression is enabled, fixed gain is adjustable from –28 to 30. If compression is disabled, fixed gain is adjustable from 0 to 30.
  • max_gain - The max gain in dB. Must be between 18 and 30.
  • amplifier_shutdown - Amplifier shutdown. Amplifier is disabled if True. Defaults to False. If True, device is in software shutdown, e.g. control, bias and oscillator are inactive.
  • speaker_enable_l - Enables left speaker. Defaults to enabled. Set to False to disable.
  • speaker_enable_r - Enables right speaker. Defaults to enabled. Set to False to disable.

For example to set max_gain and fixed_gain:

tpa.max_gain = 28
tpa.fixed_gain = -16

To shutdown the amplifier:

tpa.amplifier_shutdown = True

To disable the left speaker:

tpa.speaker_enable_l = False

This demonstrates some of the capabilities of this breakout.

To use real-time compression, the driver and breakout include the ability to set compression_ratioattack_time, release_time and more. For more information and examples, check out the documentation.

That's all there is to using the TPA2016 with CircuitPython!

Full Example Code

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import busio
import board
import adafruit_tpa2016

i2c = busio.I2C(board.SCL, board.SDA)
tpa = adafruit_tpa2016.TPA2016(i2c)

tpa.fixed_gain = -16

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

This page (Python & CircuitPython) was last updated on Apr 18, 2021.

Text editor powered by tinymce.