library for SEN0169 ph probe from DFRobot
Dependents: 2022_TICE_Electrolyse
Revision 0:f17189a32558, committed 2022-01-11
- Comitter:
- jsanchez
- Date:
- Tue Jan 11 15:01:35 2022 +0000
- Commit message:
- v.1.0
Changed in this revision
lib_SEN0169.cpp | Show annotated file Show diff for this revision Revisions of this file |
lib_SEN0169.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r f17189a32558 lib_SEN0169.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib_SEN0169.cpp Tue Jan 11 15:01:35 2022 +0000 @@ -0,0 +1,89 @@ +#include "lib_SEN0169.h" + + +SEN0169::SEN0169(PinName data_pin) : _datapin(data_pin) +{ + this->_phValue = 7.0f; + this->_voltage = 1500.0f; +} + +void SEN0169::calibration(uint8_t ph_tampon) +{ + float avg_voltage, buf_[20], temp_; + + for(int i=0; i<20; i++) { + buf_[i]=_datapin.read(); + ThisThread::sleep_for(10ms); + } + for(int i=0; i<19; i++) { + for(int j=i+1; j<20; j++) { + if(buf_[i]>buf_[j]) { + temp_=buf_[i]; + buf_[i]=buf_[j]; + buf_[j]=temp_; + } + } + } + avg_voltage = 0.0f; + for(int i=5; i<15; i++) avg_voltage += buf_[i]; + avg_voltage = avg_voltage*3.3/10.0f; + + + switch (ph_tampon) { + case 4 : + this->_params[ACIDE] = avg_voltage; + break; + + case 7 : + this->_params[NEUTRE] = avg_voltage; + break; + + case 10 : + this->_params[BASIC] = avg_voltage; + break; + + default : + break; + } +} + +float* SEN0169::get_parameters(void) +{ + return _params; +} + +float SEN0169::read_voltage(void) +{ + this->_voltage = _datapin.read()*3.3; + return _voltage; +} + +float SEN0169::read_ph(void) +{ + float avg_voltage, buf_[10], temp_; + + for(int i=0; i<10; i++) { // take 10 samples of voltage + buf_[i]=_datapin.read(); + ThisThread::sleep_for(10ms); + } + for(int i=0; i<9; i++) { // sort the voltages from small to large + for(int j=i+1; j<10; j++) { + if(buf_[i]>buf_[j]) { + temp_=buf_[i]; + buf_[i]=buf_[j]; + buf_[j]=temp_; + } + } + } + avg_voltage = 0.0f; + for(int i=2; i<8; i++) avg_voltage += buf_[i]; // supress the 2 smallest and 2 largest samples + avg_voltage = avg_voltage*3.3/6.0f; + + this->_voltage = avg_voltage; + this->_params[SLOPE] = ((3.0f*(4.0f*_params[ACIDE]+7.0f*_params[NEUTRE]+10.0f*_params[BASIC]))-(21.0f*(_params[ACIDE]+_params[NEUTRE]+_params[BASIC])))/54.0f; + // Slope = [3*sum(xy)-sum(x)*sum(y)]/[3*sum(x²)-sum(x)²] + this->_params[INTERCEPT] = ((_params[ACIDE] + _params[NEUTRE] + _params[BASIC])/3.0f) - (_params[SLOPE] * 7.0f); + this->_phValue = (avg_voltage*1000.0 - _params[INTERCEPT]) / _params[SLOPE]; + + return _phValue; +} \ No newline at end of file
diff -r 000000000000 -r f17189a32558 lib_SEN0169.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib_SEN0169.h Tue Jan 11 15:01:35 2022 +0000 @@ -0,0 +1,56 @@ +/** SEN0169 Class +* +* @purpose MbedOs library for Gravity : Analog pH Sensor / Meter Kit V2, SKU: SEN0161-V2 +* +* Example: +* @code +* +* #include "mbed.h" +* #include "lib_SEN0169.h" +* +* Serial pc(USBTX, USBRX, 230400); +* SEN0169 sensor_0(A0); +* +* int main() +* { +* pc.printf("2022_TEST_SEN0169\n\r"); +* pc.printf("pH = (voltage - intercept) / slope\n\r"); +* while (true) { +* ThisThread::sleep_for(1000); +* pc.printf("voltage = %.03f ; pH = %.01f\n\r", sensor_0.read_voltage(), sensor_0.read_ph()); +* float* parameters = sensor_0.get_parameters(); +* pc.printf("ph4_voltage = %.03f ; pH7_voltage = %.03f ; pH10_voltage = %.03f ; slope = %f ; intercept = %f\n\r", parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]); +* } +* } +* +* @endcode +* +* @file lib_SEN0169.h +* @date Jan 2022 +* @author Jérémie SANCHEZ +*/ + +#ifndef _SEN0169_H_ +#define _SEN0169_H_ + +#include "mbed.h" + +class SEN0169 +{ +public: + SEN0169(PinName data_pin); + ~SEN0169(); + void calibration(uint8_t ph_tampon); + float* get_parameters(void); + float read_voltage(void); + float read_ph(void); + +private: + AnalogIn _datapin; + float _phValue; + float _voltage; + typedef enum {ACIDE, NEUTRE, BASIC, SLOPE, INTERCEPT} enum_params; + float _params[5] = {2032.44f,1500.0f,967.56f,-177.48f,2742.36f}; +}; + +#endif