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);
}