前提・実現したいこと
Arduinoで使うためのTDSセンサーをESP8266(単体)で使いたい。
※TDSセンサーとは水の中に溶けている不純物の濃度を調べるものです。
発生している問題・エラーメッセージ
Ardoinoのアナログ入力は0-5Vの入力電圧を0-1023の整数値に変換しているようですが、 ESP8266のアナログ入力の入力電圧は0-1Vなので、センサーの測定結果がおかしくなってしまいます。
該当のソースコード
c言語
1/*************************************************** 2 DFRobot Gravity: Analog TDS Sensor / Meter For Arduino 3 <https://www.dfrobot.com/wiki/index.php/Gravity:_Analog_TDS_Sensor_/_Meter_For_Arduino_SKU:_SEN0244> 4 5 Created 2017-8-22 6 By Jason <jason.ling@dfrobot.com@dfrobot.com> 7 8 GNU Lesser General Public License. 9 See <http://www.gnu.org/licenses/> for details. 10 All above must be included in any redistribution 11 12 /***********Notice and Trouble shooting*************** 13 1. This code is tested on Arduino Uno and Leonardo with Arduino IDE 1.0.5 r2 and 1.8.2. 14 2. More details, please click this link: <https://www.dfrobot.com/wiki/index.php/Gravity:_Analog_TDS_Sensor_/_Meter_For_Arduino_SKU:_SEN0244> 15 ****************************************************/ 16 17#define TdsSensorPin A0 18#define VREF 5.0 // analog reference voltage(Volt) of the ADC 19#define SCOUNT 30 // sum of sample point 20int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC 21int analogBufferTemp[SCOUNT]; 22int analogBufferIndex = 0,copyIndex = 0; 23float averageVoltage = 0,tdsValue = 0,temperature = 25; 24 25void setup() 26{ 27 Serial.begin(115200); 28 pinMode(TdsSensorPin,INPUT); 29} 30 31void loop() 32{ 33 static unsigned long analogSampleTimepoint = millis(); 34 if(millis()-analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC 35 { 36 analogSampleTimepoint = millis(); 37 analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer 38 analogBufferIndex++; 39 if(analogBufferIndex == SCOUNT) 40 analogBufferIndex = 0; 41 } 42 static unsigned long printTimepoint = millis(); 43 if(millis()-printTimepoint > 800U) 44 { 45 printTimepoint = millis(); 46 for(copyIndex=0;copyIndex<SCOUNT;copyIndex++) 47 analogBufferTemp[copyIndex]= analogBuffer[copyIndex]; 48 averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value 49 float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0)); 50 float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensation 51 tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5; //convert voltage value to tds value 52 //Serial.print("voltage:"); 53 //Serial.print(averageVoltage,2); 54 //Serial.print("V "); 55 Serial.print("TDS Value:"); 56 Serial.print(tdsValue,0); 57 Serial.println("ppm"); 58 } 59} 60int getMedianNum(int bArray[], int iFilterLen) 61{ 62 int bTab[iFilterLen]; 63 for (byte i = 0; i<iFilterLen; i++) 64 bTab[i] = bArray[i]; 65 int i, j, bTemp; 66 for (j = 0; j < iFilterLen - 1; j++) 67 { 68 for (i = 0; i < iFilterLen - j - 1; i++) 69 { 70 if (bTab[i] > bTab[i + 1]) 71 { 72 bTemp = bTab[i]; 73 bTab[i] = bTab[i + 1]; 74 bTab[i + 1] = bTemp; 75 } 76 } 77 } 78 if ((iFilterLen & 1) > 0) 79 bTemp = bTab[(iFilterLen - 1) / 2]; 80 else 81 bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2; 82 return bTemp; 83}
試したこと
・サンプルコードではアナログ入力ピンはA1となっていましたが、ESP8266のアナログ入力ピンは1つしかなく、A0(TOUT)というピンを使いました。
・センサーの電源を3.3Vにして#define VREF 3.3にして試しました。
・センサーの電源を5.0Vにして#define VREF 5.0にして試しました。
補足情報(FW/ツールのバージョンなど)
このアナログTDSセンサーを使っています。
https://www.dfrobot.com/product-1662.html
前提・実現したいこと
ESP8266、チューブモーター、リレースイッチ、Blynkというアプリを使って、スマホから水質のモニタリングや遠隔操作による水質調整をしようとしています。
発生している問題・エラーメッセージ
使おうとしているアナログTDSセンサーがArduino UNOでは正常に作動するが、そのセンサーをESP8266で使おうとしても数値がおかしくなる。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/25 03:36