Pages: [1]   Go Down
Author Topic: reading multiple analog inputs  (Read 6226 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,

We are working on a project where we are using an arduino uno in order to read battery voltage and solar panel voltage. As we tried this, we run into a big issue: when we try to read in from one analog pin, it works perfectly, but if we read from two analog pins at the same time, none of the two readings are accurate even though we used the same code as before. Please if anybody can shed some light into this issue??? We pretty much just hit a wall as we have tried eveything possible. We will really appreciate your help!
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8517
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but if we read from two analog pins at the same time,
Technically this isn't possible, can you post your code?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Espoo, Finland
Offline Offline
God Member
*****
Karma: 7
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Shcematics would be a good idea as well. Do they have same ground potential? Are those batteries in parallel or serial?

Cheers,
Kari
Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is the code. Maybe I did not explain myself right. We are reading from two different analog pins on the arduino. They do have the same ground potential.hopefully, this code will give you guys an idea.


#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// variables for input pin and control LED
  int solarV = 7;
  int batteryV= 3;
  float vout = 0.0;
  float vin = 0.0;
  float vin1=0.0;
  float vout1=0.0;
  float R1 = 4.7;   
  float R2 = 1.005; 
  float R11=4.690;
  float R22= .998;
  float solarPanelVoltage = 0;
  float batteryVoltage =0;
 
void setup(){
  Serial.begin(9600); // begin sending over serial port
  lcd.begin(20, 4);
  // declaration of pin modes
  analogReference(DEFAULT);
  pinMode(solarV, INPUT);
  pinMode(batteryV, INPUT);
}

void loop(){
  // read the value on analog input
  //Serial.print("value=");
  //Serial.println(value);

   solarPanelVoltage = analogRead(solarV);
   vout = (solarPanelVoltage * 5.0) / 1024.0;
   vin = vout / (R2/(R1+R2));
 
  batteryVoltage = analogRead(batteryV);
  vout1=(batteryVoltage * 5.0) / 1024.0;
  vin1= vout1 / (R22/(R11+R22));
 
 
 Serial.print("voltage=");
 Serial.println(vin);
 lcd.setCursor(0,0);  //column and row of lcd
 lcd.print("Voltage=");
 lcd.setCursor(8, 0);
 lcd.print(vin);
 lcd.setCursor(13, 0);
 lcd.print("V");
 //delay(1000);
 
 Serial.print("battery=");
 Serial.println(vin1);
 lcd.setCursor(0,1);  //column and row of lcd
 lcd.print("Battery=");
 lcd.setCursor(8, 1);
 lcd.print(vin1);
 lcd.setCursor(13, 1);
 lcd.print("V");
 delay(1000);
   
}
Logged

Espoo, Finland
Offline Offline
God Member
*****
Karma: 7
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I understood that. By the code, it seems ok to me, but the schematic would help to understand what is going on. There's probably something floating...?

Kari
Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8517
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Doesn't the solarV in this

Code:
pinMode(solarV, INPUT);

refer to digital pins and in here

Code:
solarPanelVoltage = analogRead(solarV);

it refers to analog pins. So you are working with different pins.

However I don't think it matters because I think analogRead() will set the pin direction anyway.

______
Rob


Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27081
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try putting some delay in between these  reads
Code:
   solarPanelVoltage = analogRead(solarV);
   vout = (solarPanelVoltage * 5.0) / 1024.0;
   vin = vout / (R2/(R1+R2));
 
  batteryVoltage = analogRead(batteryV);
  vout1=(batteryVoltage * 5.0) / 1024.0;
  vin1= vout1 / (R22/(R11+R22));
You may be running into this, section 23 of the datasheet, we can't say for sure as no schematic has been posted:

"The analog input circuitry for single ended channels is illustrated in Figure 23-8. An analog
source applied to ADCn is subjected to the pin capacitance and input leakage of that pin, regardless
of whether that channel is selected as input for the ADC. When the channel is selected, the
source must drive the S/H capacitor through the series resistance (combined resistance in the
input path).
The ADC is optimized for analog signals with an output impedance of approximately 10 kΩ or
less. If such a source is used, the sampling time will be negligible. If a source with higher impedance
is used, the sampling time will depend on how long time the source needs to charge the
S/H capacitor, with can vary widely. The user is recommended to only use low impedance
sources with slowly varying signals, since this minimizes the required charge transfer to the S/H
capacitor.
Signal components higher than the Nyquist frequency (fADC/2) should not be present for either
kind of channels, to avoid distortion from unpredictable signal convolution. The user is advised
to remove high frequency components with a low-pass filter before applying the signals as
inputs to the ADC."
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 43
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had the same problem reading temperature sensors.. The trick when using multiple analog sensors is to read them twice, with a small delay after each read (10ms is good), then discard the first reading. This is because the ADC multiplexer needs switching time and the voltage needs time to stabilize after switching..  Basically the first analogRead call causes the multiplexer to switch, the delay gives the voltage time to stabilize, then your second read should be much more accurate with less jitter. You should have something like this:

Code:
  solarPanelVoltage = analogRead(solarV);
  delay(10);
  solarPanelVoltage = analogRead(solarV);
  delay(10);
   vout = (solarPanelVoltage * 5.0) / 1024.0;
   vin = vout / (R2/(R1+R2));
 
  batteryVoltage = analogRead(batteryV);
  delay(10);
  batteryVoltage = analogRead(batteryV);
  delay(10);
  vout1=(batteryVoltage * 5.0) / 1024.0;
  vin1= vout1 / (R22/(R11+R22));
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3255
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

One question popped in my mind now, does the analogPin() function in the Arduino library use the low noise sleeping mode to make a conversion?
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Offline Offline
Newbie
*
Karma: 1
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HELLO!!


The solution posted by jondecker76 worked for me

THANKS A LOT!

____

gopi
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a bunch of the dallas 1 wire temperature sensors. I have yet to be able to find any way to find their addresses or read them. I have found several sketches for discovering the addresses of the sensors and for reading them. Not one sketch I have found so far will compile. They are all full of syntax errors. I have copied and pasted them and had others try also and none of them work. It seems everything posted on the internet is an Arduino language with a different syntax than what we use. I am running the latest version of the Arduino language and have made several sketchs of my own and they have worked. I do not know enough about the Arduino compiler and its syntax and error codes to fix the problems with someones elses sketch. Does anyone out there have a sketch  -  that actually works - that will read a single one wire temperature sensors address and will read its temperature value.

Thanks

Jim

Logged

Pages: [1]   Go Up
Jump to: