Voltage on analog pin

Hi, there.

I habe an arduino mega with a safety transformer to transform
220v to the voltage arduino board is accepting(i can measure 8-9 Volt
on the output).

the parts are working very well, but i can not get constant voltage
at the analog pin(for example A0).

keeping the 220v constant the following code:

#define LED 13

void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
analogReference(DEFAULT);
}

void loop() {
int value = analogRead(A0);
if(value != 0) {
Serial.print("Voltage: ");
Serial.println(value, DEC);
}
if(value == 0) {
Serial.print("No voltage: ");
Serial.println(value, DEC);
}
delay(1000);
}

produces following output in the loop:

No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
Voltage: 322
Voltage: 1023
Voltage: 1023
Voltage: 1023
Voltage: 1023
Voltage: 1023
Voltage: 1023
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0
Voltage: 369
Voltage: 1023
Voltage: 1023
Voltage: 1023
Voltage: 1023
Voltage: 1023
Voltage: 1023
No voltage: 0
No voltage: 0
No voltage: 0
No voltage: 0

the voltage output on the safety transformer is
constant too(8V), so the problem should be in
the arduino board.

only if i remove the outside voltage(usb) i get the
direct dependency of the voltage in the pin A0 from
the input of 220v.

how can i keep the direct dependency of the input
voltage and the voltage on A0 with the outside
voltage?

You talk about a transformer, that gives AC. A Arduino can only measure DC...

Depending on what you want you can try to time the ADC capture to get the sine of the AC or justrecity and smooth it with a capacitor to measure the peak.

Ow, and by the way, 8V is NOT a save voltage for the ADC. You can apply a maximum of Vcc + 0,5V (so 5,5V on a Uno) to it!

And have a look at my signature :wink:

What do you have connected to A0? Are you trying to measure the voltage of the "transformer"? If so STOP! You can only put less than VCC + 0.5 volts on a pin.

You need to use a voltage divider and offset circuit to arrange the signal is centred arounf 2.5V with an
amplitude of less than 2.5V - then you can capture the whole waveform and compute the rms value.
That's how ac voltages are measured properly...

That's indeed a proper way to do it. But we still don't know he really needs a proper RMS measurement... Maybe one sided is enough (giving him double the resolution) or maybe top is all he need. If you assume sinus as input you can calculate Vrms which is fine for a lot of applications...

modify the delay(1000); to delay(990);
I guess, you see more changes between 1023 and 0 then, because then you are more off sync from the 50 Hz output of your transformer…

I really wonder why you did not yet kill your arduino.

to transform 220v to the voltage arduino board is accepting (i can measure 8-9 Volt on the output)

Input Pins accept Vcc+0.5V, which is much less than 8-9 Volt. And depending on how you measure those 9 Volts ( a DMM in AC Voltage mode ? ) , it would typically have peaks of +/- 13V.

First, I'd recommend some [u]protection diodes[/u]. I like the 2nd circuit on that page with 2 diodes and a resistor. And, I recommend increasing the resistor to about 1K-10K.

Next, you'll need a [u]voltage divider[/u] (2 resistors). You can actually combine those two circuits by adding diodes the the Arduino's input, then you don't need an "extra" resistor for the protection circuit.

The Arduino will read 1023 whenever the voltage exceeds 5V and it will read zero during the negative half of the AC waveform... That's assuming it survives excess voltage and negative voltage! So you would expect to read zero half the time.

As Michael says, the peaks of an AC sine wave are about 1.4 times the RMS. And, transformers are usually rated at their maximum current rating. With a light current load, an 8V transformer might put out more than 10V RMS with no load. So once you've got your voltage divider and protection circuits, you'll probably have to calibrate your software for the actual voltage ratio out of the transformer.

Post a schematic showing all connections to arduino and specifying whether it is ac or dc.