Pages: 1 [2]   Go Down
Author Topic: Wrong analogRead values  (Read 1227 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

analogRead works.

However you have built delays into your code.

Code:
delay (2);

That's 2 mS. Printing the previous result:

Code:
1/960 * 6 = 6.25 mS

Taking the reading:

Code:
0.104 mS

So you are basically sampling every 8.354 mS.

The period of a 65 Hz wave would be:

Code:
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.
« Last Edit: February 14, 2013, 03:35:20 am by Nick Gammon » Logged

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

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


* Sample Rate.jpg (310.93 KB, 2550x3480 - viewed 11 times.)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

"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.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this: (Compiled but untested.)
Code:
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); 
  }
}
Logged

"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.

Offline Offline
Full Member
***
Karma: 2
Posts: 138
W9LZ
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unplug it! It'll go to zero. (or pretty close) smiley-cool
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unplug it! It'll go to zero. (or pretty close) smiley-cool
Bonus marks to Learning if you can explain why those sunglasses are unearned.  smiley-cool

(Hint: have you tried it?)
Logged

"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.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31332
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Unplug it! It'll go to zero. (or pretty close) smiley-cool
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.
Logged

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

Try this: (Compiled but untested.)
Code:
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.... smiley-sad
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31332
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-cool

Cool glasses justified I think.
Logged

Pages: 1 [2]   Go Up
Jump to: