TroubleshootingArduino

EC sensor (K=1) coding problem

userHead Pavlos98cy 2023-03-23 06:49:49 481 Views1 Replies

I have a problem with my code. I have this EC sensor with a temperature sensor both from DFRobot. I combined their codes given from manufacturer, installed the necessary library but I have always results for EC=0. I checked the connection and everything is OK.
Here is my code:

#include <OneWire.h>
#include "DFRobot_EC.h"
#include <EEPROM.h>

// Temperature sensor i/o
int DS18S20_Pin = 2; // DS18S20 Signal pin on digital 2
OneWire ds(DS18S20_Pin);  // on digital pin 2

// EC sensor i/o
#define EC_PIN A2
float voltage,ecValue,temperature = 25;
DFRobot_EC ec;

void setup(void) {
 Serial.begin(115200); // Baud rate: 115200
 ec.begin();
}

void loop(void) {
 // Read temperature
 float temp = getTemp();
 Serial.print("Temperature: ");
 Serial.print(temp, 2);
 Serial.println(" °C");

 // Read EC
 voltage = analogRead(EC_PIN) / 1024.0 * 5000; // read the voltage
 ecValue = ec.readEC(voltage, temp); // convert voltage to EC with temperature compensation
 Serial.print("EC: ");
 Serial.print(ecValue, 2);
 Serial.println(" ms/cm");

 // EC calibration process by Serial CMD
 ec.calibration(voltage, temp);          
 delay(1000); // Wait for 1 second
}

float getTemp() {
 // Returns the temperature from one DS18S20 in DEG Celsius

 byte data[12];
 byte addr[8];

 if (!ds.search(addr)) {
   // No more sensors on chain, reset search
   ds.reset_search();
   return -1000;
 }

 if (OneWire::crc8(addr, 7) != addr[7]) {
   Serial.println("CRC is not valid!");
   return -1000;
 }

 if (addr[0] != 0x10 && addr[0] != 0x28) {
   Serial.print("Device is not recognized");
   return -1000;
 }

 ds.reset();
 ds.select(addr);
 ds.write(0x44, 1); // start conversion, with parasite power on at the end

 byte present = ds.reset();
 ds.select(addr);
 ds.write(0xBE); // Read Scratchpad

 for (int i = 0; i < 9; i++) { // we need 9 bytes
   data[i] = ds.read();
 }

 ds.reset_search();

 byte MSB = data[1];
 byte LSB = data[0];

 float tempRead = ((MSB << 8) | LSB); // using two's complement
 float TemperatureSum = tempRead / 16;

 return TemperatureSum;
}

2023-03-23 19:40:12

 

Please calibrate the EC sensor. As it read the data from EEPROM as the parameter for calculation, you need to first calibrate and save the right parameter value into the EEPROM.

About how to calibrate the sensor, see the below link.

https://wiki.dfrobot.com/Gravity__Analog_Electrical_Conductivity_Sensor___Meter_V2__K%3D1__SKU_DFR0300 

userHeadPic NeloKin