Monday, July 2, 2012

Using the Arduino PT2322 library for audio control

There are a limited set of audio volume & tone processors available on the market especially in an easy to use DIP format. The PT2322 is in common use in audio equipment around the world. However it hasn't been picked up by the Arduino community; most likely because of the lack of an available library. In the process of developing an internet radio around this chip and an MP3 decoder I created a library for the PT2322. The PT2322 is available from oddWires here.

The PT2322 is an interesting chip and is very flexible for audio control. It's designed as a 5.1 audio processor enabling you to control your audio setup from your Arduino. oddWires has the only library written for the Arduino for this device. Download it here: Library and Example. In addition to its designed use you could use the 6 channels to control an active loudspeaker system using 3 channels for hi/mid/lo over two channels. It's very cost-effective if you use any of the chip amps oddWires has on the site. There is also a PCB board for LM1875/TDA2030 chip amps available here.

PT2322 is a 6-Channel Audio Processor IC utilizing CMOS Technology specially designed for audio applications. 6-channel individual input, 6-channel master volume control, 6-channel individual volume trim control, 3-band tone control (treble, middle, and bass), mute function, 3D effect function, tone defeat function are all built into a single chip having the highest performance and reliability with few external components. The features are shown below.

Features
  • Very Low Power Consumption (DC=9V)
  • I2C Bus Control
  • 6-Channel Individual Input
  • 6-Channel Master Volume Control: 0 to -79 dB (1 dB/step)
  • 6-Channel Individual Output TRIM Volume Control: 0 to -15 dB (1dB/step)
  • 3-Band Tone Control (Treble, Middle, Bass): + 14dB , 2dB/step
  • Mute Function
  • 3D Effect Function
  • Tone Defeat Function
  • Low Noise
  • High Channel Separation
  • Low Harmonic Distortion
  • Few External Components
  • Easy to Use
  • 28-pin DIP Package 
Functional Diagramhttp://www.oddwires.com/product_images/t/087/Screen_Shot_2012_06_23_at_4.18.16_PM__78824_zoom.png

Constructing the PT2322 circuit

At first, the wiring diagram may seem to require a lot. In fact, much of the circuitry is repeated. It consists of impedence setting resistors & DC blockers on all the inputs and outputs. Then there are a set of RC circuits for shaping the treble, mid and bass response. Hooking up the Arduino is simple as the PT2322 is an I2C device using SDA & SCL. You'll hook these up to pin 4 and 5 respectively on your controller board unless you have an Arduino Mega where the pins are 20 and 21.  The chip works from 5 to 15V DC. I used the 9V supply used to power my Arduino. A regulated supply would be preferable.

http://www.oddwires.com/product_images/l/535/Screen_Shot_2012_06_24_at_3.19.25_PM__08157_zoom.pngUsing the oddWires PT2322 Library

Once you have it wired up, you're ready to start. You could program it directly using the standard Arduino Wire library but an easier solution is to use the library referred to above because it handles all the control necessary to drive the device per the datasheet to perform all of its capabilities:

      int init(void);
    void muteOn(void);
    void muteOff(void);
    void _3DOn(void);
    void _3DOff(void);
    void toneOn(void);
    void toneOff(void);
    void leftVolume(unsigned char flv);
    void rightVolume(unsigned char frv);
    void centerVolume(unsigned char cv);
    void rearLeftVolume(unsigned char rlv);
    void rearRightVolume(unsigned char rrv);
    void subwooferVolume(unsigned char sv);
    void masterVolume(unsigned char mv);
    void bass(unsigned char tb);
    void middle(unsigned char tm);
    void treble(unsigned char tt);


Just call init() first and then use the functions above as you wish. An example is supplied with the library. The example has additional functionality and shows how to use four buttons and a 16 x 2 LCD to display the current settings, change settings, mute etc.

The PT2322 is available from oddWires here.


18 comments:

  1. I am experimenting with the functionality and am using this IC to fade a stereo audio signal left and right. In the library, rearRightVolume() and rightVolume() seem to change the volume of SR (surround right) above. I cant seem to find how to change volume of FR (front right).
    leftVolume() controls (FL) so I assumed:
    rightVolume() controls (FR)...
    I have 2 PT2322 and they do the same thing.

    ReplyDelete
    Replies
    1. Found the problem in the PT2322.cpp library file:
      both the rightVolume and rearRightVolume functions are tied to RR_VOLUME_CONTROL I2C address
      I changed the rightVolume from RR_VOLUME_CONTROL to FR_VOLUME_CONTROL. Works great now!

      void PT2322::rightVolume(unsigned char frv)
      {
      writeI2CChar(RR_VOLUME_CONTROL | -frv);
      }

      //range : 0 to -15dB
      void PT2322::centerVolume(unsigned char cv)
      {
      writeI2CChar(CENTER_VOLUME_CONTROL | -cv);
      }

      //range : 0 to -15dB
      void PT2322::rearLeftVolume(unsigned char rlv)
      {
      writeI2CChar(RL_VOLUME_CONTROL | -rlv);
      }

      //range : 0 to -15dB
      void PT2322::rearRightVolume(unsigned char rrv)
      {
      writeI2CChar(RR_VOLUME_CONTROL | -rrv);
      }

      Delete
    2. Thanks to Ziz for finding the bug! The library has been updated.

      Delete
  2. Can any help me please, I bought PT2322, and I am having problem with it program, I am using an arduino Uno, everything is hook up per data sheet, and I just can't get it to work. Thanks Jean

    ReplyDelete
  3. Hi, it's not possible to determine anything from your comments. Needs data to help.

    ReplyDelete
  4. Well, when I verified the code this is the error message I was getting
    Arduino: 1.5.5 (Windows 8), Board: "Arduino Uno"

    C:\Users\jean\AppData\Local\Temp\build8207638989266376754.tmp/core.a(main.cpp.o): In function `main':
    C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/main.cpp:11: undefined reference to `setup'
    C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/main.cpp:14: undefined reference to `loop'

    This report would have more information with
    "Show verbose output during compilation"
    enabled in File > Preferences.

    ReplyDelete
  5. And this is the one I've used


    #include
    #include
    #include

    int toneAttenuation[30] = {
    -14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
    };

    unsigned const char toneValues[30] = {
    0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15
    };

    // tone table lookup
    unsigned char PT2322::toneLookup (int tone){
    int* ptr = toneAttenuation;
    for (int i=0;i> 1); // transmit to device 0x88, PT2322
    Wire.write(c);
    int rtnCode = Wire.endTransmission(); // stop transmitting
    return rtnCode;
    }

    // initialize PT2322
    int PT2322::init(void)
    {
    delay(300); // in case this is first time - I2C bus not ready for this long on power on with 10uF cref
    function = _3D_OFF; //mute OFF, 3D OFF, tone control ON
    unsigned char masterVolumeValue = 0; //master volume = -15db - temporary at 0
    unsigned char bassValue = 0x07; //Bass = -0dB
    unsigned char middleValue = 0x07; //Middle = -0dB
    unsigned char trebleValue = 0x07; //Treble = -0dB

    // initialize device
    writeI2CChar(SYSTEM_RESET);
    writeI2CChar(INPUT_SW_ACTIVE); // required to activate

    // set the trim volumes to zero
    writeI2CChar(FL_VOLUME_CONTROL); //0db
    writeI2CChar(FR_VOLUME_CONTROL); //0db
    writeI2CChar(CENTER_VOLUME_CONTROL); //0db
    writeI2CChar(RL_VOLUME_CONTROL); //0db
    writeI2CChar(RR_VOLUME_CONTROL); //0db
    writeI2CChar(SUB_VOLUME_CONTROL); //0db

    // set the master voume
    Wire.beginTransmission(0x88 >> 1); // transmit to device 0x88, PT2322
    Wire.write(MASTER_VOLUME_1STEP | (HEX2BCD(masterVolumeValue) & 0x0f));
    Wire.write(MASTER_VOLUME_10STEP | ((HEX2BCD(masterVolumeValue) & 0xf0)>>4));
    Wire.endTransmission(); // stop transmitting

    // set default function
    writeI2CChar(FUNCTION_SELECT | function);

    // and finish with the tone controls
    writeI2CChar(BASS_TONE_CONTROL | bassValue);
    writeI2CChar(MIDDLE_TONE_CONTROL | middleValue);
    return writeI2CChar(TREBLE_TONE_CONTROL | trebleValue);
    }
    void PT2322::rightVolume(unsigned char frv)
    {
    writeI2CChar(RR_VOLUME_CONTROL | -frv);
    }

    //range : 0 to -15dB
    void PT2322::centerVolume(unsigned char cv)
    {
    writeI2CChar(CENTER_VOLUME_CONTROL | -cv);
    }

    //range : 0 to -15dB
    void PT2322::rearLeftVolume(unsigned char rlv)
    {
    writeI2CChar(RL_VOLUME_CONTROL | -rlv);
    }

    //range : 0 to -15dB
    void PT2322::rearRightVolume(unsigned char rrv)
    {
    writeI2CChar(RR_VOLUME_CONTROL | -rrv);
    }

    ReplyDelete
  6. You are not using the library correctly. You need download from https://store-a8995.mybigcommerce.com/content/PT2322.zip. Unzip, then copy the PT2322 folder containing PT2322.cpp & PT2322.h into documents/arduino/libraries (as you would for any arduino library). Restart the Arduino IDE. Then use the example: pt2322ExampleWithControls.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
  7. please give pin connection of micro controller

    ReplyDelete
  8. I cannot import the library. plzz help.

    ReplyDelete
  9. Give the microcontroler connection....

    ReplyDelete
    Replies
    1. Standard Wire.h sda and scl pinout for arduino uno: A4-SDA, A5-SCL

      Delete
  10. Which microcontroller I should use for this?

    ReplyDelete
  11. Please give the circuit diagram for the included example in the library.

    ReplyDelete
    Replies
    1. Standard Wire.h sda and scl pinout for arduino uno: A4-SDA, A5-SCL

      Delete
  12. I wired this up on a breadboard per the circuit diagram, tried using 5v from arduino, also tried using 9v. Tried with and without 4.7k pullups on SDA/SCL lines. Nothing I'm doing will work. I always receive a return value of 2 (NACK on transmit of address) when calling the init() method in your library. Any thoughts?

    ReplyDelete