A/D Issue

I have tried to read 6 battery voltages with the Arduino. I have built a voltage divider to adjust the voltages to the 5V range. I have swapped in another Arduino to see if mine was broken. I still can’t get it to read the 6 voltages.

I currently have 5 V on the Aref pin and it is grounded to the last negative battery terminal for a reference.

It still does not work.

I would appreciate any help.

/*

This will read the battery voltages, compare them and then turn on
or off the PWM to drive the MOSFETs

Created 5/25/2010
Stacey Page
University of Idaho
Electrical and Computer Engineering

*/

//These constants won't change.  They're used to give names
//to the pins used.

const int batt6=19;
const int batt5=18;
const int batt4=17;
const int batt3=16;
const int batt2=15;
const int batt1=14;

int V1=0;
int V2=0;
int V3=0;
int V4=0;
int V5=0;
int V6=0;

float Vbatt1=0.0;
float Vbatt2=0.0;
float Vbatt3=0.0;
float Vbatt4=0.0;
float Vbatt5=0.0;
float Vbatt6=0.0;

float V1temp=0.0;
float V2temp=0.0;
float V3temp=0.0;
float V4temp=0.0;
float V5temp=0.0;
float V6temp=0.0;

float Vbattavg=0;

unsigned long counter=600001;


void setup()
{
Serial.begin(9600);  //Set up serial library for use
analogReference(DEFAULT);
pinMode(batt1, INPUT);
pinMode(batt2, INPUT);
pinMode(batt3, INPUT);
pinMode(batt4, INPUT);
pinMode(batt5, INPUT);
pinMode(batt6, INPUT);


pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);

}

//Start of the Main Program

void loop() {

//Read the battery voltages and store them in a variable


V1=analogRead(batt1);
V2=analogRead(batt2);
V3=analogRead(batt3);
V4=analogRead(batt4);
V5=analogRead(batt5);
V6=analogRead(batt6);

V1temp=V1*0.01258;
V2temp=V2*0.01258;
V3temp=V3*0.01258;
V4temp=V4*0.01258;
V5temp=V5*0.01258;
V6temp=V6*0.01258;

Vbatt1=V1temp;
Vbatt2=V2temp-V1temp;
Vbatt3=V3temp-V2temp;
Vbatt4=V4temp-V3temp;
Vbatt5=V5temp-V4temp;
Vbatt6=V6temp-V5temp;

//Get an average of the battery voltages for comparison and to turn
//on PWM
Vbattavg=(Vbatt1+Vbatt2+Vbatt3+Vbatt4+Vbatt5+Vbatt6)/6.0;

//Print the values out for capture by the telnet program
if (counter>=600000){
  
//Serial.println(V1temp);
//Serial.println(V2temp);
//Serial.println(V3temp);
//Serial.println(V4temp);
//Serial.println(V5temp);
Serial.println(V6temp);
Serial.println("");

counter=0;
}
while (counter<=600000){

if (Vbatt1>=Vbattavg){
  digitalWrite(3,HIGH);
}

if (Vbatt2>=Vbattavg){
  digitalWrite(5,HIGH);
}

if (Vbatt3>=Vbattavg){
  digitalWrite(6,HIGH);
}

if (Vbatt4>=Vbattavg){
  digitalWrite(9,HIGH);
}

if (Vbatt5>=Vbattavg){
  digitalWrite(10,HIGH);
}

if (Vbatt6>=Vbattavg){
  digitalWrite(11,HIGH);
}
delayMicroseconds(50);

digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delayMicroseconds(50);

counter=counter+1;
}
}

Sorry about not posting the code. I was a bit frustrated with it. I don’t think it is a coding issue. I think it is a hardware/connection type issue. I suspect that I am not powering someting or grounding something etc.

I still can't get it to read the 6 voltages.

Maybe you didn't remember to write a program to perform the task? Or did you just forget to post your code. :wink:

You are using the wrong pins. This block:-
const int batt6=19;
const int batt5=18;
const int batt4=17;
const int batt3=16;
const int batt2=15;
const int batt1=14;

should read:-
const int batt6=5;
const int batt5=4;
const int batt4=3;
const int batt3=2;
const int batt2=1;
const int batt1=0;

You only use the pin numbers 14 to 19 when you are using the analogue inputs as digital pins.

Perfect. I got it working now. I had one other error that was making a mess of things too but once I got the correct pins it was easy to fix also.

Thanks