Source and Sink on Digital pins

We are building a conductivity meter and need to reverse the polarity between our Source and Sink pins in order to break the galvanic cell.

In a spreadsheet, the whole thing works well, but in practice we find that when we have high conductivity, we suddenly are measuring negative values in both the forward and backward direction.

We use a voltage divider to measure the voltage on an analog pin while using two digital pins as source and sink.

I include a screenshot of the design spreadsheet and our sketch code.

Does anybody have an idea why we get current flow from our sink?

THanks,
Pieter

int SOURCE_PIN=6;
int SINK_PIN=5;
int Va_pin=A1;
int source;
int sink;
int frequency= 5000; //milliseconds

float R3=10000;
float R2=10000;
float Vd=1023;

void setup() {
Serial.begin(9600); // open serial port and set data rate to 9600 bps
Serial.println("Measuring water conductivity");
Serial.println("\nDIRECTION\t Va R1");

}

void loop() {
forward();
delay(frequency);
backward();
delay(frequency);
}

void toggle_circuit_direction(int source,int sink){
pinMode(source,OUTPUT);
digitalWrite(source, HIGH);
pinMode(sink,OUTPUT);
digitalWrite(sink, LOW);
}

void forward(){
Serial.print("FORWARD\t");
toggle_circuit_direction(SOURCE_PIN,SINK_PIN);
int Va; // Integer value of voltage reading
float R1; // Computed resistance of the water
Va =analogRead(Va_pin);
R1 = R2*(Vd / float(Va) - 1.0 ) - R3;
Serial.print(" ");
Serial.print(Va);
Serial.print("\t");
Serial.println(R1);
}

void backward(){
Serial.print("BACKWARD\t");
toggle_circuit_direction(SINK_PIN,SOURCE_PIN);
int Va; // Integer value of voltage reading
float R1; // Computed resistance of the water
Va = analogRead(Va_pin);
R1 = -(R2*Va)/(Va-Vd) - R3;
Serial.print(" ");
Serial.print(Va);
Serial.print("\t");
Serial.println(R1);
}

pieterclaassen:
we find that when we have high conductivity, we suddenly are measuring negative values in both the forward and backward direction.

The analogRead() function cannot return a negative value under any circumstances - it will only return a value in the range 0 .. 1023.

If your calculations are producing a negative value from that then I suggest you need to review your arithmetic to see why that is happening. To start with, I suggest you print out the raw value returned by analogRead() together with the value you have calculated from it, and if the result is not what you expect then review your calculation to see why it produced that output from that input.

Peter, you are 100% correct. analogRead() does produce nice values in a range between 0 and 1023 which is what we are expecting. If you see the attached image of the spreadsheet we used, then the problem lies with how we calculated our voltage split.

I have redone the calculation now a few times, but I am making a fundamental mistake. I might repost this to general electronics for comment.

Thanks for your feedback.

Is this it?

void toggle_circuit_direction(int source,int sink){
...
  toggle_circuit_direction(SINK_PIN,SOURCE_PIN);