EC sensor part

References :

Three Dollar EC - PPM Meter [Arduino] | Details | Hackaday.io

Introduction

Many nations today are struggling with a workforce deficit because of aging society and lowing birth rate. My decision to work on the agriculture project, "Development of a simple smart hydroponics system," with Dr. Shinji Chiba's kind assistance, was motivated by the fact that the younger generation in Thailand, particularly in the agricultural sector, believes that it is difficult work and does not want to work in this field.

Concept

It is the development of the simple hydroponic system with the automatic added liquid fertilizer system by using M5stack. It includes 2 main parts that can be divided into 4 sections which are EC sensor part, relay controller, cloud database and cloud service.

Picture2.png

EC sensor -EC sensor: We developed an EC sensor by ourselves -relay controller: Using the values from the sensor to control the water pump

EC sensor EC is the inverse of the electrical resistance of the fluid so we will measure the resistance between two probes in the liquid of interest. EC25= EC/((1+a(T-25)) ) ; a = Temperature coefficient (we use 0.019) EC=1000/(RcK); K = 3.6 Rc=(VdropR1)/(Vin-Vdrop); Vdrop = ((Vin*raw))/4096

The converter value: We use 0.64 which is an EU conversion. (USA: 0.5, EU: 0.64) Temperature coefficient: We use 0.019 that generally considered the standard for plant nutrients, and it depends on the chemical that we are measuring.

Main code:

#include <Arduino.h>
#include <M5Stack.h>
#include "OneWire.h"
#include "DallasTemperature.h"

float CalibrationEC=1.38; //EC value of Calibration solution is s/cm
int R1= 1000;
int Ra=25; //Resistance of powering Pins
#define ECPin 3
#define ECGround 1
#define ECPower 16

 
float PPMconversion=0.64;
float K= 3.16;
float TemperatureCoef = 0.019;

#define ONE_WIRE_BUS 2        
//const int TempProbePossitive =16;  
//const int TempProbeNegative=17; 

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

float Temperature=10;
float EC=0;
float EC25 =0;
int ppm =0;
 
 
float raw= 0;
float Vin= 3.3;
float Vdrop= 0;
float Rc= 0;
float buffer=0;

void setup() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(96000);
  M5.begin();
  M5.Power.begin();
  delay(100);
  //set the resolution to 12 bits (0-4096)
  analogReadResolution(12);
  delay(100);
  sensors.begin();
  R1=(R1+Ra);
  
}

void loop() {
  dacWrite (25,0); //mute
  for (int i = 0; i <13; i++)
  {
  GetEC();
  PrintReadings();
  delay(500);  // delay in between reads for clear read from serial
  if(i==12)
    { 
      delay(500);
      M5.Lcd.clear(BLACK);
      M5.Lcd.setCursor(0,0);
      i==0;
    }
  }
}

void GetEC(){
  sensors.requestTemperatures();
  Temperature=sensors.getTempCByIndex(0);

//************Estimates Resistance of Liquid ****************//
  digitalWrite(ECPower,HIGH);
  raw = analogRead(ECPin);
  raw = analogRead(ECPin);
  digitalWrite(ECPower,LOW);

//***************** Converts to EC **************************//
Vdrop= (Vin*raw)/4096.0;
Rc=(Vdrop*R1)/(Vin-Vdrop);
Rc=Rc-Ra; //acounting for Digital Pin Resitance
EC = 1000/(Rc*K);

//*************Compensating For Temperaure********************//
EC25  =  EC/ (1+ TemperatureCoef*(Temperature-25.0));
ppm=(EC25)*(PPMconversion*1000);

}

void PrintReadings(){
Serial.print("Rc:");
Serial.print(Rc);
Serial.print(" EC:");
Serial.print(EC25);
Serial.print(" Simens");
Serial.print(ppm);
Serial.print(" ppm  ");
Serial.print(Temperature);
Serial.println(" *C ");

M5.Lcd.printf("Rc:%.1f ",Rc);
M5.Lcd.printf("EC:%.2f Simens %dppm ",EC25,ppm);
M5.Lcd.printf("Temperature:%.2fC\\n\\n",Temperature);

}

Adding the relay to add the chemical automatically

//import library 
#include <Arduino.h>
#include <M5Stack.h>
#include "OneWire.h" //get input from the wire
#include "DallasTemperature.h" //get temperature 

//define pin
#define ECPin 35
//#define ECGround 1
#define ECPower 5
#define ONE_WIRE_BUS 2 
#define relay 17

float CalibrationEC=1.38; //EC value of Calibration solution is s/cm
int R1= 1000;
int Ra=25; //Resistance of powering Pins
//***********Converting to ppm**************//
// Hana      [USA]        PPMconverion:  0.5
// Eutech    [EU]          PPMconversion:  0.64
//Tranchen  [Australia]  PPMconversion:  0.7
float PPMconversion=0.64;
float K= 3.16;
float TemperatureCoef = 0.019;//this changes depending on what chemical 
//we are measuring, 0.019 is generaly considered the standard for plant nutrient

     
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.

//set up variable
float Temperature = 10;
float EC = 0;
float EC25 = 0;
int ppm = 0;

float raw = 0;
float Vin = 3.3;
float Vdrop = 0;
float Rc = 0;
float buffer = 0;

float x = 1.5;

void setup() {
 Serial.begin(9600);
  //setup M5stack
  M5.begin();
  M5.Power.begin();
  delay(100);
  //set the resolution to 12 bits (0-4096)
  analogReadResolution(12);
  delay(100);
  //setup sensor
  sensors.begin();
  R1 = (R1 + Ra);
  //setup relay
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);

  pinMode(ECPower, OUTPUT);
}

void loop() {
  dacWrite (25,0); //mute
  for (int i = 0; i <8; i++)//loop to clear the screen
  {
  GetEC();//function to get EC value
  PrintReadings();//function to print on the screen
  //******************** Condition loop for relay *******************//
  if(EC25 <= x) // x is the EC25 value of the water when it has to add the chemical.
    {
  //the EC value is lowwer than X --> the pump on and add the chemical
      digitalWrite(relay, HIGH); //switch on
      M5.Lcd.printf("Adding chemical");//pump will work
    }
    else
    {
      //When it EC reach X --> the pump off
      digitalWrite(relay, LOW); //switch off
    }
    delay(500);  // delay in between reads for clear read from serial
    if (i == 7) //reset i value
    {
      delay(500);
      M5.Lcd.clear(BLACK);//clear screen
      M5.Lcd.setCursor(0, 0); //set cursor at the top left of the screen
      i == 0;
    }
  }
}
//function GetEC
void GetEC(){
  sensors.requestTemperatures(); // Send the command to get temperatures
  Temperature=sensors.getTempCByIndex(0);

//************Estimates Resistance of Liquid ****************//
  digitalWrite(ECPower,HIGH);
  raw = analogRead(ECPin);
	delay(2);
  raw = analogRead(ECPin);
  digitalWrite(ECPower,LOW);

//***************** Converts to EC **************************//
Vdrop= (Vin*raw)/4096.0;
Rc=(Vdrop*R1)/(Vin-Vdrop);
Rc=Rc-Ra; //acounting for Digital Pin Resitance
EC = 1000/(Rc*K);

//*************Compensating For Temperaure********************//
EC25  =  EC/ (1+ TemperatureCoef*(Temperature-25.0));
ppm=(EC25)*(PPMconversion*1000);

}

void PrintReadings(){
Serial.print("Rc:");
Serial.print(Rc);
Serial.print(" EC:");
Serial.print(EC25);
Serial.print(" Simens");
Serial.print(ppm);
Serial.print(" ppm  ");
Serial.print(Temperature);
Serial.println(" *C ");

M5.Lcd.printf("Rc:%.1f ",Rc);
M5.Lcd.printf("EC:%.2f Simens %dppm ",EC25,ppm);
M5.Lcd.printf("Temperature:%.2fC\\n\\n",Temperature);

}