Go Down

### Topic: Wrong analogRead values (Read 8630 times)previous topic - next topic

#### nickgammon

#15
##### Feb 14, 2013, 09:26 amLast Edit: Feb 14, 2013, 09:35 am by Nick Gammon Reason: 1

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`

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`

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.
Please post technical questions on the forum, not by personal message. Thanks!

#### gmbroh

#16
##### Feb 14, 2013, 09:49 am

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

#17
##### Feb 14, 2013, 10:00 am
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 (who) 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.
I speak for myself, not Arduino.

#### nickgammon

#18
##### Feb 14, 2013, 10:07 am

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?
Please post technical questions on the forum, not by personal message. Thanks!

#### gmbroh

#19
##### Feb 14, 2013, 10:40 am

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

#20
##### Feb 14, 2013, 11:08 am
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 (who) 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.
I speak for myself, not Arduino.

#### Learning

#21
##### Feb 14, 2013, 06:15 pm
Unplug it! It'll go to zero. (or pretty close)

#### AWOL

#22
##### Feb 14, 2013, 07:03 pm

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

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

(Hint: have you tried it?)
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

#### Grumpy_Mike

#23
##### Feb 14, 2013, 07:39 pm

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

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

#24
##### Feb 15, 2013, 09:10 pm

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

#25
##### Feb 15, 2013, 09:15 pm
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

Cool glasses justified I think.

Go Up