How does the voltage measured by a microcontroller from the output of a 10K potentiometer differ between a 5v circuit and a 3.3v circuit? How does a change from an Arduino and 5v circuit to an ESP32 and a 3.3v circuit affect the analog values from a potentiometer as seen by a sensing analog pin on the microcontroller?
With a past project using the Arduino UNO and a 10K potentiomter connected to an analog pin on the Arduino in a 5v circuit, the Arduino provided readings in the correct range of 0 to 1023 as I turned the knob. When I changed the set point by rotating the knob, the values reported by the Arduino through the Serial Monitor would also change to follow. When I stopped rotating the knob the reported value would stick without floating.
Attempting the same project with an ESP32 and the same pot but using a 3.3v circuit, the behavior is quite different. The range reported by the ESP32 is greater since it is a 12 bit value so should be between 0 and 4095.
However what the ESP32 is reporting when I turn the knob to a new point are values that will vary, floating about some point in that range even when the knob is not turning. The variation or floating values vary by as much as 40 or sometimes more. With the Arduino the values would change when I rotated the knob and then stick and not float when I stopped twisting the knob.
I've also just removed the 3.3v connector and the ground connector from those pins of the potentiometer leaving only the lead from the sensing pin of the ESP32 to the middle pin on the pot and I still see the reported values change when I would expect them to be zero and stay zero.
I'm referencing this article https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
The source code for the pin is:
int myPin = 34;
static int iPotValSave = 0;
void setup() {
Serial.begin(9600);
delay(500);
Serial.println("begin setup()");
pinMode (myPin, INPUT);
iPotValSave = analogRead (myPin);
Serial.print (" pot value initial ");
Serial.println (iPotValSave);
}
void loop() {
int iPotVal = analogRead (myPin);
if (iPotVal != iPotValSave) {
Serial.print (" new pot value ");
Serial.println (iPotValSave);
}
delay(500);
}
My question seems to be similar to the following:
esp32-wrover ADC, incorrect values?
How to convert 10K Pot raw output ADC data to voltage and resistance from arduino uno? with an answer which seems to indicate that the ESP32 needs a reference voltage for the ADC built into the pin.
Notes
Espressif documentation Analog to Digital Converter, https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#adc-api-adc-calibration
The ESP32 ADC can be sensitive to noise leading to large discrepancies in ADC readings. To minimize noise, users may connect a 0.1uF capacitor to the ADC input pad in use. Multisampling may also be used to further mitigate the effects of noise.
and see also this section:
esp_err_t adc1_config_channel_atten(adc1_channel_tchannel, adc_atten_tatten)
Set the attenuation of a particular channel on ADC1, and configure its associated GPIO pin mux.
The default ADC full-scale voltage is 1.1 V. To read higher voltages (up to the pin maximum voltage, usually 3.3 V) requires setting >0 dB signal attenuation for that ADC channel.
How to use ADC of ESP32 – Measuring voltage example https://microcontrollerslab.com/adc-esp32-measuring-voltage-example/
ESP32 Pinout Reference: Which GPIO pins should you use? https://randomnerdtutorials.com/esp32-pinout-reference-gpios/