Hi, since my post has been moved it deleted my reply and I can't attach the same file again (the diagram) because the server seems to find a copy somewhere. I'll at least rewrite the answer, without the diagram attachment:
The extra components in my diagram (which I hope I can attach soon!) are 2 Arducams, a couple of temperature sensors, an SD shield and 3 buttons. The second circuit with relays is where the current sources (solar cells) are placed, and the problem I was describing is when reading the voltage drop over the shunt resistor, which is called V_AMP in the diagram. This voltage reading is not correct when using the internal voltage reference, because the internal reference changes in function of the input voltage V_AMP which is read by my pin A15.
My code is long and exceeds the allowed characters, so I'll paste the snippet where I take the measurement I'm talking about. I always find it hard to make snippets, but I'll try:
int shunt_input = A15;
float shunt_analogamount_array[10]; //to make 10 fast readings which will be averaged
float shunt_voltageamount_array[10]; //to calculate 10 shunt voltage drops which will be averaged
float shunt_analogamount_sum[4]; //to store the sums of the analogamounts
float shunt_voltage_sum[4]; //to store the sums of the calculated voltage drops
float shunt_voltage_sqDevSum[4]; //to store the sum of the squares of the differences from the mean
float shunt_voltage_stDev[4]; //to store the standard deviation of the series of fast voltage drop measurements
float cell_current_sqDevSum[4]; //to store the sum of the squares of the differences from the mean
float cell_current_stDev[4]; //to store the standard deviation of the series of Isc measurements
float shunt_analogamount[] = {0, 0, 0, 0};
float shunt_voltage[] = {0, 0, 0, 0};
float cell_current[] = {0, 0, 0, 0};
float Rshunt = 0.120; //Calculated from tests (cf excel). resistance must be calibrated/measured
const byte RelayPin[] = {33, 31, 29, 27}; //Cell1 to 33, Cell2 to 31, Cell3 to 29, Cell4 to 27
float AREF_voltage = 1.03; //actual measured internal voltage(measured with DMM)
CellN=4;
SAMPLES=10;
//in setup
for (int thisPin = 0; thisPin < CellN; thisPin++) { //transistor pins: for the 4 relays
pinMode(RelayPin[thisPin], OUTPUT);
digitalWrite(RelayPin[thisPin], LOW); //Set to low, just in case this is not default: we don't want the cells to be shorted now.
}
pinMode(shunt_input, INPUT);
//in loop
//thisCell is incremented under certain conditions and tells which relay to close
//Measure Isc
digitalWrite(RelayPin[thisCell], HIGH); //Send input to the transistor which shorts the circuit of actual cell
Serial.print("Circuit Shorted for Isc measurement for pin: ");
Serial.println(RelayPin[thisCell]);
delay(20);
if(thisCell==3)delay(5000); //just to calibrate and have the time to read value
for (int i = 0; i < SAMPLES; i++) { //make average of 10 fast readings
if (i == 0) { //set sums to 0 before starting
shunt_voltage_sum[thisCell] = 0;
}
shunt_analogamount_array[i] = analogRead(shunt_input);
shunt_voltageamount_array[i] = shunt_analogamount_array[i] * AREF_voltage / 1023.0; // in volts
Serial.print(shunt_voltageamount_array[i]);
Serial.print(" --- ");
Serial.println(shunt_analogamount_array[i]);
shunt_analogamount_sum[thisCell] += shunt_analogamount_array[i];
shunt_voltage_sum[thisCell] += shunt_voltageamount_array[i];
Serial.print("shunt_voltage_sum[thisCell] =");
Serial.println(shunt_voltage_sum[thisCell]);
Serial.print("AREF=");
Serial.println(analogRead(A0));
};
shunt_analogamount[thisCell] = shunt_analogamount_sum[thisCell] / float(SAMPLES); //calculate average analog amount and store in analog amount array
shunt_voltage[thisCell] = shunt_voltage_sum[thisCell] / float(SAMPLES); //calculate average voltage drop and store in analog amount array
cell_current[thisCell] = shunt_voltage[thisCell] / Rshunt; //Convert with Ohm Law Isc=Vshunt/Rshunt (Rshunt determined experimentally by comparing input (Isc) and output (Vamp) (slope))
// Calculate Standard Deviation for voltage drop over shunt (Vamp) and Isc
// STEP 1, Find the mean. (We Just did)
// STEP 2, Sum the squares of the differences from the mean
for (int i = 0; i < SAMPLES; i++) {
// pow(x, 2) = x^2
shunt_voltage_sqDevSum[thisCell] += pow((shunt_voltage[thisCell] - shunt_voltageamount_array[i]), 2);
cell_current_sqDevSum[thisCell] += pow((cell_current[thisCell] - shunt_voltageamount_array[i] / Rshunt), 2);
}
// STEP 3, Take the square root of the mean of that
shunt_voltage_stDev[thisCell] = sqrt(shunt_voltage_sqDevSum[thisCell] / float(SAMPLES));
cell_current_stDev[thisCell] = sqrt(cell_current_sqDevSum[thisCell] / float(SAMPLES));
Serial.print("Averaged analogread for shunt: ");
Serial.print(shunt_analogamount[thisCell]);
Serial.print("Averaged Vamp=");
Serial.println(shunt_voltage[thisCell]);
Serial.print("Standard deviation for Vamp=");
Serial.println(shunt_voltage_stDev[thisCell]);
Serial.print("Averaged Isc=");
Serial.println(cell_current[thisCell]);
Serial.print("Standard deviation for Isc=");
Serial.println(cell_current_stDev[thisCell]);
delay(20);
digitalWrite(RelayPin[thisCell], LOW);//Close relay[thisCell] again
Serial.println("Circuit Closed again.");