TDS
(总溶解固体)是衡量水中溶解的各类无机和有机物质总量的指标,通常以毫克 / 升(mg/L)或百万分比(ppm)表示。这些物质包括钙、镁、钠、钾等矿物质,以及少量的有机物、重金属等。
本文为你介绍基于Arduino 如何测量当前水质的TDS值。
硬件准备
-
Arduino uno 开发板(或ESP8266) -
TDS水质传感器(12元左右) -
0.96 OLED屏幕
线路连接
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
示例代码
#include"U8glib.h"
// 传感器配置
constint TDS_PIN = A1;
constfloat REF_VOLTAGE = 5.0; // ADC参考电压(根据实际修改)
constint ADC_RESOLUTION = 1024; // ADC位数
constfloat TDS_FACTOR = 0.5; // 国家标准常用系数(需现场校准)
// 显示配置
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
// 国家标准水质分类阈值(GB/T 14848-2017)
constint TDS_GOOD = 300; // Ⅰ类水 ≤300mg/L
constint TDS_FAIR = 1000; // Ⅲ类水 ≤1000mg/L
// 汉字点阵数据(保持不变)
staticconstuint8_t PROGMEM dang[] = {0x01,0x00,0x21,0x08,0x11,0x08,0x09,0x10,0x09,0x20,0x01,0x00,0x7F,0xF8,0x00,0x08,
0x00,0x08,0x00,0x08,0x3F,0xF8,0x00,0x08,0x00,0x08,0x00,0x08,0x7F,0xF8,0x00,0x08,};
staticconstuint8_t PROGMEM qian[] = {0x10,0x10,0x08,0x10,0x08,0x20,0xFF,0xFE,0x00,0x00,0x3E,0x08,0x22,0x48,0x22,0x48,
0x3E,0x48,0x22,0x48,0x22,0x48,0x3E,0x48,0x22,0x08,0x22,0x08,0x2A,0x28,0x24,0x10,};
staticconstuint8_t PROGMEM deng[] = {0x20,0x40,0x3F,0x7E,0x48,0x90,0x85,0x08,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,
0xFF,0xFE,0x00,0x00,0x00,0x20,0x7F,0xFC,0x08,0x20,0x04,0x20,0x04,0xA0,0x00,0x40,};
staticconstuint8_t PROGMEM ji[] = {0x10,0x00,0x13,0xFC,0x20,0x84,0x20,0x88,0x48,0x88,0xF8,0x90,0x10,0x9C,0x20,0x84,
0x41,0x44,0xF9,0x44,0x41,0x28,0x01,0x28,0x1A,0x10,0xE2,0x28,0x44,0x44,0x01,0x82,};
staticconstuint8_t PROGMEM you[] = {0x08,0x90,0x08,0x88,0x08,0x88,0x10,0x80,0x17,0xFE,0x30,0xA0,0x30,0xA0,0x50,0xA0,
0x90,0xA0,0x10,0xA0,0x11,0x20,0x11,0x22,0x11,0x22,0x12,0x22,0x12,0x1E,0x14,0x00,};
staticconstuint8_t PROGMEM zhong[] = {0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,
0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,};
staticconstuint8_t PROGMEM cha[] = {0x08,0x20,0x04,0x40,0x7F,0xFC,0x01,0x00,0x01,0x00,0x3F,0xF8,0x02,0x00,0x02,0x00,
0xFF,0xFE,0x04,0x00,0x08,0x00,0x17,0xF8,0x20,0x80,0x40,0x80,0x80,0x80,0x1F,0xFC,};
// 改进的TDS读取(增加均值滤波)
floatgetTDSValue(){
constint samples = 10;
float avgVoltage = 0;
for(int i=0; i<samples; i++){
avgVoltage += analogRead(TDS_PIN) * (REF_VOLTAGE / ADC_RESOLUTION);
delay(10);
}
avgVoltage /= samples;
// 根据国家标准公式:TDS(mg/L) = k * conductivity(μS/cm)
// 典型k值范围0.55-0.8,默认0.65需校准
float tdsValue = avgVoltage * TDS_FACTOR * 1000;
return tdsValue;
}
// 国家标准水质判定
constunsignedchar* getWaterLevel(float tds){
if (tds <= TDS_GOOD) return you; // 优(Ⅰ类)
elseif (tds <= TDS_FAIR) return zhong; // 中(Ⅲ类)
elsereturn cha; // 差(超Ⅲ类)
}
voidsetup(){
Serial.begin(115200);
// 显示初始化(保持不变)
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255);
}
// ...其他显示模式设置
}
voidloop(){
staticfloat tds = 0;
tds = getTDSValue();
u8g.firstPage();
do {
draw(tds);
} while( u8g.nextPage() );
delay(500); // 适当延长采样间隔
}
// 优化显示界面
voiddraw(float tds){
u8g.setFont(u8g_font_unifont);
// 显示标题
u8g.drawBitmapP( 0, 0, 2, 16, dang);
u8g.drawBitmapP( 16, 0, 2, 16, qian);
u8g.drawStr( 32, 12, "TDS");
// 显示数值(保留1位小数)
u8g.setPrintPos(60, 12);
u8g.print(tds, 1);
u8g.drawStr( 100, 12, "mg/L"); // 国家标准单位
// 显示水质等级
u8g.drawBitmapP( 16, 32, 2, 16, deng);
u8g.drawBitmapP( 32, 32, 2, 16, ji);
constunsignedchar* level = getWaterLevel(tds);
u8g.drawBitmapP( 64, 32, 2, 16, level);
// 串口输出调试信息
Serial.print("TDS: ");
Serial.print(tds);
Serial.println(" mg/L");
}
注意,由于Arduino 内存有限,汉字显示没有ESP8266强大。因此,只能用字模软件,将需要显示的文字提取出来,放到代码中。
效果显示
注意:水质等级是按照国家标准水质分类阈值(GB/T 14848-2017)划分。Ⅰ类水 ≤300
mg/L
水质分级(行业常用)
-
0-9 mg/L:纯净水(如蒸馏水)。 -
10-60 mg/L:山泉水、矿化水(含天然矿物质)。 -
60-100 mg/L:净化水(经活性炭、超滤处理)。 -
100-300 mg/L:普通自来水。 -
300 mg/L 以上:可能存在污染,需进一步检测。
原文始发于微信公众号(kali笔记):基于Arduino 水质检测
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论