
Originally Posted by
snowsh
Paul, I think I see, so the volume change is achived by changing the AGND in some way? Im a bit lost in the analogue side, much more of a programmer but keen to know more about how this works.....
@snowsh:
In this particular case, the two ends of the potentiometer are wired to GND & 3.3VDC. So, as the potentiometer shaft is turned, the voltage on the wiper varies from 0VDC (when the wiper is at the GND end) to 3.3VDC (when the wiper is at the 3.3VDC end). If the wiper is connected to an analog input pin (say, PIN 15) & you call the analogRead(15) function, the return from that function call will be a value between 0 (when the wiper is at the GND end, so that the voltage provided to & thus measured at that pin is 0VDC) & 1023 (when the wiper is at the 3.3VDC end, so that the voltage provided to & thus measured at that pin is 3.3VDC). When the wiper is in the middle of the pot, the return from that function call will be approximately 512, corresponding roughly to a voltage of 1.65VDC provided to & thus measured at that pin. See <this> reference for an explanation of how the analogRead() function can be used.
I hope these few examples of input voltage vs. returned value from the analogRead() function help to clarify how the analog inputs work. From there, you write code that then takes that value returned from the analogRead() function & translates it into how you want to manage the volume on the SGTL5000 audio chip. For example, if you want 3.3VDC to represent full volume, & 0VDC to represent full silence, one possible way to implement this is as follows:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
// GUItool: begin automatically generated code
AudioSynthWaveformSine sine1; //xy=203,233
AudioOutputI2S i2s1; //xy=441,233
AudioConnection patchCord1(sine1, 0, i2s1, 0);
AudioConnection patchCord2(sine1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=452,162
// GUItool: end automatically generated code
elapsedMillis msec;
int new_volume_input;
int previous_volume_input;
void setup(void) {
Serial.begin(9600);
AudioMemory(4);
previous_volume_input = 0.0;
sine1.amplitude(1.0);
sine1.frequency(440);
sgtl5000_1.enable();
sgtl5000_1.volume(0.0f); // set the main volume to fully off
Serial.println("setup done");
}
void loop(void) {
if (msec > 100) { // check/change the volume every tenth of a second
// read the input from the potentiometer
new_volume_input = analogRead(15);
// see if the volume input value changed
if (new_volume_input != previous_volume_input)
{
// if so, then save off the new value
previous_volume_input = new_volume_input;
// actually set the new volume
sgtl5000_1.volume((float)(new_volume_input) / 1024.0f); // set the main volume to the new setting
// and print the new settings
Serial.print("Volume input: ");
Serial.println(new_volume_input);
Serial.print("Volume level: ");
Serial.println(new_volume_input);
}
msec = 0;
}
}
Hopefully, this all helps to clarify how to implement volume control on the SGTL5000 audio chip using the analog input from a potentiometer. If not, please feel free to ask more questions !!
Mark J Culross
KD5RXT