Go Down

Topic: Inaccurate analog reading and interrupt problem (Read 1 time) previous topic - next topic

lojkoro

Hi, I'm very new to this and I'm liking it very much! I have a problem with the analog readings though. I connected a battery (1.5v) to ground and analog input 0. The value it reads is 368 (~1.8V). I tested the battery and the most precise value I get is 1.58 V (from a calibrated tester). Is this normal? such a big discrepancy? The code is as simple as it gets so that can't be it. I have a duemilanove with atmega 328p. This is the code anyhow:
Code: [Select]
int valor;
void setup(){
 Serial.begin(9600);
}
void loop(){
 valor=analogRead(0);
 Serial.println(valor,DEC);
 delay(1000);
}

The other problem I've been having is with two ISR interrupts (in another application). The part of the code that handles the interrupts is this:
Code: [Select]
void fotoPuerta(int puertos) {

 pinMode(2, INPUT);   //Poner los digitales como input
 pinMode(3, INPUT);
 EICRA = B00001111;
 switch(puertos) //Selecciona cuales se van a medir
 {
 case 1:
   EIMSK |= B00000001; //Activa Interrupcion en puerto 2.
   break;
 case 2:
   EIMSK |= B00000010; //Activa Interrupcion en puerto 3.
   break;
 case 3:
   EIMSK |= B00000011; //Activa Interrupcion en ambos.
   break;
 }
 delay(1000);

 //Serial write loop para los valores
 do {
   if (k<=(i-1) && k<=150){
     mandarFoto1(ValTiempo0[k]);
     k++;
     if(k==i){
       k=0;
       i=0;
     }

     if (l<=(j-1) && l<=150){
       mandarFoto2(ValTiempo1[l]);
       l++;
       if(l==j){
         l=0;
         j=0;
       }
     }
   }

 }
 while(parar==0);
 EIMSK =0; //Detiene las interrupciones
 parar=0;

}

//////////// Funciones para las interrupciones ////////
ISR(INT0_vect){  
 ValTiempo0[i++]=micros();
}
ISR(INT1_vect){
 ValTiempo1[j++]=micros();
}
The problem is that it messes up my analog meassures (not in this code) when I use both interrupt functions (with one it doesn't seem to be a problem, so it is very strange). It sends all of the adc to 1023.

I should say I've tried both situations with two different boards. Don't really know what is happening so if someone could please help me it would be great. Thanks.  :)

PaulS

Quote
I connected a battery (1.5v) to ground and analog input 0.


Why?

lojkoro

To get a good and easy test of the accuracy of the analog to digital conversor. I also tried connecting the 3.3v to the analog and the 5 volt to the analog as well. The 5v was the only one that gave me the correct result (1023) and I suppose that a smaller potential difference would give me 1023 also (as everything I try gives me more than what it is supposed to).

PaulS

The analog reading will be the ratio of the input voltage over Vref, multiplied by 1023.

If Vref is 5V (the default, unless you supply a different Vref), the only way to get a 1023 out is to put 5V in.

Grumpy_Mike

Quote
The 5v was the only one that gave me the correct result (1023) and I suppose that a smaller potential difference would give me 1023


No the 5V line is the voltage reference voltage so suppose it is low in terms of absolute voltage say 4.8V measuring this with a 4.8V reference will still give you 1023.

What you need to do is to have and external stable accurate Vref. Then the figures will come out right. When you input a voltage equal to this Vref you will measure 1023.

lojkoro

thank you very much Grumpy Mike and PaulS. I was thinking it might be a different upper floor but wasn't sure. I will stabilize with Vref. And in this line, can I use a 10v Vref? or something might be damaged in the process...
Thanks again.

pluggy

The maximum VREF is 5 volts.  If you're running it on external power of suitable voltage and power, the default VREF should be very close to 5 volts.  The problem comes when you run it on USB power which are frequently quite a way off from the 5 volt its supposed to be.  (Mine runs at around 4.75 volts).  
http://pluggy.is-a-geek.com/index.html

lojkoro


Grumpy_Mike

Actually to be pedantic (again) the maximum Vref is the supply voltage of the chip. Have a look at reference voltage chips these are normally in the 3 to 4 volt range. Don't be tempted to cut down a reference voltage with a potential divider as the Vref input needs a low (ish) impedance source. Have a read of the data sheet for more information.

Go Up