Reading of A0 changes when A1 is read?

Hey Guys,

I am new to this board and the Arduino in general and I was trying to built a soil moisture logger powered by a 9V battery.
I wanted to log the battery voltage. My Idea was to use a voltage divider to measure the voltage of the battery. Because I do not want to drain the battery over the voltage divider I used an optocoupler turning on and of the voltage divider by digital PIN 13. The voltage is measured by the A0 pin.

The soil moisture sensor is powered by the 5V power supply from the arduino. The signal ist read by the A1 pin.

So the thing is that when I only measure the battery voltage it reads 8.3 Volts.
At the moment I am measuring the analog voltage output of the soil moisture sensore the measured battery voltage drops to 6.9 Volts.

Why is that?

This is the sketch:

int const PowerPin=13;
int const VoltagePin = A0;
int VoltagePinVal;
float Voltage;

int const SoilMoist1Pin = A1;
int SoilMoist1Val;

float SoilMoist1Voltage;

unsigned long prevmilis = 0;

void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(PowerPin, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
unsigned long curmilis = millis();

if (curmilis - prevmilis >= 2000)
{
//Spannung messen
digitalWrite(PowerPin, HIGH);
delay(10);
VoltagePinVal = analogRead(VoltagePin);
delay(1);
digitalWrite(PowerPin, LOW);
Voltage = ((VoltagePinVal / 1024.0)5.02)-0.1;
Serial.println(Voltage);

//Messen
SoilMoist1Val = analogRead(SoilMoist1Pin);
delay(100);
SoilMoist1Voltage = (SoilMoist1Val / 1024.0)*5.0;
Serial.print(" SoilMoist1: ");
Serial.println(SoilMoist1Voltage);

prevmilis = curmilis;
}

}

Try

(void)analogRead(Voltage pin);
VoltagePinVal = analogRead(VoltagePin);
  delay(1);
  digitalWrite(PowerPin, LOW);
  Voltage = ((VoltagePinVal / 1024.0)*5.0*2)-0.1;
  Serial.println(Voltage);


  //Messen
  (void)analogRead(SoilMoist1Pin);
  SoilMoist1Val = analogRead(SoilMoist1Pin);
  delay(100);

Your voltage divider appears to use two 1M resistors. Change them to two 10k resistors and the reading should
become stable.

The analog circuitry expects your source impedance to be 10k or less for accurate readings whenever you read from more than one analog pin.

No need for the optocoupler, even a 10k+10k divider will drain way way less power than the Arduino itself.

There is also a work-around for high impedance sources - you can take two successive readings from the same
pin and ignore the first - this allows time for the sampling capacitor to charge up to the pin's voltage.