Padat
Kualitas air adalah aspek penting yang mempengaruhi kesehatan dan lingkungan. Salah satu cara untuk memantau kualitas air adalah dengan menggunakan sensor TDS (Total Dissolved Solids) yang mampu mendeteksi partikel padat terlarut dalam air. Dalam panduan ini, kami akan membimbing Anda langkah demi langkah untuk membuat sistem pengukuran TDS yang sederhana menggunakan ESP32. Kami juga akan menyediakan kode gratis yang dapat Anda gunakan untuk memulai proyek ini.

Berikut Merupakan Codingan Yang Telah Kami Sediakan, Telah Diuji Untuk Memastikan Keakuratan Serta Kestabilannya.
#include <Arduino.h>
#define TdsSensorPin 34
#define VREF 3.3 // analog reference voltage(Volt) of the ADC
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0, copyIndex = 0;
float averageVoltage = 0, tdsValue = 0, temperature = 29;
void proses_analog_tds() {
int a = analogRead(TdsSensorPin);
if (a != 0) {
static unsigned long analogSampleTimepoint = millis();
if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = a; //read the analog value and store into the buffer
analogBufferIndex++;
if (analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long printTimepoint = millis();
if (millis() - printTimepoint > 800U) {
printTimepoint = millis();
for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 4096.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //convert voltage value to tds value
Serial.print("TDS:");
Serial.print(tdsValue, 0);
Serial.println("ppm");
// d
}
}
}
int getMedianNum(int bArray[], int iFilterLen) {
int bTab[iFilterLen];
for (byte i = 0; i < iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++) {
for (i = 0; i < iFilterLen - j - 1; i++) {
if (bTab[i] > bTab[i + 1]) {
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}
void setup()
{
Serial.begin(9600);
Serial.begin(9600);
pinMode(TdsSensorPin, INPUT);
}
void loop(){
proses_analog_tds();
delay(1000);
}
untuk skematik, kamu bisa menggunakan gambar berikut:

| Sensor TDS Pin | ESP32 Pin |
|---|
| VCC | 3.3V |
| GND | GND |
| AOUT (Analog Output) | GPIO 34 |
Selamat mencoba! Semoga sukses dengan proyekmu. Jadikan setiap langkah sebagai pengalaman berharga. Salam dari Labrobotika, tempat inovasi dan kreativitas!
Jika anda berminat menggunakan jasa labrobotika, silahkan hubungi admin: WHATSAPP

