Go Down

Topic: Wrong analogRead values (Read 1 time) previous topic - next topic

Nick Gammon

#15
Feb 14, 2013, 09:26 am Last Edit: Feb 14, 2013, 09:35 am by Nick Gammon Reason: 1
analogRead works.

However you have built delays into your code.

Code: [Select]

delay (2);


That's 2 mS. Printing the previous result:

Code: [Select]

1/960 * 6 = 6.25 mS


Taking the reading:

Code: [Select]

0.104 mS


So you are basically sampling every 8.354 mS.

The period of a 65 Hz wave would be:

Code: [Select]

1/65 = 15.3 mS


Your sampling isn't fast enough.

You are supposed to sample at twice the frequency, in this case: 7.65 mS, although because of the full-wave rectification I suspect half that, namely 3.825 mS.
http://www.gammon.com.au/electronics

gmbroh


Quote
The input to the arduino is 0 to 5volts ac  I dont understand why you think its dc.

Because your scope trace clearly shows everything above 0V.
Alternating current implies a current reversal.


At first the code was working really well. All I was doing was changing the delay to get different sample rates. Please see below

AWOL

In your other thread on this subject, which I referred to in reply #1, I suggested buffering readings and then printing them out.
Why didn't you do this?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon


At first the code was working really well. All I was doing was changing the delay to get different sample rates. Please see below


Quote
The problem is that the output on the serial monitor only shows the peak values of the voltage. That is from 4-5v instead of zero.


The chart you posted (reproduced output) shows the graph reaching zero. What are you talking about?
http://www.gammon.com.au/electronics

gmbroh



At first the code was working really well. All I was doing was changing the delay to get different sample rates. Please see below


Quote
The problem is that the output on the serial monitor only shows the peak values of the voltage. That is from 4-5v instead of zero.


The chart you posted (reproduced output) shows the graph reaching zero. What are you talking about?


The chart of the reproduced output is from when the code was working but not I am not getting those values.

AWOL

Try this: (Compiled but untested.)
Code: [Select]
const int numReadings = 100;
const int analogInPin = A0; 
int inputReading [numReadings];

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  for (int i = 0; i < numReadings; i++) {
    inputReading [i]= analogRead(analogInPin); // roughly 9kHz sampling
  }
 
  for (int i = 0; i < numReadings; i++) {   
    float voltage = ((inputReading[i]/1023.0)*5.0);                             
    Serial.println(voltage); 
  }
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

MisterResistor

Unplug it! It'll go to zero. (or pretty close) 8)

AWOL


Unplug it! It'll go to zero. (or pretty close) 8)

Bonus marks to Learning if you can explain why those sunglasses are unearned.  8)

(Hint: have you tried it?)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Grumpy_Mike


Unplug it! It'll go to zero. (or pretty close) 8)

No it won't it will float which is why I suggested you need a load resistor. So why have you not tried this.
Better still put the scope lead on the analogue input and run the code again. Is it different than when the scope is disconnected.

gmbroh


Try this: (Compiled but untested.)
Code: [Select]
const int numReadings = 100;
const int analogInPin = A0; 
int inputReading [numReadings];

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  for (int i = 0; i < numReadings; i++) {
    inputReading [i]= analogRead(analogInPin); // roughly 9kHz sampling
  }
 
  for (int i = 0; i < numReadings; i++) {   
    float voltage = ((inputReading[i]/1023.0)*5.0);                             
    Serial.println(voltage); 
  }
}



I tried connecting a 10k resistor as someone suggested. And it WORKED !. I uploaded your code for fun and it also works. Its a bit better than mine.... :(

Grumpy_Mike

Quote
I tried connecting a 10k resistor as someone suggested. And it WORKED


As some one once said, tell me when I am wrong not when I am right  8)

Cool glasses justified I think.

Go Up