user29594: In my sketch I have a potentiometer connected to A0.  The value of the pot is used to adjust the speed of a connected item.
I have set the speed to have a default value, but doing analogRead() on a pin with nothing attached gives random values rather than using the default value.  The docs state "If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.)."  https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/
How can I test whether something is connected to A0 and if not, use the default value?

user313: Take some number of analog readings over some period of time and look at the variation among the samples. Do this both with, and without, a potentiometer connected. You should see very stable readings with it connected, ranging over a few counts or less. Without it connected, the readings are likely to cover nearly the entire range A/D range.
A simple way to detect the pot would be to look at the difference between the highest and lowest readings. Pick a range that covers the "pot is connected" case. If the difference falls within that range, assume the pot is connected. Otherwise, assume it is not.
You could improve on this a number of ways, but a simple way would be to take, say, 50 readings, sort them by value, average the lowest 5 or 10 of them, and average the highest 5 or 10. Subtract those averages, and proceed as above.

user69936: The ADC input has a really high impedance (100 Meg).  All you need to do to get a consistent reading (of zero) is to connect it to ground through a suitable resistor.  100K will probably be fine, and you might be able to get away with 1 Meg.  It should be at least 10x bigger than your pot to avoid loading it when connected.

user7508: The input stage of the analog-to-digital converter is a sample-and-hold
capacitor. When you take an analog reading, the capacitor is connected
to the input pin, it is charged to the voltage you want to measure, then
it is disconnected from the input and it “holds” the voltage while the
conversion is in progress.
You can leverage this capacitor to detect whether your input pin is
floating. If you try to take an analog reading from a floating pin, the
sample-and-hold capacitor will not be able to charge or discharge
through it. You should then get a reading that is close to the
previous reading you took, although not exactly the same, as floating
pins a very prone to noise. Thus I suggest the following algorithm:

take a reading from a pin tied to GND, and discard this reading
take a reading from your input pin and record it
take a reading from a pin tied to +5V and discard it
take and record a second reading from your input pin

If the input pin is floating, the first recorded reading should be close
to zero, and the second one close to 1023.
Here is a test sketch demonstrating this approach. It uses
digitalWrite() to tie the required pins to GND and +5V, thus no
external connections are needed:
const uint8_t pin_input = A0;  // the one we are interested in
const uint8_t pin_low   = A1;  // will be tied to GND
const uint8_t pin_high  = A2;  // will be tied to +5V

void setup() {
    Serial.begin(9600);
    pinMode(pin_input, INPUT);
    pinMode(pin_low, OUTPUT);
    digitalWrite(pin_low, LOW);
    pinMode(pin_high, OUTPUT);
    digitalWrite(pin_high, HIGH);
}

void loop() {
    analogRead(pin_low);  // take a reading and discard it
    Serial.print(analogRead(pin_input));
    Serial.print(" ");
    analogRead(pin_high);  // take a reading and discard it
    Serial.println(analogRead(pin_input));
    delay(200);
}

And here is a sample output from an Arduino Uno:
167 710
271 748
287 753
287 751
284 748
281 746
279 741
271 736
268 733
264 732

It looks like the capacitor does not hold its charge very well across
consecutive readings. This is presumably caused by the parasitic
capacitance of the input pin, which partially charges and discharges the
sample-and-hold. Yet, the difference between consecutive recorded
readings (about 460) is still very high, and this very clearly shows
that the input pin is not connected to a stable voltage.

29594: <p>In my sketch I have a potentiometer connected to A0.  The value of the pot is used to adjust the speed of a connected item.</p>
<p>I have set the speed to have a default value, but doing analogRead() on a pin with nothing attached gives random values rather than using the default value.  The docs state &quot;<em>If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).</em>&quot;  <a href="https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/" rel="nofollow noreferrer">https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/</a></p>
<p>How can I test whether something is connected to A0 and if not, use the default value?</p>
 313: <p>Take some number of analog readings over some period of time and look at the variation among the samples. Do this both with, and without, a potentiometer connected. You should see very stable readings with it connected, ranging over a few counts or less. Without it connected, the readings are likely to cover nearly the entire range A/D range.</p>
<p>A simple way to detect the pot would be to look at the difference between the highest and lowest readings. Pick a range that covers the &quot;pot is connected&quot; case. If the difference falls within that range, assume the pot is connected. Otherwise, assume it is not.</p>
<p>You could improve on this a number of ways, but a simple way would be to take, say, 50 readings, sort them by value, average the lowest 5 or 10 of them, and average the highest 5 or 10. Subtract those averages, and proceed as above.</p>
 69936: <p>The ADC input has a really high impedance (100 Meg).  All you need to do to get a consistent reading (of zero) is to connect it to ground through a suitable resistor.  100K will probably be fine, and you might be able to get away with 1 Meg.  It should be at least 10x bigger than your pot to avoid loading it when connected.</p>
 7508: <p>The input stage of the analog-to-digital converter is a sample-and-hold
capacitor. When you take an analog reading, the capacitor is connected
to the input pin, it is charged to the voltage you want to measure, then
it is disconnected from the input and it “holds” the voltage while the
conversion is in progress.</p>
<p>You can leverage this capacitor to detect whether your input pin is
floating. If you try to take an analog reading from a floating pin, the
sample-and-hold capacitor will not be able to charge or discharge
through it. You should then get a reading that is close to the
<em>previous</em> reading you took, although not exactly the same, as floating
pins a very prone to noise. Thus I suggest the following algorithm:</p>
<ul>
<li>take a reading from a pin tied to GND, and discard this reading</li>
<li>take a reading from your input pin and record it</li>
<li>take a reading from a pin tied to +5V and discard it</li>
<li>take and record a second reading from your input pin</li>
</ul>
<p>If the input pin is floating, the first recorded reading should be close
to zero, and the second one close to 1023.</p>
<p>Here is a test sketch demonstrating this approach. It uses
<code>digitalWrite()</code> to tie the required pins to GND and +5V, thus no
external connections are needed:</p>
<pre class="lang-cpp prettyprint-override"><code>const uint8_t pin_input = A0;  // the one we are interested in
const uint8_t pin_low   = A1;  // will be tied to GND
const uint8_t pin_high  = A2;  // will be tied to +5V

void setup() {
    Serial.begin(9600);
    pinMode(pin_input, INPUT);
    pinMode(pin_low, OUTPUT);
    digitalWrite(pin_low, LOW);
    pinMode(pin_high, OUTPUT);
    digitalWrite(pin_high, HIGH);
}

void loop() {
    analogRead(pin_low);  // take a reading and discard it
    Serial.print(analogRead(pin_input));
    Serial.print(&quot; &quot;);
    analogRead(pin_high);  // take a reading and discard it
    Serial.println(analogRead(pin_input));
    delay(200);
}
</code></pre>
<p>And here is a sample output from an Arduino Uno:</p>
<pre><code>167 710
271 748
287 753
287 751
284 748
281 746
279 741
271 736
268 733
264 732
</code></pre>
<p>It looks like the capacitor does not hold its charge very well across
consecutive readings. This is presumably caused by the parasitic
capacitance of the input pin, which partially charges and discharges the
sample-and-hold. Yet, the difference between consecutive recorded
readings (about 460) is still very high, and this very clearly shows
that the input pin is not connected to a stable voltage.</p>
