user26227: I am having a hard time understanding why I can't read one analog pin's output with another analog pin. The docs state to put a delay in between, which I've done (I've tried up to one second with the same results). I've got this minimized code:
void setup() {
    Serial.begin(9600);
    pinMode(A0, INPUT);
    pinMode(A5, OUTPUT);
}

void loop() {
    for (int i=0; i<1024; i++){
      analogWrite(A5, i);
      delay(100);
      Serial.println(analogRead(A0));
    }
    Serial.println("done");
    delay(10000);
}

When it starts, it displays 6-9 for a few dozen iterations, then suddenly jumps to ~1018 and remains throughout the rest of the run.
A5 is connected directly to A0 via a 4" jumper cable. Nothing is connected to AREF. If I connect AREF to 5v, the output does not change but by a single digit. If I connect it to ground, it immediately outputs 1023.
I'm hoping this is a "do'h" moment here. Can someone please let me know what I'm not doing correctly?
Update: This is a legit Arduino Uno R3 board. When I connect A0 directly to 5v+, I get 1023 as expected as the output.

user10794: For a start, A5 is not a valid pin to do analogWrite to. On a Uno they are marked with the "~" symbol, and are pins: 3, 5, 6, 9, 10, 11.
Thus the code will treat it as a digitalWrite, and will write LOW up to 127 and HIGH after that. From the code in wiring_analog.c:
    case NOT_ON_TIMER:
    default:
        if (val < 128) {
            digitalWrite(pin, LOW);
        } else {
            digitalWrite(pin, HIGH);
        }

You are then just reading back (close to) 0 or 1023.

Second, PWM output is not what you can read back using analogRead. An analogRead reads a voltage level, whereas PWM output is just 0s and 1s interleaved at the PWM frequency, and with a width of the duty cycle.
See https://www.arduino.cc/en/Reference/AnalogWrite, in particular:

The analogWrite function has nothing to do with the analog pins or the analogRead function. 


Third, writing 1023 to analogWrite is out of range. See the documentation from the page linked above:

Parameters
pin: the pin to write to.
value: the duty cycle: between 0 (always off) and 255 (always on). 

You can see that the most you can write is 255 (not 1023).

References

More information about the ADC (analogRead) at my page about the ADC converter.
More information about PWM output at my page about timers.


user30005: 
Can someone please let me know what I'm not doing correctly?

the analog (output) pins on those chips are not exactly analog: they are pwm output pins. as such, their output is either 0 or 1, nothing in between, with the chance of being 1 equal the duty cycle.
that kind of output can be a steady "analog" if you run it through a low pass filter - the simplest would be an rc filter where the r is in serial with the c.
so if you hook th epwm pin up to a rc filter and adc the output of that rc filter, you will get a real "analog".

26227: <p>I am having a hard time understanding why I can't read one analog pin's output with another analog pin. The docs state to put a delay in between, which I've done (I've tried up to one second with the same results). I've got this minimized code:</p>

<pre><code>void setup() {
    Serial.begin(9600);
    pinMode(A0, INPUT);
    pinMode(A5, OUTPUT);
}

void loop() {
    for (int i=0; i&lt;1024; i++){
      analogWrite(A5, i);
      delay(100);
      Serial.println(analogRead(A0));
    }
    Serial.println("done");
    delay(10000);
}
</code></pre>

<p>When it starts, it displays 6-9 for a few dozen iterations, then suddenly jumps to ~1018 and remains throughout the rest of the run.</p>

<p><code>A5</code> is connected directly to <code>A0</code> via a 4" jumper cable. Nothing is connected to <code>AREF</code>. If I connect <code>AREF</code> to 5v, the output does not change but by a single digit. If I connect it to ground, it immediately outputs <code>1023</code>.</p>

<p>I'm hoping this is a "do'h" moment here. Can someone please let me know what I'm not doing correctly?</p>

<p>Update: This is a legit Arduino Uno R3 board. When I connect <code>A0</code> directly to 5v+, I get <code>1023</code> as expected as the output.</p>
 10794: <p>For a start, A5 is not a valid pin to do analogWrite to. On a Uno they are marked with the "~" symbol, and are pins: 3, 5, 6, 9, 10, 11.</p>

<p>Thus the code will treat it as a digitalWrite, and will write LOW up to 127 and HIGH after that. From the code in wiring_analog.c:</p>

<pre><code>    case NOT_ON_TIMER:
    default:
        if (val &lt; 128) {
            digitalWrite(pin, LOW);
        } else {
            digitalWrite(pin, HIGH);
        }
</code></pre>

<p>You are then just reading back (close to) 0 or 1023.</p>

<hr>

<p>Second, PWM output is not what you can read back using analogRead. An analogRead reads a voltage level, whereas PWM output is just 0s and 1s interleaved at the PWM frequency, and with a width of the duty cycle.</p>

<p>See <a href="https://www.arduino.cc/en/Reference/AnalogWrite" rel="nofollow noreferrer">https://www.arduino.cc/en/Reference/AnalogWrite</a>, in particular:</p>

<blockquote>
  <p>The analogWrite function has nothing to do with the analog pins or the analogRead function. </p>
</blockquote>

<hr>

<p>Third, writing 1023 to analogWrite is out of range. See the documentation from the page linked above:</p>

<blockquote>
  <p><strong>Parameters</strong></p>
  
  <p>pin: the pin to write to.</p>
  
  <p>value: the duty cycle: between 0 (always off) and 255 (always on). </p>
</blockquote>

<p>You can see that the most you can write is 255 (not 1023).</p>

<hr>

<h3>References</h3>

<ul>
<li><p>More information about the ADC (analogRead) at <a href="http://www.gammon.com.au/adc" rel="nofollow noreferrer">my page about the ADC converter</a>.</p></li>
<li><p>More information about PWM output at <a href="http://www.gammon.com.au/timers" rel="nofollow noreferrer">my page about timers</a>.</p></li>
</ul>
 30005: <blockquote>
  <p>Can someone please let me know what I'm not doing correctly?</p>
</blockquote>

<p>the analog (output) pins on those chips are not exactly analog: they are pwm output pins. as such, their output is either 0 or 1, nothing in between, with the chance of being 1 equal the duty cycle.</p>

<p>that kind of output can be a steady "analog" if you run it through a low pass filter - the simplest would be an rc filter where the r is in <strong><em>serial</em></strong> with the c.</p>

<p>so if you hook th epwm pin up to a rc filter and adc the output of that rc filter, you will get a real "analog".</p>
