The ESP32 ADC is a strange beast and you need to treat it differently to the Uno.
As you havent set the attenuation and resolution your reading of 4095 indicates a voltage over 2600mV.
To make the situation simpler I'd suggest connecting a pot as shown in the link, (or even just a simple 10k:10k resistive divider from the 3v3 line) and see how the readings behave.
When you change channels on an ADC the advice is to take a reading and scrap it, then take another.
Even better, to take a set of readings and average them.
Having said all that I see no reason why the most recent reading should be incorrect. How does the arduino know which is the last reading?
Hey, thanks for all the help so far! I'll have more time over the weekend to do further research. However, at the moment, using a 10K resistor between 3.3V (or 5V) and the analog input pin still results in reading the maximum value. On the other hand, a potentiometer works perfectly on all analogRead pins, even the last one being read. For additional context, I'm writing my code in Arduino IDE 2.3.2.
Regarding your question, "Having said all that, I see no reason why the most recent reading should be incorrect. How does the Arduino know which is the last reading?" β I donβt have an explanation for that. However, I noticed that if I read from an unused analog pin after reading from the pin connected to the capacitive soil sensor, the sensor pin starts working correctly. Without this extra read, the final analogRead for the sensor consistently fails, but it works fine for a potentiometer.
After further testing, I discovered that switching from the Arduino IDE to VSCode with PlatformIO tools resolved the issue. In PlatformIO, I selected the Arduino framework, and the code now works as expected. The last moisture sensor is consistently read correctly, which suggests the issue likely lies within the Arduino IDE or its third-party board packages.
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 9600 ; Set this to match Serial.begin
Switching to VSCode from the Arduino IDE solved the issue, though it doesn't explain why the Arduino IDE behaves differently. Hopefully, this information will be helpful to someone. If anyone discovers why the Arduino IDE was causing this behavior, please let me know.