DFRobot pH Sensor

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.


void loop()

Greetings!

I would just like to ask for help with regards to the calibration of the pH Sensor I am using. The reference code that is available at the DFRobot Website works just fine if I were to emulate the same code and schematic diagram for the connections of the hardware. The pH sensor I am using is the analog pH sensor version 2.

Unfortunately, when I try to emulate the schematic diagram and code in order to work for ESP8266/NodeMCU, the serial monitors detect pH:nan or pH:inf when I calibrate the sensor via serial monitor, and I am running out of idea what causes the error in detection of the parameter I need.

The following is the original code (from DFRobot which is designed for Arduino UNO) and along with it is the modified code designed to work with NodeMCU.

THE ORIGINAL CODE:

#include "DFRobot_PH.h"
#include <EEPROM.h>

#define PH_PIN A1
float voltage,phValue,temperature = 25;
DFRobot_PH ph;

void setup()
{
    Serial.begin(115200);  
    ph.begin();
}

void loop()
{
    static unsigned long timepoint = millis();
    if(millis()-timepoint>1000U){                  //time interval: 1s
        timepoint = millis();
        //temperature = readTemperature();         // read your temperature sensor to execute temperature compensation
        voltage = analogRead(PH_PIN)/1024.0*5000;  // read the voltage
        phValue = ph.readPH(voltage,temperature);  // convert voltage to pH with temperature compensation
        Serial.print("temperature:");
        Serial.print(temperature,1);
        Serial.print("^C  pH:");
        Serial.println(phValue,2);
    }
    ph.calibration(voltage,temperature);           // calibration process by Serail CMD
}

THE MODIFIED VERSION:

#include "DFRobot_PH.h"
#include <EEPROM.h>
#include <Blynk.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";

#define PH_PIN A0
float voltage,phValue,temperature = 25;
DFRobot_PH ph;

void setup()
{
    Serial.begin(9600);  
    ph.begin();
    Blynk.begin(auth, ssid, pass);
}

void loop()
{
   Blynk.run();
    static unsigned long timepoint = millis();
    if(millis()-timepoint>1000U){                  //time interval: 1s
        timepoint = millis();
        //temperature = readTemperature();         // read your temperature sensor to execute temperature compensation
        voltage = analogRead(PH_PIN) * 3.3 / 1024.0;  // read the voltage
        phValue = ph.readPH(voltage,temperature);  // convert voltage to pH with temperature compensation
        Serial.print("temperature:");
        Serial.print(temperature,1);
        Serial.print("^C  pH:");
        Serial.println(phValue,2);

        Blynk.virtualWrite(V6, phValue);
    }
    ph.calibration(voltage,temperature);           // calibration process by Serail CMD
}

@Haruu Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your “Modified Version” code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Also, you’ve used the word “emulate” twice in your description of the issue. Does this mean that you aren’t running this code on any actual hardware, but are using some sort of hardware emulation software?

Pete.

@PeteKnight

Noted sir! I already edited the code (the modified version) and placed the code in between the ```. As for the word emulate, sorry to mislead you but what I mean by that is I tried testing the code with actual Arduino Uno Hardware and NodeMCU hardware.

It seems that the code posted by DFRobot for the pH Sensor is working fine and can be calibrated smoothly. But when I attempted to upload the modified version of the code to the NodeMCU, the sensor indicates either “pH:inf” or “pH:nan”.

The only necessary change I made from the original to modified version was the addition of the libraries, changing the reference voltage from 5V to 3.3V and changing the analog signal input from A1 to A0 since only A0 is the only one available in the NodeMCU.

My advice is always to start by adding some serial print commands to see what values are coming from your sensor before you start manipulating this data.

You do seem to be dong something odd here…

with 5v it was multiplied by 5000, not 5.0…

Also, why do you have this line of code…

and you are missing the firmware configuration code from your sketch…

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME         "Device"
#define BLYNK_AUTH_TOKEN            "YourAuthToken"

when you do this, it makes sense to replace this…

with this…

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

and remove this line…

its also helpful to add this…

#define BLYNK_PRINT Serial

near the top of your sketch, so that you get some useful info from your serial monitor.

The biggest issue from Blynk’s point of view is the cluttered void loop. You should use a timer to call the Ph code, not do millis comparisons in the void loop.
Read this for more info…

Pete.

@PeteKnight

I already made the changes as you have suggested, but still there were no changes in the serial monitor.
But thankfully with the help of the feedback send by the arduino program, I was able to make the necessary changes and ultimately arrived at the correct code.

The following is the code I used in order for the pH parameter to reflect in the Blynk Application:

#include <Blynk.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DFRobot_PH.h>

char auth[] = "BLYNK_AUTH_TOKEN";
char ssid[] = "SSID";
char pass[] = "Password of the WIFI connection";

const int phSensorPin = A0; // pH Sensor

DFRobot_PH phSensor; // declare the phSensor object

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
  // Read pH sensor value
  int pHValue = analogRead(phSensorPin);

  // Convert pH sensor value to pH level
  float voltage = analogRead(phSensorPin) * 3300 / 1024.0;
  float temperature = 25.0; // Replace with actual temperature
  float pHLevel = phSensor.readPH(voltage, temperature);

  Serial.print(F("pH:")); Serial.println(pHLevel);

  Blynk.virtualWrite(V6, pHLevel);

  // Calibrate pH sensor
  phSensor.calibration(voltage, temperature);

  delay(1000);
}

Once again, thank you for your help sir!

Clearly not, you have only made on of the changes I suggested, and ignored the rest, and made matters worse by adding a blocking delay in your void loop.

Your approach will result in problems with stability.

Pete.