Voltage reading with Aruino Nano and 24V sensor - 2 analog inputs

Hi,

Before I made his post, I researched the problem I was having with the code I've been working and found what I thought were all the answers to my problem here, reading multiple analog inputs - Sensors - Arduino Forum but I'm still having some issues. When I apply voltage to one input I get the same reading for both inputs when I should be getting 24 for one and nothing from the other. Is there something wrong with my code?

/*
DC Voltmeter Using a Voltage Divider
Based on Code Created By
T.K.Hareendran
*/

int motor1V = A0;
int motor2V = A5;
float vout = 0.0;
float vin = 0.0;
float vout1 = 0.0;
float vin1 = 0.0;
float R1 = 30000.0; //  
float R2 = 7500.0; // 
int value = 0;
int value1 =0;
void setup(){
   analogReference(DEFAULT);
   pinMode(motor1V, INPUT);
   pinMode(motor2V, INPUT);
   Serial.begin(9600);
}
void loop(){
   // read the value at analog input
   value = analogRead(motor1V);
   delay(10);
   value = analogRead(motor1V);
   delay(10);
   vout = (value * 5.0) / 1024.0; // see text
   vin = vout / (R2/(R1+R2)); 
   

   value1 = analogRead(motor2V);
   delay(10);
   value1 = analogRead(motor2V);
   delay(10);
   vout1 = (value1 * 5.0) / 1024.0;
   vin1 = vout1 / (R2/(R1+R2));

Serial.print("Motor1V= ");
Serial.println(vin);
Serial.print("Motor2V= ");
Serial.println(vin1);
delay(500);
}

Regards,
Josh

   pinMode(motor1V, INPUT);
   pinMode(motor2V, INPUT);

This is affecting the digital nature of the pin that you are using as an analog pin. The calls are, therefore, useless.

If you are really putting 24V into the Arduino, it is no wonder why you see the same reading on both pins, which share the ADC that you have fried.

Nothing's "jumping-out" at me, so the 1st question is, have you checked the voltages with a multimeter? Or have you tried simply grounding analog input(s) to see if you they read zero?

And for troubleshooting/debugging it wouldn't hurt to simplify the program to just read the two analog inputs without doing any calculations. It's generally a good idea to start by doing that, and after you're confident it's reading correctly, add the code to calculate voltage.

...You shouldn't have to read twice and delay. You should read twice or delay between reading two different pins (so the shared ADC can "settle").

And, your code could be simplified.* The analog reading and voltage calculations can all be done with one line of code. You've got unneeded temporary variables.

And since the voltage divider is fixed, you can pre-calculate the voltage reduction factor as a constant... There's no reason for the Arduino to re-calculate it every time through the loop. (Actually, it's repeating the same calculation twice per loop.) You can show the resistance values and/or the calculations in your comments.

  • You should check my math but I get:
    Voltage1 = 0.0245 * analogRead(motor1V);

@PaulS Why would it fry the board if it's regulated to read up to 25V? Maybe you can enlighten me since I am a newbie when it comes to electronics. As well as a newbie when it comes to coding, hence the reason I researched my problem before I posted but thank you for your input.

I will do as you suggested DVDdoug and let you know how it goes.

DVDdoug

I removed the delays and added one between the reads between the different pins and commented out the calculations.

I have 24 Volts connected to A5. This is the code as well as the pictures from the serial read

/*
DC Voltmeter Using a Voltage Divider
Based on Code Created By
T.K.Hareendran
*/

int motor1V = A0;
int motor2V = A5;
float vout = 0.0;
float vin = 0.0;
float vout1 = 0.0;
float vin1 = 0.0;
float R1 = 30000.0; //  
float R2 = 7500.0; // 
int value = 0;
int value1 = 0;
void setup(){
   analogReference(DEFAULT);
   pinMode(motor1V, INPUT);
   pinMode(motor2V, INPUT);
   Serial.begin(9600);
}
void loop(){
   // read the value at analog input
   value = analogRead(motor1V);
   //vout = (value * 5.0) / 1024.0; // see text
   //vin = vout / (R2/(R1+R2)); 
   delay(10);

   value1 = analogRead(motor2V);
   //vout1 = (value1 * 5.0) / 1024.0;
   //vin1 = vout1 / (R2/(R1+R2));

//Serial.print(vin);
Serial.println(value);
//Serial.print(vin1);
Serial.println(value1);
delay(500);
}

I take it you are using voltage dividers on the analog inputs - they are only rated at 5v. What are the values and what is the circuit diagram

I see now that I probably fried the board since I didn't use any resistors...even though I was getting the 24V reading when connected to the analog input. Am I right? I believe I'm supposed to use a 30K for Voltage in and 7.5K for the ground....Just learned the max voltage allowed on the board was roughly 7V?? Please correct me if I am wrong. If I fried the board and I'm still getting a reading but the same for both inputs does that mean the connection between the ADC and inputs are fried? Sorry for the questions, trying to teach myself all of this.

I didn't fry the board, I have a voltage divider on my 24V sensor, 7.5k and 30k, hence the reason for the formula. So why should I get the same reading on two different inputs?

jmed

I see some problems...

are you feeding 24v to your nano???
where is your ground connection?
I only see one ADC input on A06
what is that shield inbetween the powersupply and your nano

better pictures would show all the details...

I have a voltage divider with resistors that senses the voltage. Everything is working as it should now, I really appreciate everyone's help. The problem was, when I just connected one channel they both would read the voltage but when I connected one of them to a "0" or a ground, one would read zero and the other would read the voltage I was supplying. If that makes any sense.