Temperature Compensating Atlas's Salinity Sensor

Temperature Compensating Atlas's Salinity Sensor © CC BY-NC

How to temperature compensate the Atlas salinity sensor automatically.

  • 3,775 views
  • 0 comments
  • 5 respects

Components and supplies

Apps and online services

About this project

In this project, we will be automatically temperature compensating the salinity sensor from Atlas Scientific. Temperature changes have an impact on the conductivity/total dissolved solids/salinity of fluids and by compensating for it, we are ensuring that our reading is what it actually is at that specific temperature. Atlas's temperature sensor is used.

The temperature readings are passed to the salinity sensor after which the compensated salinity readings are outputted. Operation is via I2C protocol and readings are displayed on the Arduino serial plotter or monitor.

Advantages

  • The temperature is automatically accounted for, enabling accurate conductivity readings.
  • Real-time conductivity and temperature output.
  • Can be expanded to include more EZO sensors such as the pH and dissolved oxygen

Step 1: Pre-assembly Requirements

a) Calibrate the sensors: Each sensor has a unique calibration process. Refer to the following: salinity calibration,temperature calibration. If you are using other sensors refer to their respective datasheet which can be found on the Atlas Scientific website.

b) Set sensors' protocol to I2C and each sensor needs a unique I2C address. In accordance with the sample code for this project, the following addresses are used: salinity sensor address is 100 and temperature sensor address is 102. For information on how to change between protocols, refer to this LINK.

The calibration and the switch to I2C MUST be done before implementing the sensors into this project.

Step 2: Assemble Hardware

Connect the hardware as shown in the schematic.

You can use either an Arduino UNO or a STEMTera board. The STEMTera board was used in this project for its compact design where the Arduino is combined with the breadboard.

Datasheets: EZO EC, EZO RTD

Step 3: Load Program Onto Arduino

The code for this project makes use of a customized library and header file for the EZO circuits in I2C mode. You will have to add them to your Arduino IDE in order to use the code. The steps below include the process of making this addition to the IDE.

a) Download Ezo_I2c_lib, a zip folder from GitHub onto your computer.

b) On your computer, open the Arduino IDE (You can download the IDE from HERE if you do not have it). If you would like to use the serial plotter be sure to download the most recent version of the IDE.

c) In the IDE, go to Sketch -> Include Library -> Add.ZIP LIbrary -> Select the Ezo_I2c_lib folder you just downloaded. The appropriate files are now included.

There are two sample codes which will work for this project. You can choose either.

d) Copy the code from temp_comp_example or temp_comp_rt_example onto your IDE work panel. You can also access them from the Ezo_I2c_lib zip folder downloaded above.

The "temp_comp_example" code works by setting the temperature in the EC sensor and then take a reading. As for the "temp_comp_rt_example" code, the temperature is set and a reading is taken in one shot. Both will give the same result.

e) Compile and upload temp_comp_example or temp_comp_rt_example to your Arduino Uno or STEMTera board.

f) In your IDE, go to Tools -> Serial Plotter or press Ctrl+Shift+L on your keyboard. The plotter window will open. Set the baud rate to 9600. The real-time graphing should now begin.

h) To use the serial monitor, go to Tools -> Serial Monitor or press Ctrl+Shift+M on your keyboard. The monitor will open. Set the baud rate to 9600 and select "Carriage return". The EC and temperature readings should display.

Demonstration

Part 1: No temperature compensation

Initially, the water is at a temperature of about 30°C. It is then heated to about 65°C while the conductivity (green graph) and temperature (red graph) readings are observed on the serial plotter. (For Arduino sample code that permits the reading of multiple circuits without automatic temperature compensation refer to this LINK).

Part 2: Temperature compensation

The Arduino code that accounts for automatic temperature compensation is uploaded to the board. See this LINK for the code. Once more, the starting point of the water is around 30°C. It is gradually raised to about 65°C while the conductivity (green graph) and temperature (red graph) readings are observed on the serial plotter.

Code

Temp_comp_example codeC/C++
#include <Ezo_i2c.h>                                             //include the EZO I2C library from https://github.com/Atlas-Scientific/Ezo_I2c_lib
#include <Wire.h>                                                //include Arduinos i2c library
Ezo_board EC = Ezo_board(100, "EC");                             //create a EC circuit object, who's I2C address is 100 and name is "EC"
Ezo_board RTD = Ezo_board(102, "RTD");                           //create an RTD circuit object who's I2C address is 102 and name is "RTD"

enum reading_step {REQUEST_TEMP, READ_TEMP_AND_COMPENSATE, REQUEST_EC, READ_RESPONSE };          //the readings are taken in 3 steps
                                                                                  //step 1 tell the temp sensor to take a reading
                                                                                  //step 2 consume the temp reading and send it to EC
                                                                                  //step 4 tell the EC to take a reading based on the temp reading we just received 
                                                                                  //step 3 consume the EC readings

enum reading_step current_step = REQUEST_TEMP;                                    //the current step keeps track of where we are. lets set it to REQUEST_TEMP (step 1) on startup     

unsigned long next_step_time = 0;                                                 //holds the time in milliseconds. this is used so we know when to move between the 3 steps    
const unsigned long reading_delay = 815;                                          //how long we wait to receive a response, in milliseconds 
const unsigned int temp_delay = 300;
const unsigned int loop_delay = 5000;

void setup() {
  Wire.begin();                           //start the I2C
  Serial.begin(9600);                     //start the serial communication to the computer
}

void loop() {
  switch(current_step) {                                                          //selects what to do based on what reading_step we are in
//------------------------------------------------------------------
 
    case REQUEST_TEMP:                                                            //when we are in the first step
      if (millis() >= next_step_time) {                                           //check to see if enough time has past, if it has 
        RTD.send_read_cmd();
        next_step_time = millis() + reading_delay; //set when the response will arrive
        current_step = READ_TEMP_AND_COMPENSATE;       //switch to the receiving phase
      }
      break;
//------------------------------------------------------------------
    case READ_TEMP_AND_COMPENSATE:
      if (millis() >= next_step_time) {
        
        RTD.receive_read_cmd();                                                   //get the temp reading  
        Serial.print(RTD.get_name()); Serial.print(": ");                         //print the name of the circuit we just got a reading from
        
        if ((reading_succeeded(RTD) == true) && (RTD.get_last_received_reading() > -1000.0)) { //if the temperature reading has been received and it is valid
          EC.send_cmd_with_num("T,", RTD.get_last_received_reading());
        } else {                                                                                      //if the temperature reading is invalid
          EC.send_cmd_with_num("T,", 25.0);                                                          //send default temp = 25 deg C to EC sensor
        }

        if(RTD.get_error() == Ezo_board::SUCCESS){                                            //if the RTD reading was successful
          Serial.print(RTD.get_last_received_reading(), 1);                                   //print the reading (with 1 decimal places)
        }
        
        Serial.print(" ");    
        next_step_time = millis() + temp_delay; //set when the response will arrive
        current_step = REQUEST_EC;       //switch to the receiving phase
      }
      break;
      
    case REQUEST_EC:
      if (millis() >= next_step_time) {
        EC.send_read_cmd();
        next_step_time = millis() + reading_delay;                                           //advance the next step time by adding the delay we need for the sensor to take the reading
        current_step = READ_RESPONSE;                                                              //switch to the next step
      }
      break;
//------------------------------------------------------------------

    case READ_RESPONSE:                             //if were in the receiving phase
      if (millis() >= next_step_time) {  //and its time to get the response
        Serial.print(" ");
         
        EC.receive_read_cmd();                                                                //get the EC reading 
        Serial.print(EC.get_name()); Serial.print(": ");                                      //print the name of the circuit we just got a reading from
        
        if(reading_succeeded(EC) == true){                                                    //if the EC reading has been received and it is valid
          Serial.print(EC.get_last_received_reading(), 0);                                    //print the reading (with 0 decimal places)
        }

        Serial.println();
        next_step_time =  millis() + loop_delay;                                              //update the time for the next reading loop 
        current_step = REQUEST_TEMP;                                                          //switch back to asking for readings
      }
      break;
  }
}

bool reading_succeeded(Ezo_board &Sensor) {                                                 //this function makes sure that when we get a reading we know if it was valid or if we got an error 

  switch (Sensor.get_error()) {                                                             //switch case based on what the response code was
    case Ezo_board::SUCCESS:                                                                //if the reading was a success
      return true;                                                                          //return true, the reading succeeded 
      
    case Ezo_board::FAIL:                                                                   //if the reading faild
      Serial.print("Failed ");                                                              //print "failed"
      return false;                                                                         //return false, the reading was not successful

    case Ezo_board::NOT_READY:                                                              //if the reading was taken to early, the command has not yet finished calculating
      Serial.print("Pending ");                                                             //print "Pending"
      return false;                                                                         //return false, the reading was not successful

    case Ezo_board::NO_DATA:                                                                //the sensor has no data to send
      Serial.print("No Data ");                                                             //print "no data"
      return false;                                                                         //return false, the reading was not successful
      
    default:                                                                                //if none of the above happened
     return false;                                                                          //return false, the reading was not successful
  }
} 
Temp_comp_rt_example codeC/C++
#include <Ezo_i2c.h>                                             //include the EZO I2C library from https://github.com/Atlas-Scientific/Ezo_I2c_lib
#include <Wire.h>                                                //include Arduinos i2c library
Ezo_board EC = Ezo_board(100, "EC");                             //create a EC circuit object, who's I2C address is 100 and name is "EC"
Ezo_board RTD = Ezo_board(102, "RTD");                           //create an RTD circuit object who's I2C address is 102 and name is "RTD"

enum reading_step {REQUEST_TEMP, READ_TEMP_AND_REQUEST, READ_RESPONSE };          //the readings are taken in 3 steps
                                                                                  //step 1 tell the temp sensor to take a reading
                                                                                  //step 2 consume the temp reading and tell the EC to take a reading based on the temp reading we just received 
                                                                                  //step 3 consume the EC readings

enum reading_step current_step = REQUEST_TEMP;                                    //the current step keeps track of where we are. lets set it to REQUEST_TEMP (step 1) on startup     

unsigned long next_step_time = 0;                                                 //holds the time in milliseconds. this is used so we know when to move between the 3 steps    
const unsigned long reading_delay = 815;                                          //how long we wait to receive a response, in milliseconds 
const unsigned int temp_delay = 300;
const unsigned int loop_delay = 5000;

void setup() {
  Wire.begin();                           //start the I2C
  Serial.begin(9600);                     //start the serial communication to the computer
}

void loop() {
  switch(current_step) {                                                          //selects what to do based on what reading_step we are in
//------------------------------------------------------------------
 
    case REQUEST_TEMP:                                                            //when we are in the first step
      if (millis() >= next_step_time) {                                           //check to see if enough time has past, if it has 
        RTD.send_read_cmd();
        next_step_time = millis() + reading_delay; //set when the response will arrive
        current_step = READ_TEMP_AND_REQUEST;       //switch to the receiving phase
      }
      break;
//------------------------------------------------------------------

    case READ_TEMP_AND_REQUEST:
      if (millis() >= next_step_time) {
        
        RTD.receive_read_cmd();                                                   //get the temp reading  
        Serial.print(RTD.get_name()); Serial.print(": ");                         //print the name of the circuit we just got a reading from
        
        if ((reading_succeeded(RTD) == true) && (RTD.get_last_received_reading() > -1000.0)) {        //if the temperature reading has been received and it is valid
          EC.send_read_with_temp_comp(RTD.get_last_received_reading());                               //send readings from temp sensor to EC sensor
        } else {                                                                                      //if the temperature reading is invalid
          EC.send_read_with_temp_comp(25.0);                                                          //send default temp = 25 deg C to EC sensor
        }

        if(RTD.get_error() == Ezo_board::SUCCESS){                                            //if the RTD reading was successful
          Serial.print(RTD.get_last_received_reading(), 1);                                   //print the reading (with 1 decimal places)
        }
        
        Serial.print(" ");                                                                    //print a blank space so the output string on the serial monitor is easy to read
        
        next_step_time = millis() + reading_delay;                                           //advance the next step time by adding the delay we need for the sensor to take the reading
        current_step = READ_RESPONSE;                                                              //switch to the next step
      }
      break;
//------------------------------------------------------------------

    case READ_RESPONSE:                             //if were in the receiving phase
      if (millis() >= next_step_time) {  //and its time to get the response
        Serial.print(" ");
         
        EC.receive_read_cmd();                                                                //get the EC reading 
        Serial.print(EC.get_name()); Serial.print(": ");                                      //print the name of the circuit we just got a reading from
        
        if(reading_succeeded(EC) == true){                                                    //if the EC reading has been received and it is valid
          Serial.print(EC.get_last_received_reading(), 0);                                    //print the reading (with 0 decimal places)
        }

        Serial.println();
        next_step_time =  millis() + loop_delay;                                              //update the time for the next reading loop 
        current_step = REQUEST_TEMP;                                                          //switch back to asking for readings
      }
      break;
  }
}

bool reading_succeeded(Ezo_board &Sensor) {                                                 //this function makes sure that when we get a reading we know if it was valid or if we got an error 

  switch (Sensor.get_error()) {                                                             //switch case based on what the response code was
    case Ezo_board::SUCCESS:                                                                //if the reading was a success
      return true;                                                                          //return true, the reading succeeded 
      
    case Ezo_board::FAIL:                                                                   //if the reading faild
      Serial.print("Failed ");                                                              //print "failed"
      return false;                                                                         //return false, the reading was not successful

    case Ezo_board::NOT_READY:                                                              //if the reading was taken to early, the command has not yet finished calculating
      Serial.print("Pending ");                                                             //print "Pending"
      return false;                                                                         //return false, the reading was not successful

    case Ezo_board::NO_DATA:                                                                //the sensor has no data to send
      Serial.print("No Data ");                                                             //print "no data"
      return false;                                                                         //return false, the reading was not successful
      
    default:                                                                                //if none of the above happened
     return false;                                                                          //return false, the reading was not successful
  }
} 

Schematics

Salinity sensor temp comp wiring diagram
Ec rtd wiring diagram 33mghcxzfi

Comments

Similar projects you might like

DIY pH Dosing Pump

by Atlas Scientific

  • 5,568 views
  • 2 comments
  • 28 respects

Arduino pH Sensor Calibration

by Atlas Scientific

  • 11,612 views
  • 13 comments
  • 10 respects

How To Use DS18B20 Water Proof Temperature Sensor

Project showcase by Team IoTBoys

  • 89,363 views
  • 16 comments
  • 26 respects
Add projectSign up / Login