AC signal voltage


I have referred to previous posts and couldn't find solution. And I m new to programming platform.

When I used the code:

float voltage = sensorValue * (5.0 / 1023.0); DigitShield.setvalue(voltage);

I am getting accurate value if i give the DC input on display. But When I use AC, It is showing all the sensor values on sine waveform on display, which is undesirable. I tried writing the code as maxSensor-minSensor values, even then couldn't find solution.

Could you please suggest me how to calculate peak to peak value of AC sine wave(offset applied) ? Also, Even if I release the push button to stop the analog signal, the value should remain on the display. Only if I press the other push button defined by me, the value should reset.

Please suggest...


You're making a number of inappropriate assumptions about our knowledge of your project:

  • What's a digitshield?
  • What's "the push button"
  • What's "the other push button"

Since an AC signal varies with time, you cannot just measure its amplitude
with a single ADC reading at a single time.

You have two approaches: hardware or software.
And you have to decide whether to measure peak voltage or rms voltage,
and whether that should be a true rms voltage or just peak voltage / root-2.

In hardware a peak detection circuit (related to precision rectification) can
be used - an opamp is used to capture peak voltage on a capacitor.

In software you sample the waveform repeatedly over a whole number of
cycles and calculate the value you want (be it maximum, or true rms).

Also to measure AC you have to voltage shift the signal so the whole
waveform is between 0 and 5V (ADC cannot measure outside that range,
and any significant currents (1mA or more?) flowing outside that range
will risk damaging the chip.

The simplest level shift circuit is a capacitor to a 1:1 voltage divider

MarkT: The simplest level shift circuit is a capacitor to a 1:1 voltage divider

Can you sketch that please Mark?

I could but my camera's elsewhere!

The input circuit is like the one in Answer 1 here: Skip the opamp and route the signal direct to the analog pin via a 10k resistor (overload protection).

I could but my camera’s elsewhere!

So like the attached then?

No, that's not what I said.

I said 10k resistor for overload protection, that's the maximum value you can use without compromising the ADC input requirements.

I said 10k resistor for overload protection

You did indeed say that… so the below?

That's the one!

Although it does assume a low impedance source for the incoming AC to drive the ADC properly, since there is no active buffering. If the source impedance was already 10k you'd want to lower the 10k resistor to perhaps 1k (or live with an LSB or two of error)

I'm going to build that and 'scope it out: any excuse to use my new DSO......

(I bought a nice 240VAC:20VAC fused transformer the other day too, for just this experimental purpose. I can use the 20VAC as a surrogate for the mains without having to stick my probes in the wall.

Edit... just struck me though: does that divider scheme assume the input is of a suitable Vpp (but crossing zero) or does it squish it down to 5Vpp all positive if the incoming Vpp is too big?

Thanks for your replies…

I have attached the pic of the kit I m using for my project. I am using Arduino Uno board and Digit shield ( just the seven segment display kit). I defined two push buttons, one to feed the analog input to the pin and other to reset the RAM values.

I am trying to display the true RMS value of an Audio signal(20kHz). So, Initially I want to test the program with sine wave
(0V-5V _ offset applied in signal generator). So, I just want to display the peak value of AC waveform and reset the value in RAM. As a next step, I will make manipulations for RMS value.


You failed to initially mention that you have a 20khz input ac signal which you need to determine the rms voltage

At full accuracy the Arduino ATMega328 can only sample at around 10khz


So you will not be able to sample the signal fast enough to compute a true RMS value.

If you know the signal is a true sinusoid etc, you can calculate RMS from peak voltage, so there would be a way to to this talking thousands of samples and recording the highest and lowest values registered, e.g over for example several seconds.

But if you incoming signal is of an unknown shape e.g. Chirps emitted by a bat, it would not be possible to determine the RMS

Thanks rogerClark... I have referred to the post,6549.0.html

and set my prescalar to 16 to get ADC clock frequency to 200khz. I think its good enough for the audio signal. Can I proceed in this way ?

I'm not sure even setting the prescaler to divide by 16 instead of 128 is going to be fast enough for the system to accurately calculate the RMS of a 20khz audio signal.

In the posting you linked to in Reply #18 on: December 08, 2010, 09:22:23 am »

the real world results were

16 62.5 kHz 32 33.2 kHz 64 17.8 kHz 128 8.9 kHz

ie at a 16 prescaler you can sample at 62.5 khz not 200khz

So you are only sampling 3 times faster than the frequency of the signal you want to determine the RMS.

I suspect that 3 times over sampling is not going to give you enough samples per cycle of the incoming audio signal (20khz) to determine the RMS.

Note. I don't think you've said if the incoming signal is a pure sine wave or not.

As you have said "audio" I presume that its a complex sound rather than a nice clean sine wave ??

Personally I'd buy a Due for this job as its at least 5 times faster CPU clock rate and I suspect the ADC is even faster again

You are right about Arduino DUE and even I selected the same for my application. But it doesn't support seven segment module( or similar which is must for my project. I tried modifying the existing libraries and left with garbage values. So, I switched to UNO+seven segment display which has readily available code.

The Due is a 3.3V device, so the changes are, it didn't work with the shield because of that.

I'd stick with the Due, getting the LED's to work can't be that hard ;-)

JimboZA: I bought a nice 240VAC:20VAC fused transformer the other day too

The fuse worked :) ...... what a jackass :blush: