Go Down

Topic: Parasite readings from analogRead (Read 841 times) previous topic - next topic

Faye_V

Hi everyone, I'm measuring a range of voltage values (0-5V) from a voltage divider with a capacitive load for a project, I take 100 samples in an interval of about 10 to 20ms, but I keep getting wrong values, my multimeter reads 3.8 and the value that the analogRead returns is 5V or 0 (1023 or 0) for most of the samples.

Can you guys please help me with this?

johnwasser

Not sure what you mean by "with a capacitive load".  How is everything wired?  Is the input voltage AC or DC?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

MarkT

Sounds like a rectangular waveform, so both are right - multimeters average over second or so with a dual-slope integrating ADC, the ADC on the Arduino is a successive approximation ADC clocked at 125kHz and sampling its input in a few microseconds (if low impedance).  About 5 orders of magnitude difference in how quickly they respond to changing voltage.
[ I won't respond to messages, use the forum please ]

Faye_V

#4
Oct 22, 2012, 11:09 pm Last Edit: Oct 22, 2012, 11:20 pm by Faye_V Reason: 1
Quote
Not sure what you mean by "with a capacitive load".  How is everything wired?  Is the input voltage AC or DC?


The capacitive load uses a capacitor connected to the voltage source, making it fluctuate from 0 to 5V "automatically", I'm using a voltage divider to get these 0 to 5V, because my original source (a PV) delivers 21V DC.

Quote
About 5 orders of magnitude difference in how quickly they respond to changing voltage.
Where can I get more information about this?

Here's my code, thank you for checking it out!

Code: [Select]
#include <Wire.h>
#include <avr/io.h>
#include <SD.h>
#include <avr/power.h>
#include <stdio.h>
#include <stdlib.h>
#include "RTClib.h"


RTC_DS1307 RTC;

#define porcentaje 0.7

#define FASTADC 1

// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

int rly1Pin = 2;
int rly2Pin = 3;
int rly3Pin = 4;
int voltPin = A2;
int corrPin = A3;
int valor = 0;
const float incremento = (5.0/1024.0);
float voltVec[100]={0};
float corrVec[100]={0};
//Para la tarjeta SD de memoria
int CS_pin = 10;
int pow_pin = 8;



void setup () {
   Serial.begin(9600);
   Wire.begin();
   RTC.begin();

 if (! RTC.isrunning()) {
   Serial.println("RTC is NOT running!");}
   
//RTC.adjust(DateTime(__DATE__, __TIME__));
 pinMode(CS_pin, OUTPUT);
 pinMode(pow_pin, OUTPUT);
 pinMode(rly1Pin, OUTPUT);
 pinMode(rly2Pin, OUTPUT);
 pinMode(rly3Pin, OUTPUT);
 Desactivar_Relay();
 SwitchOff_Carga();
 digitalWrite(rly3Pin, LOW);
 digitalWrite(pow_pin, HIGH);
 
 if (!SD.begin(CS_pin))
 {
   Serial.println("Card Failure");
   return;
 }
#if FASTADC
 // set prescale to 16
 sbi(ADCSRA,ADPS2) ;
 cbi(ADCSRA,ADPS1) ;
 cbi(ADCSRA,ADPS0) ;
#endif

}


void loop(){  
 
 DateTime now = RTC.now();
 int hora = now.hour();
 int minuto = now.minute();


 if (hora<7)
{
Dormir();
}

 if (hora>22)
{
Dormir();
}
 
   
 else
{
  if (minuto==0)
    {
    Despierta();
    Activar_Relay();
    int voc = 0;
   
    voc = analogRead(voltPin);

    int maximo = voc*porcentaje;
    int delta1 = maximo/5;
    int delta2 = (voc-maximo)/95;
    Serial.print("VOC = ");
    Serial.println(voc,DEC);
    SwitchOn_Carga();
    Medicion_Datos_IV(delta1,1,6);
    Medicion_Datos_IV(delta2,6,101);
    Imprimir_Serial();
    Desactivar_Relay();
    SwitchOff_Carga();
    Switch_Descarga();
    Nada();
}
 
 
 else
 {
    Desactivar_Relay();
  }
}
 
}    



void Activar_Relay (){
    digitalWrite(rly1Pin, HIGH);
}

void Desactivar_Relay(){
   digitalWrite(rly1Pin, LOW);
}

void Medicion_Datos_IV (int delta, int lim_inf, int lim_sup){
 
 int compara = 0;
 int i = 0;
 
 while(i<lim_sup)
 {
 //compara = compara+delta;
 int volt = analogRead(voltPin);    
 int corr = analogRead(corrPin);
 //if (volt>=compara)
 //{
 voltVec[i] = (volt*incremento);
 corrVec[i] = (corr*incremento);
 i=i+1;
 //}
 }
 
 
}

void Imprimir_Serial(){
 for(int i=1; i<=101;i++)
 {
 Serial.println(voltVec[i], DEC);
 }
 Serial.println("/");
 for(int i=1; i<=101;i++)
 {
 Serial.println(corrVec[i], DEC);
 }
}

void Nada (){
 Serial.println("Fin de toma de datos");
}

void Despierta(){
//power_spi_enable();
power_usart0_enable();
//power_usart1_enable();
power_timer0_enable();
power_timer1_enable();
power_timer2_enable();
//power_twi_enable();
}
void Dormir(){

Serial.println("No molestar, estoy durmiendo");
//power_spi_disable();
power_usart0_disable();
//power_usart1_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
//power_twi_disable();

}  

void SwitchOn_Carga(){
 digitalWrite(rly2Pin, HIGH);
 Serial.println("Conectando carga");
}

void SwitchOff_Carga(){
 digitalWrite(rly2Pin, LOW);
 Serial.println("Desconectando carga");
}

void Switch_Descarga(){    
 int medida = analogRead(voltPin);
 while (medida >= 1){
 digitalWrite(rly3Pin, HIGH);
 medida = analogRead(voltPin);
 delay(5);
 }
 digitalWrite(rly3Pin, LOW);
}  

Go Up