PH-4502C sensor not working

Hi all,

To begin with im new to this forum and fairly new to Arduino :wink:

The last few weeks i created a system for my aquarium that measures and shows temperature from a DS18b20 sensor on a 2004A display which controls a relais (for aquarium heater) depending on the setpoint. Also it lights up a green or red led depending on the setpoint.

This works great and i am very happy about it :slight_smile:

Now i want to add a PH sensor to it all that will also show on the display the value.
When i set it all up and edited the sketch it shows up on the display but the value stays around -7 and it does not react to the 2 potmeters or the sensor in different fluids.

Unfortunatly i cannot find a good tutorial on the PH-4502C, this is the best i found and there i got the code from: https://www.botshop.co.za/how-to-use-a-ph-probe-and-sensor/

Can somebody point me in the right direction on this? Or let me know whats maybe wrong about my sketch?

// Program: DS18B20 temperature sensor
 
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define RELAY1  8  // Relay heating
#define RELAY2  10  // Relay heating

int red = 2; // red LED cold
int green = 9; // green LED warm
const int analogInPin = A0;
int sensorValue = 1; 
unsigned long int avgValue; 
float b;
int buf[10],temp;

// DS18B20 signal pin port
#define ONE_WIRE_BUS 3
 
// Defines an instance of oneWire to communicate with the sensor
OneWire oneWire(ONE_WIRE_BUS);
 
// Stores minimum and maximum temperatures
float tempMin = 999;
float tempMax = 0;
 
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1;
 
// Initializes the LCD
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
 
void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
  // Finds and displays sensor addresses
  Serial.println("Locating DS18B20 sensors...");
  Serial.print("Were found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" sensors.");
  if (!sensors.getAddress(sensor1, 0)) 
     Serial.println("Sensors not found !"); 
  // Shows the address of the sensor found on the bus
  Serial.print("Sensor address: ");
  show_sensor_address (sensor1);
  Serial.println();
  Serial.println();
  lcd.begin(20, 4);

pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);

    }
 
void show_sensor_address(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // Add zeros if necessary
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}
 
void loop()
{
  // Read sensor information
  sensors.requestTemperatures();
  float tempC = sensors.getTempC(sensor1);
  // Updates minimum and maximum temperatures
  if (tempC < tempMin)
  {
    tempMin = tempC;
  }
  if (tempC > tempMax)
  {
    tempMax = tempC;
  }
  // Shows data on the serial monitor
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Min : ");
  Serial.print(tempMin);
  Serial.print(" Max : ");
  Serial.println(tempMax);
   
  // Shows Temp data on the LCD  
    delay(3000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp.:       ");
  //Degree symbol
  lcd.write(223);
  lcd.print("C");
  lcd.setCursor(7,0);
  lcd.print(tempC);
  lcd.setCursor(0,1);
  lcd.print("L: ");
  lcd.setCursor(3,1);
  lcd.print(tempMin,2);
  lcd.setCursor(12,1);
  lcd.print("H: ");
  lcd.setCursor(15,1);
  lcd.print(tempMax,2);

   // read the input on analog pin 0:
 int sensorValue = analogRead(A0);
 // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
 float voltage = sensorValue * (5.0 / 1023.0);
 // print out the value you read:
 Serial.println(voltage);

  for(int i=0;i<10;i++) 
 { 
  buf[i]=analogRead(analogInPin);
  delay(10);
 }
 for(int i=0;i<9;i++)
 {
  for(int j=i+1;j<10;j++)
  {
   if(buf[i]>buf[j])
   {
    temp=buf[i];
    buf[i]=buf[j];
    buf[j]=temp;
   }
  }
 }
 avgValue=0;
 for(int i=2;i<8;i++)
 avgValue+=buf[i];
 float pHVol=(float)avgValue*5.0/1024/6;
 float phValue = -5.70 * pHVol + 21.34;
 Serial.print("sensor = ");
 Serial.println(phValue);
 
 delay(20);

   // Shows pH data on the LCD  
  lcd.setCursor(0,3);
  lcd.print("pH: ");
  lcd.setCursor(4,3);
  lcd.print(phValue,2);

  float temperature = sensors.getTempCByIndex(0);
    if(temperature > 29)
    {
digitalWrite(red,HIGH);
digitalWrite(green,LOW);

      lcd.setCursor(1,2);
      lcd.print("ALARM ALARM ALARM");

digitalWrite(RELAY1,1); 
digitalWrite(RELAY2,1);   
           }
    else if(temperature > 23)
    {
digitalWrite(red,LOW);
digitalWrite(green,HIGH);

      lcd.setCursor(0,2);
      lcd.print("Heating off");

digitalWrite(RELAY1,1);
digitalWrite(RELAY2,1);
           }
else {
digitalWrite(red,HIGH);
digitalWrite(green,LOW);

      lcd.setCursor(0,2);
      lcd.print("Heating on");
      
digitalWrite(RELAY1,0);
digitalWrite(RELAY2,0);
}
}

You forgot to include a link to the datasheet of your specific sensor.

Are you sure you don't have any other electrical connection to the water? Ground loops or other current loops are a common problem with pH sensors.

https://www.botshop.co.za/how-to-use-a-ph-probe-and-sensor/

There is not really a good datasheet for this sensor.

It is currently not in a aquarium. I have a external 5V 10A power supply on the breadboard where everything is connected to.

Thomassen85: Or let me know whats maybe wrong about my sketch?

I don't see a problem with your sketch. I just tested the part that reads the pH sensor and it works as I would expect it to when I use a potentiometer in place of the sensor. However, when you're having problems it's a very good idea to remove all unnecessary variables. Your sketch is far more complex than it needs to be to test your sensor. If the sensor outputs an analog value, then run this sketch on your Arduino with only the pH sensor attached:

//prints the analogRead value to Serial Monitor

int analogPin = A0;    // select the analog input pin

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(analogPin));
  delay(200);
}

Then open Serial Monitor and make sure it's set to 9600 baud.

Dip the sensor in solutions of different pH. Do you see a change in the values printed to Serial Monitor?

Ok i uploaded your sketch.

Nothing changes the values, even removing the ph sensor doesnt affect the values shown in the serial monitor. It shows 279/280

After uploading the first sketch again it shows a pH reading of 14.83 now instead of -7

So now you know for sure that it's not a software problem; it's a hardware problem.