Hi,
I just finished my first Arduino script. I use a mini pro 3.3V ATmega328 and want to mesasure TDS/conductivity of water. I found a description of a sensor principle in this discussion , or more directly, in this PDF.
I connected my sensor (2 stainless steel rods) as seen in the diagramm, the capacitor is 3.3nF.
This is my code:
void setup() {
Serial.begin(9600);
}
void loop() {
for (int k = 0; k < 10; k++) {
float tds;
tds = measure_tds();
Serial.println(tds);
delay(500); //wait 0.5 sec
}
}
// measures the duration (in microseconds) a digital pin is in HIGH or LOW state
// If threshold is exeeded, value of threshold is given instead
unsigned long change_microsec(int pin, int state, unsigned long thres) {
int new_state = 0; // current state of the digital input
unsigned long duration = 0;
unsigned long starttime = 0;
unsigned long endtime = 0;
new_state = digitalRead(pin);
starttime = micros();
while (new_state == state && duration < thres) {
endtime = micros();
duration = endtime - starttime;
new_state = digitalRead(pin);
}
endtime = micros();
duration = endtime - starttime;
return duration;
}
float measure_tds() {
int pin_Cplus = 2; //C+
int pin_Cminus = 3; //C-
int pin_EC = 4; //EC
int freq = 3000; // Frequency of min. oszillation in Hz
#define n_measure 16
int t1_array[n_measure]; //Make empty array containing t1 values
int t2_array[n_measure]; //Make empty array containing t2 values
for (int i = 0; i < n_measure; i++) {
int t1 = 0;
int t2 = 0;
//Step 1
pinMode(pin_EC, INPUT); //high impedance state?
pinMode(pin_Cplus, OUTPUT);
digitalWrite(pin_Cplus, HIGH);
pinMode(pin_Cminus, OUTPUT);
digitalWrite(pin_Cminus, LOW); // charge capacitor to Vcc
//Step 2
pinMode(pin_Cplus, INPUT);
pinMode(pin_EC, OUTPUT);
digitalWrite(pin_EC, LOW); //C- and EC "linked" to ground
//Step 3
t1 = change_microsec(pin_Cplus, HIGH , 1 / freq); //Measure time in microseconds needed to go from HIGH to LOWs
//Step 4
pinMode(pin_EC, INPUT);
pinMode(pin_Cplus, OUTPUT);
digitalWrite(pin_Cplus, HIGH);
pinMode(pin_Cminus, OUTPUT);
digitalWrite(pin_Cminus, HIGH); // Capacitor will be completely discharged
//Step 5
digitalWrite(pin_Cplus, LOW); //Charge the capacitor to an opposite voltage than that of step 1
//Step 6
//Pin C+ direction is set to input (high impedance) and pin EC is set as output (high logic level)
//In this way, Rx and C1 are put in parallel and referenced to Vcc
//The discharge time T2 in which the C+ voltage rises to 2/3 of Vcc is measured
pinMode(pin_Cplus, INPUT);
pinMode(pin_EC, OUTPUT);
digitalWrite(pin_EC, HIGH);
t2 = change_microsec(pin_Cplus, LOW, 1 / freq); //Measure time in microseconds needed to go from LOW to HIGH
//Step 7
//Pin EC is set as high impedance, pins C- and C+ are set to low level output. Temperature value is read from the sensor
pinMode(pin_EC, INPUT);
pinMode(pin_Cplus, OUTPUT);
digitalWrite(pin_Cplus, LOW);
pinMode(pin_Cminus, OUTPUT);
digitalWrite(pin_Cminus, LOW);
//read temp
//write t1 and t2, eventually include temperature compensation
t1_array[i] = t1;
t2_array[i] = t2;
}
//calculate mean
float tds_raw_mean = 0;
for (int j = 0; j < n_measure; j++) {
tds_raw_mean += t1_array[j] + t2_array[j];
}
tds_raw_mean = tds_raw_mean / (2 * n_measure);
return tds_raw_mean;
}
It's the first ever code I wrote for Arduino otherwise I just used R, so please excuse my mistakes.
I don't get reasonable results, just values of 6.50,6.75,7.00, 7.25 and 7.50. Mostly 7.00.
The values do not change if I connect anything to pins 2-4.
Can you help me figuring out the problem?