How to Connect an Electrical Conductivity Sensor to a Raspberry Pi

by | May 23, 2016

If you have a saltwater pool and are using a chlorinator to provide most of the chlorine then knowing the amount of dissolved salt in the water is crucial to maintaining the health and safety of your pool.

Electrical Conductivity sensors provide a reading in micro-siemens, which is a measure of conductance (as opposed to resistance).  Using this we can calculate a salinity reading in parts per million(ppm).

The Atlas Scientific Electrical Conductivity sensor is an industrial grade sensor that works well with the Raspberry Pi, it is fully submersible up to the BNC connector in saltwater. The sensor can work in serial or via the I2C protocol, for this project you will be configuring the sensor to use the I2C interface on the Pi.

To configure the Pi I am assuming that you are running the latest version of Raspian and have the ability to connect to your Pi either through SSH with putty and FTP with Filezilla, or directly with a keyboard and monitor if you haven’t set-up your Pi yet then check out my getting started section.

 

Materials

 

In this tutorial I will be using the following materials:

 

The first thing we need to do is enable the I2C interface on the Pi. This is done by entering the following at the command prompt to start the configuration tool

 

 

select option 9 – Advanced Options
select option A7 – I2C
select “Yes” for all the questions and reboot the Pi

 

 

Note: The GPIO pins 2&3 on the Pi have now been configured as the Serial Data Line (SDA) and Serial Clock Line (SCL) for use by the I2C protocol. The TX connection of the sensor circuit will connect to SDA (pin 2) on the Pi and the RX connection will go to the SCL (pin 3).

 

After the reboot ensure that all of the Raspbian packages are up to date by running the following commands.

 

 
Next, check/add the i2c tools package

 

 

This should produce the following without the sensor attached.

 

i2c-detect

 

Now that we have our I2C module working correctly we can go ahead and connect our EC sensor. 

When describing the physical pin connections I will be following the GPIO pin numbering convention shown below.

 

gpio-numbers-pi2

 

Firstly we need to get the EC circuit into the correct mode, when delivered the EC circuit will be in UART (serial) mode, the EC circuit has to be manually switched from UART mode to I2C mode. When this is done the EC circuit will have its I2C address set to 100 (0x64 Hexadecimal).

Using your breadboard perform the following actions

  • Cut the power to the device
  • Disconnect any jumper wires going from TX and RX to the Pi
  • Short the PRB pin to the TX pin
  • Power the device
  • Wait for LED to change from Green to Blue
  • Remove the short from the probe pin to the TX pin
  • Power cycle the device

 

ec-manual-i2c-config

 

The device is now I2C mode.

The Pi and EC circuit are now configured so we can go ahead and connect it all together.

 

Atlas Scientific Electrical Conductivity Sensor to Raspberry Pi Circuit diagram

 

Assuming that all of the parts are now mounted on your breadboard

  • Connect the GND pin of the EC circuit to the ground pin of your RPi.
  • Connect the TX(SDA) pin of the EC circuit to GPIO pin 2.
  • Connect the RX(SCL) pin of the EC circuit to GPIO pin 3.

 

Note: Do Not Use jumper wires for these connections or your readings will not be accurate.
EC-Circuit-Connection-OK
EC-Circuit-Connection-Not-OK

 

  • The 2 PRB pins should be connected via your breadboard to the center and shield pins of your BNC connector.
Note: It makes no difference which way around the pins on the E.C. circuit are connected to the two BNC pins.

 

  • Finally, power your EC circuit by connecting the Vcc pin to the +3.3V pin.

You can now run a quick test to prove that we are set up correctly, from the command prompt enter the following:

 

 

you should see the following response, if not then check your connections, ensure the light on the EC circuit is blue and reboot your Pi.

 

i2cdetect-output

 

In the image above I have 3 sensors connected to my Pi, the EC sensor connection is indicated by Hex value 64. The factory pre-set address for the EC sensor is 100 or 64 in hexadecimal as mentioned above, if you have more than 1 EC circuit connected then you will need to specify a different value. To do this we need to add some python code to our Pi.

Atlas Scientific provides the python code that I will be using here for interfacing with the EC Circuit.

We start by importing the required python modules

 

 

Next, we add the class code to interface with the EC circuit (or any other Atlas Scientific circuit for that matter)

 

 

Finally, we will add our main program

 

 

All of this python code is available on my HydroPi GitHub Repository.

We now transfer our code to our chosen folder on the Pi using an FTP client and then run the program.

 

 

Atlas Scientific Electrical Conductivity Sensor Code screen capture

 

The screenshot above shows that we are ready to start sending commands to our EC circuit, to confirm that sensor is now fully functioning we will enter the following command.

 

 

This will poll the sensor every 5 seconds and return the result until a ctrl-c command is entered as shown below, to stop the program enter ctrl-c again.

 

ec-sensor-code-polling

 

With the sensor now working, there are also a series of other commands that we now have available to us to configure our probe.

The EC circuit actually outputs 4 different values in a CSV string, Electrical Conductivity(EC), Total Dissolved Solids(TDS), Salinity(S) and Specific Gravity(SG) as can be seen above.  For the purposes of determining the salinity of a pool, we will only be using the EC value. The additional commands that can be seen in the print out are explained just below under the title Removing Output Parameters.

 

Warning: It is important to know that the EC circuit is discharging a small electrical current into the water. This small current creates an interference field that can be detected by other devices such as a pH or ORP probe. This may make other devices inaccurate. If you are using other sensors in your project then it is important to electrically isolate them from the EC Circuit, this can be done using an Atlas Scientific Pwr-Iso board on each of the circuits.

 

Enable/disable the LED on the EC Circuit:

 

L,1 -LED enable
L,0 – LED disable
L,? – Query the LED

 

Take a single reading:

 

R – Returns a single reading

 

Set the Probe Type:

 

K,(Any value between 0.1 and 10) – Should be set to the value of your probe (0.1, 1, 10), for this project K1.0 is the correct choice.
K,? – Report the current K value

 

Temperature Compensation:

 

To ensure accurate results the probe needs to know the temperature of the liquid it is measuring in °C, the factory default is 25°C

T,22.5 – Set the temperature offset value
T,? – Query the set temperature

 

Calibration:

 

The EC circuit can be calibrated using a single point or double point reference

Cal,dry – This should always be the first reference point you configure.  The probe must be completely dry, as any liquid will affect the result.

Single Point Calibration

Cal,one,n – Used for single point calibration, n is any EC value expressed in micro-siemens

Double Point Calibration

Cal,low,n – Used for the double point calibration, n is the low test EC value expressed in micro-siemens
Cal,high,n – Used for the double point calibration, n is the high test EC value expressed in micro-siemens
Cal,clear – Clears all calibration data
Cal,? – Query the calibration

 

Removing Output Parameters:

 

The EC circuit outputs 4 different values in a CSV string, if you don’t want all of these parameters then some will have to be removed from the output.

O,(parameter),(1 or 0) – Parameter can be any one of the four listed above(EC, TDS, S, SG).To enable the parameter set the second value to 1, to disable set it to 0.
O,? – Displays the enabled/disabled state of each of the parameters.

See the image above for example.

 

Circuit Address Change:

 

I2c,n – n is the new decimal address
Changes to the address of the circuit will cause a loss of connectivity until the python script is restarted with the new address.

 

Info, Status, Low Power and Factory Reset:

 

I – Device information
STATUS – Reports the reason for the last reboot and the Vcc voltage
FACTORY – Factory reset. This will not change the communications protocol back to UART.
SLEEP – Enter a low power sleep state.

Note: Any command sent to the EC circuit will wake it but 4 readings should be taken before considering them to be accurate.

 

If you would like to see how I have implemented the EC sensor why not check out my Hydropi overview page which has all the articles related to my own personal project.

For more information on the configuration of the Atlas Scientific EC circuit read this.

There we have it, you have now configured your RPi to interface with the Atlas Scientific EC Sensor.

If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.

Pin It on Pinterest

Share This