Adafruit Amplifier MAX9744 Potentiometer Volume Control

Hi
I have purchased this amplifier from adafruit. On the website it provides code for setting the volume via the I2C interface. I have successfully run this code however I would like to be able to read the value of a potentiometer and write it to the amplifier via I2C. I have started with the Knob example (designed for a servo) and have modified it however it has no effect on the volume of the amplifier. I presume it is something to do with the data types I have used e.g int8_t. But I am new to programming and I am not sure what I am doing.

Here is the link to the adafruit website:

Here is my modified knob code:

#include <Wire.h>
int potpin = 1; 
#define MAX9744_I2CADDR 0x4B
int8_t vol = 31;
int val;
void setup() {
  Wire.begin();

}  
void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 63);     // scale it for the amplifier(value between 0 and 63)             
  delay(15);           
  Wire.beginTransmission(MAX9744_I2CADDR);
  val = vol;
  Wire.write(vol);
}

Here is the Adafruit code:

#include <Wire.h>
 
// 0x4B is the default i2c address
#define MAX9744_I2CADDR 0x4B
 
// We'll track the volume level in this variable.
int8_t thevol = 31;
 
void setup() {
  Serial.begin(9600);
  Serial.println("MAX9744 demo");
  Wire.begin();
  
  if (! setvolume(thevol)) {
    Serial.println("Failed to set volume, MAX9744 not found!");
    while (1);
  }
}
 
 
// Setting the volume is very simple! Just write the 6-bit
// volume to the i2c bus. That's it!
boolean setvolume(int8_t v) {
  // cant be higher than 63 or lower than 0
  if (v > 63) v = 63;
  if (v < 0) v = 0;
  
  Serial.print("Setting volume to ");
  Serial.println(v);
  Wire.beginTransmission(MAX9744_I2CADDR);
  Wire.write(v);
  if (Wire.endTransmission() == 0) 
    return true;
  else
    return false;
}
 
// Read in + and - characters to set the volume.
void loop() {
  if (! Serial.available()) return;
  
  // read a character from serial console
  char c = Serial.read();
  
  // increase
  if (c == '+') {
    thevol++;
  }
  // decrease
  else if (c == '-') {
    thevol--;
  }
  // ignore anything else
  else 
    return;
    
  if (thevol > 63) thevol = 63;
  if (thevol < 0) thevol = 0;
 
  setvolume(thevol);
}

If you have any ideas it would be greatly appreciated!
Thanks
James :slight_smile:

Your code that doesn't work has:

  Wire.beginTransmission(MAX9744_I2CADDR);
  val = vol;
  Wire.write(vol);

Take a look at the Adafruit code that actually works. It has:

  Wire.beginTransmission(MAX9744_I2CADDR);
  Wire.write(v);
  if (Wire.endTransmission() == 0)

It may not be obvious, but that endTransmission method call is VERY important.

Hi
Thanks fro the reply!
I was wondering how I would implement this. Could I just use Wire.endTransmission() on it's own or would I need a boolean variable to get it to work properly?
Thanks
James :slight_smile:

Could I just use Wire.endTransmission() on it's own

Of course.

Hi
I have changed my code by putting Wire.endTransmission() at the end of the loop but the potentiometer is still not controlling the volume and seems to have no effect on it at all!
If you have any ideas of where I’m going wrong I would love to hear
Thanks
James

Hi
I managed to fix this.
Here is the code if anyone is interested

#include <Wire.h>
int potpin = 1; 
// 0x4B is the default i2c address
#define MAX9744_I2CADDR 0x4B
 
// We'll track the volume level in this variable.
int8_t thevol;
int val;
 
void setup() {
  Serial.begin(9600);
  Serial.println("MAX9744 demo");
  Wire.begin();
  
  if (! setvolume(thevol)) {
    Serial.println("Failed to set volume, MAX9744 not found!");
    while (1);
  }
}
 
 
// Setting the volume is very simple! Just write the 6-bit
// volume to the i2c bus. That's it!
boolean setvolume(int8_t v) {
  // cant be higher than 63 or lower than 0
  if (v > 63) v = 63;
  if (v < 0) v = 0;
  
  
  Wire.beginTransmission(MAX9744_I2CADDR);
  Wire.write(v);
  if (Wire.endTransmission() == 0) 
    return true;
  else
    return false;
}
 
// Read in + and - characters to set the volume.
void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 63);     // scale it to use it with the servo (value between 0 and 180)              // sets the servo position according to the scaled value
  delay(15);
  Serial.println(val);           
  setvolume(val);
}

Thanks
James 8)
8) 8) 8) 8) 8)