# pH sensor circuit v1.1 arduino ide code

**URL:** <https://forum.arduino.cc/t/ph-sensor-circuit-v1-1-arduino-ide-code/550118>\
**Category:** Programming\
**Created:** [October 9, 2018, 1:21pm UTC](https://forum.arduino.cc/t/ph-sensor-circuit-v1-1-arduino-ide-code/550118 "2018-10-09T13:21:00Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [October 9, 2018, 8:07pm UTC](https://forum.arduino.cc/t/ph-sensor-circuit-v1-1-arduino-ide-code/550118/3 "2018-10-09T20:07:31Z")

</div>

If anyone wants to help, the instructions for the DFRobotics "pH meter V1.1" (SKU: SEN0161) are here:  
[http://wiki.dfrobot.com.cn/index.php/(SKU:SEN0161)pH\_meter\_V1.1\_pH%E8%AE%A1V1.1](http://wiki.dfrobot.com.cn/index.php/(SKU:SEN0161)pH_meter_V1.1_pH%E8%AE%A1V1.1)

The example sketch is on that page. It compiles fine for UNO and MEGA 2560 so I don't know what the problem is.

```cpp
/*
  # This sample code is used to test the pH meter V1.1.
  # Editor : YouYou
  # Ver    : 1.1
  # DAT    : 2014.06.23
  # Product: analog pH meter V1.1
  # SKU    : SEN0161
*/
#define SensorPin A2            //pH meter Analog output to Arduino Analog Input 2
#define Offset 0.00            //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex = 0;
void setup(void)
{
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("pH meter experiment!");    //Test the serial monitor
}
void loop(void)
{
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue, voltage;
  if (millis() - samplingTime > samplingInterval)
  {
    pHArray[pHArrayIndex++] = analogRead(SensorPin);
    if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;
    voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024 + 0.05;
    pHValue = 3.5 * voltage + Offset;
    samplingTime = millis();
  }
  if (millis() - printTime > printInterval)  //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  {
    Serial.print("Voltage:");
    Serial.print(voltage, 2);
    Serial.print("    pH value: ");
    Serial.println(pHValue, 2);
    digitalWrite(LED, digitalRead(LED) ^ 1);
    printTime = millis();
  }
}

double avergearray(int* arr, int number)
{
  int i;
  int max, min;
  double avg;
  long amount = 0;
  if (number <= 0)
  {
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  }
  if (number < 5) //less than 5, calculated directly statistics
  {
    for (i = 0; i < number; i++)
    {
      amount += arr[i];
    }
    avg = amount / number;
    return avg;
  }
  else
  {
    if (arr[0] < arr[1])
    {
      min = arr[0]; max = arr[1];
    }
    else
    {
      min = arr[1]; max = arr[0];
    }
    for (i = 2; i < number; i++)
    {
      if (arr[i] < min)
      {
        amount += min;      //arr<min
        min = arr[i];
      }
      else
      {
        if (arr[i] > max)
        {
          amount += max;  //arr>max
          max = arr[i];
        }
        else
        {
          amount += arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount / (number - 2);
  }//if
  return avg;
}

```

---

_[View the full topic](https://forum.arduino.cc/t/ph-sensor-circuit-v1-1-arduino-ide-code/550118)._
