Problem with analogReference

Hi all

I want to use 2.56 voltages from my mega2560. What I did is as follows:

#define INTERNAL2V56
int PE = 2;
int CE = A1;
int temp_val_PE = 0;
int temp_val_CE = 0;
float val_PE;
float val_CE;

void setup()
{
analogReference(INTERNAL2V56);
Serial.begin(9600);
pinMode(PE,OUTPUT);
pinMode(CE,OUTPUT);
Serial.println("CE Val");
}
void loop()
{
analogWrite(CE,0); //reduction for blue
analogWrite(PE,128);
delay(1000);
pinMode(PE,INPUT);
temp_val_PE = analogRead(A0);
val_PE = temp_val_PE*(2.56 / 1024.0);
Serial.println("");
Serial.println(val_PE);
delay(100);
pinMode(PE,OUTPUT);
analogWrite(CE,128); //oxidation for bleach
digitalWrite(PE,LOW);
delay(1000);
}

On serial monitor it is continuously showing 2.56 while when I measure the voltages at A0 with multimeter it shows 5 volts.
What could be the problem?

thanks for reading my problem.

If you've got 5V on the input pin, then you would expect to see 2.56V printed with a 2.56V reference..

Please use code tags when posting code.

More generally an ADC cannot measure anything higher than it's reference voltage. Anything higher will max out the ADC reading. In the case of the ATmega328p, that's 1023, which your formula will calculate to be 2.56 volts.

Thanks AWOL for replying me.
@AWOL I did not understand what do you mean by 5V on the input pin? I am powering up mega2560 with USB i.e. 5V from laptop. I am using analog pin A0 and want to read its voltages after writing some input on that.

2nd: Can I use analogWrite on analog pins? as you can see that I used analogWrite(A0,128); because I need to apply varying voltages something around 2 V on A0 pin.

3rd: How I can implement some voltages which are continuously increasing from 0 to 2 volts?

4th: what are code tags?

Thanks

I did not understand what do you mean by 5V on the input pin?

when I measure the voltages at A0 with multimeter it shows 5 volts.

The input put on which you measured 5V.

2nd: Can I use analogWrite on analog pins?

No.

Code tags are explained in the “How to use the forum” thread.