Hi all,
I have designed a custom board based on a 32u4 chip, designed to check voltages of a connected 6s lipo. I have used a voltage divider but when some of the pins are left "floating" I still get an analog read of around 700. From a technical POV, is this stray capacitance, too high value a voltage divider or something else?
At the moment I am reading in the ADC and just printing the raw value to serial monitor. Most of the code below isnt used but is there for reference.
// These constants won't change. They're used to give names to the pins used:
int analogPins[6] = {A5, A4, A3, A2, A1, A0};
int RawAnalogReads[6] = {0, 0, 0, 0, 0, 0}; // value read from the pot
float ActualVoltages[6] = {0, 0, 0, 0, 0, 0};
float CellVoltages[6] = {0, 0, 0, 0, 0, 0};
int scaled = 0;
int valueread = 0;
int adc_value = 0;
float adc_voltage = 0.0;
float in_voltage = 0.0;
float ref_voltage = 5.0;
float R1 = 0.0;
float R2 = 0.0;
float readval(int var) {
switch (var) {
case 0:
in_voltage = (analogRead(A5) * (5.0 / 1023)); // * (560 / 330 + 560);
Serial.print("Cell 1 ");
Serial.print(in_voltage);
Serial.print(" Raw = ");
Serial.println(adc_value);
break;
case 1:
R1 = 560000.0;
R2 = 330000.0;
adc_value = analogRead(A4);
adc_voltage = (map(adc_value, 0, 1024, 0, 1024) * ref_voltage) / 1024.0;
in_voltage = adc_voltage / (R2 / (R1 + R2));
Serial.print("Cell 2 ");
Serial.print(in_voltage);
Serial.print(" Raw = ");
Serial.println(adc_value);
break;
case 2:
R1 = 560000.0;
R2 = 330000.0;
adc_value = map(analogRead(A3), 855, 1024, 0, 1024);
adc_voltage = (adc_value * ref_voltage) / 1024.0;
in_voltage = adc_voltage / (R2 / (R1 + R2));
Serial.print("Cell 3 ");
Serial.print(in_voltage);
Serial.print(" Raw = ");
Serial.println(adc_value);
break;
case 3:
R1 = 560000.0;
R2 = 150000.0;
adc_value = map(analogRead(A2), 764, 1024, 0, 1024); // 806 resting
adc_voltage = (adc_value * ref_voltage) / 1024.0;
in_voltage = adc_voltage / (R2 / (R1 + R2));
Serial.print("Cell 4 ");
Serial.print(in_voltage);
Serial.print(" Raw = ");
Serial.println(adc_value);
break;
case 4:
R1 = 560000.0;
R2 = 150000.0;
adc_value = map(analogRead(A1), 763, 1024, 0, 1024); // 805 resting
adc_voltage = (adc_value * ref_voltage) / 1024.0;
in_voltage = adc_voltage / (R2 / (R1 + R2));
Serial.print("Cell 5 ");
Serial.print(in_voltage);
Serial.print(" Raw = ");
Serial.println(adc_value);
break;
case 5:
R1 = 560000.0;
R2 = 100000.0;
adc_value = map(analogRead(A0), 695, 1024, 0, 1024); // Resting 731
adc_voltage = (adc_value * ref_voltage) / 1024.0;
in_voltage = adc_voltage / (R2 / (R1 + R2));
Serial.print("Cell 6 ");
Serial.print(in_voltage);
Serial.print(" Raw = ");
Serial.println(adc_value);
break;
default:
// statements
Serial.println("Nothing happened");
break;
}
int cellVoltage = 0;
return in_voltage;
R1 = 0.0;
R2 = 0.0;
}
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(115200);
}
void loop() {
// read the analog in value:
for (int i = 0; i <= 5; i++) {
valueread = analogRead(analogPins[i]);
Serial.print(valueread);
Serial.print(" ");
// ActualVoltages[i] = readval(i);
}
// for (int i = 0; i <= 5; i++) {
// CellVoltages[i] = ActualVoltages[i] - ActualVoltages[i-1];
// Serial.print(i);
// Serial.print(" ");
// Serial.println(CellVoltages[i]);
// }
// Serial.print(analogRead(A10));
// Serial.println();
Serial.println();
delay(200);
}
And out in the serial monitor I get:
16:52:56.564 -> 0 0 910 806 805 730
16:52:58.188 -> 0 0 910 806 806 730
16:52:59.761 -> 0 0 910 806 805 730
16:53:01.391 -> 0 0 910 806 805 730
16:53:02.987 -> 0 0 910 806 805 730
16:53:04.624 -> 0 0 910 806 805 730
16:53:06.218 -> 0 0 910 806 805 730
16:53:07.845 -> 0 0 910 806 805 730
16:53:09.431 -> 0 0 910 806 805 730
16:53:11.066 -> 0 0 910 806 805 730
16:53:12.658 -> 0 0 910 806 805 731
And this is my schematic:
And my Board layout: