0% found this document useful (0 votes)
5 views1 page

TDS Measure

The document is a C program for measuring Total Dissolved Solids (TDS) using an ADC on an ESP32 microcontroller. It configures the ADC, takes multiple samples to average the readings, converts the raw ADC value to voltage, and calculates the TDS in parts per million (ppm). The results are printed to the console every 500 milliseconds.

Uploaded by

Anh Thịnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

TDS Measure

The document is a C program for measuring Total Dissolved Solids (TDS) using an ADC on an ESP32 microcontroller. It configures the ADC, takes multiple samples to average the readings, converts the raw ADC value to voltage, and calculates the TDS in parts per million (ppm). The results are printed to the console every 500 milliseconds.

Uploaded by

Anh Thịnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"

#define TDS_ADC_CHANNEL ADC1_CHANNEL_6 // GPIO34


#define TDS_VREF 1100 // Tham chiếu điện áp (mV)
#define TDS_SAMPLE_CNT 64 // Lấy trung bình

float calculate_tds(float voltage) {


return 21.73f * voltage - 0.88f;
}

void app_main(void) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(TDS_ADC_CHANNEL, ADC_ATTEN_DB_11); // Max ~3.6V

esp_adc_cal_characteristics_t adc_chars;
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12,
TDS_VREF, &adc_chars);

while (1) {
uint32_t adc_reading = 0;
for (int i = 0; i < TDS_SAMPLE_CNT; i++) {
adc_reading += adc1_get_raw(TDS_ADC_CHANNEL);
}
adc_reading /= TDS_SAMPLE_CNT;

// ADC -> mV
uint32_t voltage_mv = esp_adc_cal_raw_to_voltage(adc_reading, &adc_chars);
float voltage_v = voltage_mv / 1000.0f;
float tds_ppm = calculate_tds(voltage_v);

printf("Voltage: %.2f V | TDS: %.2f ppm\n", voltage_v, tds_ppm);

vTaskDelay(pdMS_TO_TICKS(500));
}
}

You might also like