IOT

IoT-Based Drinking Water Quality Monitoring with ESP32

With the help of an ESP32 WiFi module, a TDS/EC sensor, and a temperature sensor, we will create an IoT-based drinking water quality monitoring system in this project. This entire system can be used to keep track of the water’s electrical conductivity, also known as EC, and temperature. On an IoT server called Thingspeak, the EC & temperature values may be checked online from any location in the world.

The EC value should not be greater than 400 S/cm, or 0.4mS/cm, according to WHO regulations. Water should be consumed at room temperature (20°C/68°F) for optimum flavor or chilled to 6°C/43°F for maximum cooling.

In order to detect the electrical conductivity and temperature of the water, this project employs a TDS Sensor, a DS18B20 waterproof temperature sensor, and an ESP32 WiFi Module. On a 0.96′′ I2C OLED Display, the EC and temperature values will be shown. The same information can be graphically followed on Thingspeak Server online.

Hardware Required

S.N.COMPONENTS NAMEDESCRIPTION
1ESP32 BoardESP32 ESP-32S Development Board (ESP-WROOM-32)
2TDS SensorAnalog TDS Sensor
4Temperature SensorDS18B20 Waterproof Temperature Sensor
5Resistor4.7K
6Connecting WiresJumper Wires
7Breadboard

What is Water Electrical Conductivity (EC)?

Water’s ability to transmit an electric current is known as its electrical conductivity or EC. The dissolution of salts and other compounds in water can produce positively and negatively charged ions. The electrical conductivity of water is dependent on the ion concentration because these free ions in the water conduct electricity.

Conductivity-of-water

The EC of water is calculated using salinity and total dissolved solids (TDS), which aids in determining the purity of the water. The conductivity decreases with increasing water purity. For instance, salt water is a very effective electrical conductor but pure water is almost an insulator.

Analog TDS Sensor

The Analog TDS Sensor is a TDS sensor/Meter Kit that works with Arduino and measures the TDS content of water. It can be used to test the water quality of home water, hydroponic systems, and other systems. This device is compatible with 5V or 3.3V control systems or boards because it covers a wide voltage input range of 3.3 to 5.5V and an analogue voltage output range of 0 to 2.3V.

Analog TDS Sensor

The excitation source is an AC signal, which effectively shields the probe from polarisation and increases the probe’s lifespan while also enhancing output signal stability. Waterproof, the TDS probe can be submerged for extended periods of time measurement.

TDS Sensor Board

The sensor’s TDS Measurement Range is 0 to 1000 ppm, and its accuracy is 10% FS at 25 °C. In water that is hotter than 55 degrees Celsius, the probe cannot be utilized.

Specifications

  • Input Voltage: 3.3 ~ 5.5V
  • Output Voltage: 0 ~ 2.3V
  • Working Current: 3 ~ 6mA
  • TDS Measurement Range: 0 ~ 1000ppm
  • TDS Measurement Accuracy: ± 10% F.S. (25 ℃)
  • Waterproof Probe

DS18B20 Waterproof Temperature Sensor

This pre-wired, waterproof variation of the DS18B20 Sensor is intended to measure distant objects or things that are wet. The Sensor is capable of measuring temperatures in the range of -55 to 125°C (-67 to +257°F). PVC is used to jacket the cable. These 1-wire digital temperature sensors are fairly accurate, with an average range of 0.5°C. Any microcontroller with a single digital pin can use them with excellent success.

DS18B20 Temperature Sensor

Two libraries, such as the One-Wire Library and the Dallas Temperature Sensor Library, are needed for the sensor. When using the sensor, it additionally needs a 4.7k resistor to act as a pullup from the DATA to the VCC line. You can read the previous post, DS18B20 Sensor Tutorial, to find out more information about this sensor.

Circuit: IoT-Based Water Quality Monitoring System

Let’s connect the DS18B20 temperature sensor and analog TDS sensor to the ESP32 board now. Because the temperature parameter is necessary for the Electrical Conductivity EC Value adjustment, we are utilizing a temperature sensor. The EC values fluctuate significantly when the temperature rises and falls.

Here is the project’s basic connection diagram.

IoT Based Drinking Water Quality Monitoring with ESP32

Connect the ESP32’s 3.3V and GND pins to the TDS and temperature sensor’s VCC and GND pins, respectively. Connect the ESP32 A0 Pin, or the GPIO36 Pin, to the TDS Sensor’s output analog pin. In a similar manner, attach the DS18B20’s output to the ESP32’s IO14 Pin. It is important to link the DS18B20 output pin and 3.3V VCC as a parasitic power source and pull a 4.7K resistor.

IoT Water Quality Monitoring ESP32

You may assemble the components on a breadboard for testing purposes.

Setting up Thingspeak Server

You must first set up Thingspeak in order to monitor Electrical Conductivity (EC) and temperature data on the Thingspeak server. Visit https://thingspeak.com/. to configure the Thingspeak Server. Register for an account or login in if you already have one. Next, make a new channel with the following information.

After that, copy the API Key from the dashboard’s API section. The code section requires this API key.

Source Code/Program

The IoT-based drinking water quality monitoring software with ESP32’s source code was created using the Arduino IDE and C/C++. In this project, the code makes extensive use of libraries.

Below is a list of some of the libraries needed for this project, along with a download link. Install all of these libraries in the Arduino library folder after downloading them.

1. OneWire Library: https://github.com/PaulStoffregen/OneWire

2. Dallas Temperature Library: 

https://github.com/milesburton/Arduino-Temperature-Control-Library

3. ADS1015 Library: https://github.com/adafruit/Adafruit_ADS1X15 (Install version 1.4)

4. DFRobot ESP EC Library: https://github.com/GreenPonik/DFRobot_ESP_EC_BY_GREENPONIK

5. Adafruit GFX library: https://github.com/adafruit/Adafruit-GFX-Library

6. Adafruit SSD1306 Library: https://github.com/adafruit/Adafruit_SSD1306

Below is a list of the project’s whole source code. You must modify the following lines of code before uploading it to the ESP32 Board.

String apiKey = “****************”; //  Enter your Write API key from ThingSpeak
 
const char *ssid =  “****************”; // replace with your wifi ssid and wpa2 key
const char *pass =  “****************”;

The key you previously copied from the Thingspeak setup API tab should be used in place of the Thingspeak API Key. Then, substitute your local SSID and password with the WiFi SSID and password.

The ESP32 WiFi Module’s code can now be uploaded at this point.

#include <Arduino.h>
#include <Wire.h>
#include <EEPROM.h>
#include <WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_ADS1015.h>
#include <DFRobot_ESP_EC.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
 
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
 
#define ONE_WIRE_BUS 14                // this is the gpio pin 13 on esp32.
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
DFRobot_ESP_EC ec;
Adafruit_ADS1115 ads;
 
float voltage, ecValue, temperature = 25;
 
String apiKey = “**************”; //  Enter your Write API key from ThingSpeak
 
const char *ssid =  “**************”; // replace with your wifi ssid and wpa2 key
const char *pass =  “**************”;
const char* server = “api.thingspeak.com”;
 
WiFiClient client;
 
 
void setup()
{
  Serial.begin(115200);
  EEPROM.begin(32);//needed EEPROM.begin to store calibration k in eeprom
  ec.begin();
  sensors.begin();
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128×64
    Serial.println(F(“SSD1306 allocation failed”));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();
 
  Serial.println(“Connecting to “);
  Serial.println(ssid);
 
 
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(“.”);
  }
  Serial.println(“”);
  Serial.println(“WiFi connected”);
}
 
 
void loop()
{
  voltage = analogRead(A0); // A0 is the gpio 36
  sensors.requestTemperatures();
  temperature = sensors.getTempCByIndex(0);  // read your temperature sensor to execute temperature compensation
  ecValue = ec.readEC(voltage, temperature); // convert voltage to EC with temperature compensation
 
  Serial.print(“Temperature:”);
  Serial.print(temperature, 2);
  Serial.println(“ºC”);
 
  Serial.print(“EC:”);
  Serial.println(ecValue, 2);
 
  display.setTextSize(2);
  display.setTextColor(WHITE);
 
  display.setCursor(0, 10);
  display.print(“T:”);
  display.print(temperature, 2);
  display.drawCircle(85, 10, 2, WHITE); // put degree symbol ( ° )
  display.setCursor(90, 10);
  display.print(“C”);
 
  display.setCursor(0, 40);
  display.print(“EC:”);
  display.print(ecValue, 2);
  display.display();
  delay(1500);
  display.clearDisplay();
 
  ec.calibration(voltage, temperature); // calibration process by Serail CMD
 
  if (client.connect(server, 80))  //   “184.106.153.149” or api.thingspeak.com
  {
 
    String postStr = apiKey;
    postStr += “&field1=”;
    postStr += String(temperature, 2);
    postStr += “&field2=”;
    postStr += String(ecValue, 2);
    postStr += “\r\n\r\n”;
    delay(500);
 
    client.print(“POST /update HTTP/1.1\n”);
    client.print(“Host: api.thingspeak.com\n”);
    client.print(“Connection: close\n”);
    client.print(“X-THINGSPEAKAPIKEY: ” + apiKey + “\n”);
    client.print(“Content-Type: application/x-www-form-urlencoded\n”);
    client.print(“Content-Length: “);
    client.print(postStr.length());
    client.print(“\n\n”);
    client.print(postStr);
    delay(500);
  }
  client.stop();
}

Water Quality Monitoring with ESP32 on Thingspeak Server

The ESP32 will attempt to connect to the WiFi network after the code has been uploaded to the ESP32 Board. It will begin reading the EC & Temperature values from the Sensor as soon as it is connected to the WiFi network. To read such values, open the Serial Monitor.

The room temperature will be displayed via the temperature sensor. The TDS Sensor will display a value of zero for EC when it is dry and not exposed to air. These statistics are available on an OLED display.

The OLED will show the EC & Temperature value of the water when the temperature sensor and TDS sensor are submerged in water.

More salts make water more electrically conductive and cause its EC value to significantly increase.

IoT Water Quality Monitoring

The Thingspeak dashboard lets you keep an eye on the same information as well. Visit the Thingspeak private view to view the data, where you will see the EC and temperature readings being uploaded every 15 seconds.

IoT Based Drinking Water Quality Monitoring with ESP32

Using an ESP32 WiFi Module, an analog TDS sensor, and a DS18B20 temperature sensor, you can create your own IoT-based drinking water quality monitoring system.

Conclusion

Hope this blog helps you to understand how to design an IoT-Based Drinking Water Quality Monitoring with ESP32 WiFi Module, Analog TDS Sensor & DS18B20 Temperature Sensor. We, MATHA ELECTRONICS will come back with more informative blogs.

Leave a Reply

Your email address will not be published.