If you use a resistor over 100k, use the double-read trick to give the sample/hold capacitor time to settle. The hardware ADC timing is designed for signal impedance of 100k or less.
Actually, I've written procedures like this in connection with random seeding. There is enough intrinsic capacitance in the pin (as shown) that you don't need an external cap.
I think what I did was pull the pin LOW, then configure it as INPUT_PULLUP and start to take readings.
Is that a good thing or a bad thing? That’s over 22 instruction cycles and an ADC read is about 13 instruction cycles.
I misremembered the target input impedance. It is 10k, not 100k. If your current source has an impedance higher than 10k you should read twice.
It's bad. I guess what I did was really, enable output and then disable, and observe the drift. I had to migrate a lot of work between computers, it's hard to find older sketches now.
I was so intrigued by this idea that I had to try it. Thanks @johnwasser !
I soon discovered you need to set the analog pin mode to input again before taking a reading.
With the right value of pot and capacitor you can even diagnose WHICH connection is broken.
See results below.
For completeness, here is my simple sketch running on a nano
/*
Analog Input Example - circuit as my schematic tutorial
modified for arduino nano
Demonstrates analog input by reading an analog sensor and
turning on and off a light emitting diode(LED)
The amount of time the LED will be on and off depends on the value obtained
by analogRead().
The circuit:
- potentiometer
center pin of the potentiometer to the analog input A0
one side pin (either one) to ground
the other side pin to +5V
- LED
anode (long leg) attached to digital output 13 through 220 ohm resistor
cathode (short leg) attached to ground
- Note: because most Arduinos have a built-in LED attached to pin 13 on the
board, the LED is optional.
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select pin 13 for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
analogReference(DEFAULT);
// Begin serial connection
Serial.begin(115200);
delay(200);
Serial.println("Analog input demo of schematic tutorial circuit");
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin); //follows delay so should be OK
Serial.print("Analog input raw value is: ");
Serial.println(sensorValue);
//test idea for determining if pot is not properly connected
//note analog input will not read correctly if it has been configured as an output.
//send a HIGH
pinMode(sensorPin, OUTPUT);
digitalWrite(sensorPin, HIGH);
pinMode(sensorPin, INPUT);
sensorValue = analogRead(sensorPin);
Serial.print("Analog reading following a HIGH is: ");
Serial.println(sensorValue);
sensorValue = analogRead(sensorPin);
Serial.print("Second analog input following a HIGH is: ");
Serial.println(sensorValue);
//now send a LOW
pinMode(sensorPin, OUTPUT);
digitalWrite(sensorPin, LOW);
pinMode(sensorPin, INPUT);
sensorValue = analogRead(sensorPin);
Serial.print("Analog input following a LOW is: ");
Serial.println(sensorValue);
sensorValue = analogRead(sensorPin);
Serial.print("Second analog input following a LOW is: ");
Serial.println(sensorValue);
//
Serial.println("three readings complete ");
Serial.println(" ");
// use LED to indicate pot setting
digitalWrite(ledPin, HIGH);
delay(sensorValue + 27); //min 27 max 1250
// turn the ledPin off:
digitalWrite(ledPin, LOW);
//delay(1050 - sensorValue); //max 1250 min 27
delay(6000);
}
and the hardware; except the pot is a 10k. I'm connecting a cap between A0 and ground.
The results with the pot set to about halfway:
With no external cap:
Analog input raw value is: 504
Analog reading following a HIGH is: 504
Second analog input following a HIGH is: 504
Analog input following a LOW is: 504
Second analog input following a LOW is: 504
three readings complete
Pot slider disconnected: *** you can see the cap discharging
Analog input raw value is: 255
Analog reading following a HIGH is: 1021
Second analog input following a HIGH is: 1007
Analog input following a LOW is: 0
Second analog input following a LOW is: 34
three readings complete
Now with a 2n7 cap:
pot fully connected
Analog input raw value is: 504
Analog reading following a HIGH is: 543
Second analog input following a HIGH is: 503
Analog input following a LOW is: 470
Second analog input following a LOW is: 503
three readings complete
+5 to pot removed:
Analog input raw value is: 0
Analog reading following a HIGH is: 228
Second analog input following a HIGH is: 0
Analog input following a LOW is: 0
Second analog input following a LOW is: 0
three readings complete
0V to pot removed:
Analog input raw value is: 1023
Analog reading following a HIGH is: 1023
Second analog input following a HIGH is: 1023
Analog input following a LOW is: 784
Second analog input following a LOW is: 1023
three readings complete
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.