当前位置:   article > 正文

Arduino 水质检测仪(浊度、TDS、温度、电导率、吸光度、硬度)带18b20温度补偿。_水质ppm检测电路

水质ppm检测电路

 

1、arduino代码如下:

可直接进行串口打印或者oled显示(spi)

  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <math.h>
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_SSD1306.h>
  6. #include <Fonts/FreeSerif9pt7b.h>
  7. #include <MsTimer2.h>
  8. #include "TSL2581.h"
  9. //---------------------------TDS-----------------------------------
  10. #include <OneWire.h>
  11. #include <DallasTemperature.h>
  12. //-----------------------------------------------------------------
  13. #include <SoftwareSerial.h> //开启软串口
  14. SoftwareSerial mySerial(2 , 3); //第2= RX,第3= TX //开启软串口
  15. WaveShare_TSL2581 tsl = WaveShare_TSL2581();
  16. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  17. #define SCREEN_HEIGHT 32 // OLED display height, in pixels
  18. // Declaration for SSD1306 display connected using software SPI (default case):
  19. #define OLED_MOSI 9
  20. #define OLED_CLK 10
  21. #define OLED_DC 11
  22. #define OLED_CS 12
  23. #define OLED_RESET 13
  24. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  25. OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  26. #define NUMFLAKES 10 // Number of snowflakes in the animation example
  27. #define Blank_KEY 7
  28. #define Dark_KEY_INT 1
  29. #define LED_Control 8
  30. //************************* User Defined Variables for TDS********************************************************//
  31. //##################################################################################
  32. //----------- Do not Replace R1 with a resistor lower than 300 ohms ------------
  33. //##################################################################################
  34. int R1= 1000;
  35. int Ra=25; //Resistance of powering Pins
  36. int ECPin= A0;
  37. int ECGround=A1;
  38. int ECPower =A3;
  39. //*********** Converting to ppm [Learn to use EC it is much better**************//
  40. // Hana [USA] PPMconverion: 0.5
  41. // Eutech [EU] PPMconversion: 0.64
  42. //Tranchen [Australia] PPMconversion: 0.7
  43. // Why didnt anyone standardise this?
  44. float PPMconversion=0.7;
  45. //*************Compensating for temperature ************************************//
  46. //The value below will change depending on what chemical solution we are measuring
  47. //0.019 is generaly considered the standard for plant nutrients [google "Temperature compensation EC" for more info
  48. float TemperatureCoef = 0.019; //this changes depending on what chemical we are measuring
  49. //********************** Cell Constant For Ec Measurements *********************//
  50. //Mine was around 2.9 with plugs being a standard size they should all be around the same
  51. //But If you get bad readings you can use the calibration script and fluid to get a better estimate for K
  52. float K=2.88;
  53. //************ Temp Probe Related *********************************************//
  54. #define ONE_WIRE_BUS 6 // Data wire For Temp Probe is plugged into pin 6 on the Arduino
  55. const int TempProbePossitive =5; //Temp Probe power connected to pin 5
  56. const int TempProbeNegative=4; //Temp Probe Negative connected to pin 4
  57. //***************************** END Of Recomended User Inputs *****************************************************************//
  58. void read2581_id(void);
  59. void displayValue(void);
  60. unsigned long TSL2581_CH0=0,TSL2581_CH1=0;
  61. float Absorbance=0.0;
  62. float Turbidity=0.0;
  63. unsigned int dark=0;
  64. unsigned int blank=0;
  65. int inCOMdate=0;
  66. volatile int darkState = LOW;
  67. volatile int timerState = LOW;
  68. //----------------------------------------------TDS-------------------------------------------------------------
  69. OneWire oneWire(ONE_WIRE_BUS);// Setup a oneWire instance to communicate with any OneWire devices
  70. DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
  71. float Temperature=10;
  72. float EC=0;// EC
  73. float EC25 =0;// EC for 25oC
  74. int ppm =0;// TDS
  75. float GH=0;// hardness of water
  76. float raw= 0;
  77. float Vin= 5;
  78. float Vdrop= 0;
  79. float Rc= 0;
  80. float buffer=0;
  81. int timeCount=0;
  82. //-------------------------------------------------------------------------------------------------------------
  83. void setup() {
  84. //------------------------------------Serial----------------------
  85. Serial.begin(115200);
  86. mySerial.begin(115200); // 设置虚拟串口波特率
  87. //---------------------------------LED and KEY---------------------
  88. pinMode(Blank_KEY, INPUT_PULLUP);
  89. pinMode(LED_Control,OUTPUT);
  90. digitalWrite(LED_Control, LOW);//led ON
  91. attachInterrupt(Dark_KEY_INT, dark_KEY, LOW);
  92. //-------------------------------18B20 and EC------------------------
  93. pinMode(TempProbeNegative , OUTPUT ); //seting ground pin as output for tmp probe
  94. digitalWrite(TempProbeNegative , LOW );//Seting it to ground so it can sink current
  95. pinMode(TempProbePossitive , OUTPUT );//ditto but for positive
  96. digitalWrite(TempProbePossitive , HIGH );
  97. pinMode(ECPin,INPUT);
  98. pinMode(ECPower,OUTPUT);//Setting pin for sourcing current
  99. pinMode(ECGround,OUTPUT);//setting pin for sinking current
  100. digitalWrite(ECGround,LOW);//We can leave the ground connected permanantly
  101. //-----------------------------18B20 one wire-----------------------------------
  102. delay(100);// gives sensor time to settle
  103. sensors.begin();
  104. delay(100);
  105. //** Adding Digital Pin Resistance to [25 ohm] to the static Resistor *********//
  106. // Consule Read-Me for Why, or just accept it as true
  107. R1=(R1+Ra);// Taking into acount Powering Pin Resitance
  108. //---------------------------------SSD1306 OLED-------------------
  109. // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  110. if(!display.begin(SSD1306_SWITCHCAPVCC)) {
  111. //Serial.println(F("SSD1306 allocation failed"));
  112. for(;;); // Don't proceed, loop forever
  113. }
  114. display.display();
  115. display.clearDisplay();
  116. displayValue();
  117. //------------------------------------TSL2581---------------------
  118. Wire.begin(); //i2c config
  119. read2581_id();
  120. /* Setup the sensor power on */
  121. tsl.TSL2581_power_on();
  122. delay(2000);
  123. // /* Setup the sensor gain and integration time */
  124. tsl.TSL2581_config();
  125. //----------------------------------------------------------------
  126. MsTimer2::set(250, acquireData); // 中断设置函数,每 500ms 进入一次中断
  127. MsTimer2::start();
  128. }
  129. void loop() {
  130. if(timerState)//采集数据
  131. {
  132. timeCount++;// add 1 of 500ms
  133. tsl.TSL2581_Read_Channel();
  134. TSL2581_CH0 = tsl.calculateLux(1, NOM_INTEG_CYCLE);
  135. TSL2581_CH1 = tsl.calculateLux(2, NOM_INTEG_CYCLE);
  136. }
  137. if(timeCount >= 10)//每5秒执行一次电导率采集,不能小于5秒
  138. {
  139. timeCount =0;
  140. GetEC();
  141. PrintReadings();
  142. mySerial.write(ppm);
  143. mySerial.write(ppm);
  144. mySerial.write(Temperature);
  145. mySerial.write(Temperature);
  146. }
  147. if (Serial.available() > 0)//串口接收到数据
  148. {
  149. inCOMdate = Serial.read();//获取串口接收到的数据
  150. }
  151. switch(inCOMdate){
  152. case 'T':{inCOMdate =0;Serial.println(Turbidity,1);};break;// output the Turbidity
  153. case 'E':{inCOMdate =0;Serial.println(EC25,2);};break;// output the EC
  154. case 'S':{inCOMdate =0;Serial.println(ppm);};break;// output the TDS
  155. case 'G':{inCOMdate =0;Serial.println(GH,1);};break;// output the Hardness
  156. case 'W':{inCOMdate =0;Serial.println(Temperature,1);};break;// output the Temperature
  157. default:break;
  158. }
  159. if((!digitalRead(Blank_KEY)) || (inCOMdate == 'B'))//KEY1 or COM=D
  160. {
  161. inCOMdate =0;
  162. blank = TSL2581_CH0;
  163. }
  164. if(darkState || (inCOMdate == 'D'))//采集暗电流
  165. {
  166. inCOMdate =0;
  167. digitalWrite(LED_Control, HIGH);//led OFF
  168. delay(500);
  169. tsl.TSL2581_Read_Channel();
  170. TSL2581_CH0 = tsl.calculateLux(1, NOM_INTEG_CYCLE);
  171. TSL2581_CH1 = tsl.calculateLux(2, NOM_INTEG_CYCLE);
  172. dark = TSL2581_CH0;
  173. darkState = LOW;
  174. digitalWrite(LED_Control, LOW);//led ON
  175. }
  176. calculate();//计算吸光度和浊度
  177. displayValue();//display data
  178. }
  179. void dark_KEY(){
  180. darkState = HIGH;
  181. }
  182. void acquireData(){
  183. timerState = !timerState;
  184. }
  185. void read2581_id(void)
  186. {
  187. int id;
  188. int a;
  189. id = tsl.TSL2581_Read_ID();
  190. a = id & 0xf0; //The lower four bits are the silicon version number
  191. if (!(a == 144)) //ID = 90H = 144D
  192. {
  193. //Serial.println("false....... ");
  194. display.clearDisplay();
  195. display.setCursor(0,10);
  196. display.print(F("false....... "));
  197. display.display();
  198. delay(500);
  199. } else {
  200. //Serial.print("I2C DEV is working ,id = ");
  201. //Serial.println(id);
  202. display.clearDisplay();
  203. display.setCursor(0,0);
  204. display.println(F("I2C DEV is working "));
  205. display.print(F("id = "));
  206. display.println(id);
  207. display.display();
  208. delay(500);
  209. }
  210. }
  211. void calculate(void){
  212. float temp1 = blank - dark;
  213. float temp2 = TSL2581_CH0 - dark;
  214. Absorbance = log10(temp1/temp2);
  215. Turbidity = (Absorbance - 0.0029)/0.004;//根据工作曲线计算斜率和截距
  216. }
  217. void displayValue(void) {
  218. display.clearDisplay();
  219. display.setFont(&FreeSerif9pt7b);
  220. // display.setTextSize(1);
  221. display.setTextColor(WHITE);
  222. // display.setTextColor(BLACK,WHITE); // Draw 'inverse' text
  223. display.setCursor(10,12);
  224. display.print(Absorbance,4);
  225. display.println(F(" A"));
  226. display.setFont();
  227. display.setCursor(89,4);
  228. display.print(TSL2581_CH0);
  229. display.setCursor(0,16);
  230. display.print(F("Dark = "));
  231. display.print(dark);
  232. display.println(F(";"));
  233. display.print(F("Blank = "));
  234. display.print(blank);
  235. display.display();
  236. }
  237. //************ This Loop Is called From Main Loop************************//
  238. void GetEC(){
  239. //*********Reading Temperature Of Solution *******************//
  240. sensors.requestTemperatures();// Send the command to get temperatures
  241. Temperature=sensors.getTempCByIndex(0); //Stores Value in Variable
  242. //************Estimates Resistance of Liquid ****************//
  243. digitalWrite(ECPower,HIGH);
  244. raw= analogRead(ECPin);
  245. raw= analogRead(ECPin);// This is not a mistake, First reading will be low beause if charged a capacitor
  246. digitalWrite(ECPower,LOW);
  247. //***************** Converts to EC **************************//
  248. Vdrop= (Vin*raw)/1024.0;
  249. Rc=(Vdrop*R1)/(Vin-Vdrop);
  250. Rc=Rc-Ra; //acounting for Digital Pin Resitance
  251. EC = 1000/(Rc*K);
  252. //*************Compensating For Temperaure********************//
  253. EC25 = EC/ (1+ TemperatureCoef*(Temperature-25.0));
  254. ppm=(EC25)*(PPMconversion*1000);
  255. //--------------GH value---------------------------------
  256. if(ppm < 160)
  257. {
  258. GH = ppm / 20.0;
  259. }
  260. else
  261. {
  262. GH = ppm / 18.2;
  263. }
  264. }
  265. //************************** End OF EC Function ***************************//
  266. //***This Loop Is called From Main Loop- Prints to serial usefull info ***//
  267. void PrintReadings(){
  268. Serial.print("Rc: ");
  269. Serial.print(Rc);
  270. Serial.print(" EC: ");
  271. Serial.print(EC25);
  272. Serial.print(" Simens ");
  273. Serial.print(ppm);
  274. Serial.print(" ppm ");
  275. Serial.print(Temperature);
  276. Serial.println(" *C ");
  277. };

 

/*开启软串口的目的是使用蓝牙传输数据给Android上位机,便于用户进行查看*/

#include <SoftwareSerial.h>      //开启软串口
SoftwareSerial mySerial(2 , 3); //第2= RX,第3= TX   //开启软串口

mySerial.begin(115200);   //  设置虚拟串口波特率

mySerial.write(ppm);
mySerial.write(ppm);
mySerial.write(Temperature); 
mySerial.write(Temperature);

 

2.电路图:

 

3.串口输出:

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/677976
推荐阅读
相关标签
  

闽ICP备14008679号