Wrong analogRead values

gmbroh:
I am trying to measure the voltage of an 5volt AC rectified voltage at 65Hz. Using the UNO R3 Below is the current code.

Is that full-wave or half-wave rectification?

Read this before posting a programming question

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

And with or without a filter capacitor?

Lefty

See reply #2:

What is your circuit?

michinyon:
The phrase "5 volt rectified AC voltage" is gibberish.

If you have rectified it, it isn't an AC voltage. The electron flow is no longer alternating in direction.

You will expect to see a DC voltage with a voltage ripple. Which is exactly what you are getting.
Depending on the type of rectifier, you will see 65 Hz or 130 Hz ripple. I can't tell from your
sampling rate, exactly which you are getting.

Your DC voltage has a peak of 5 volts and a minimum of about 4.5 volts. That is exactly what you
are getting.

The input to the arduino is 0 to 5volts ac I dont understand why you think its dc. I have attached a print screen of the input to the arduino

F0008TEK.JPG

retrolefty:

[quote author=Nick Gammon link=topic=148820.msg1118351#msg1118351 date=1360809591]

gmbroh:
I am trying to measure the voltage of an 5volt AC rectified voltage at 65Hz. Using the UNO R3 Below is the current code.

Is that full-wave or half-wave rectification?

And with or without a filter capacitor?

Lefty
[/quote]

It is full wave rectification without a filter capacitor.

It is full wave rectification without a filter capacitor.

Without any load resistor the sample and hold capacitor on the arduino input will act as a smoothing capacitor.
Put a 10K load ( resistor) between the analogue input and ground.

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.

analogRead works.

However you have built delays into your code.

delay (2);

That's 2 mS. Printing the previous result:

1/960 * 6 = 6.25 mS

Taking the reading:

0.104 mS

So you are basically sampling every 8.354 mS.

The period of a 65 Hz wave would be:

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.

AWOL:

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

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?

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

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.

Try this: (Compiled but untested.)

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);  
  }
}

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

Learning:
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?)

Learning:
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.

AWOL:
Try this: (Compiled but untested.)

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.... :frowning:

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.