Dissolved oxygen is reading negative values

  
#include <Arduino.h>
#include "DFRobot_LCD.h"
#include <Wire.h>
#include "WiFi.h"
#include "ThingSpeak.h"


// DO SENSOR-----------------------------------------------------------------/
#define DO_PIN A1
#define VREF 5000 // VREF(mv) 
#define ADC_RES 1024 //ADC Resolution/

#define SINGLE_POINT_CALIBRATION 0
#define READ_TEMP (25)

#define CAL1_V (131) //mv  CHANGE AFTER CALIBRATION
#define CAL1_T (25)   //℃

#define CAL2_V (1300) //mv  CHANGE AFTER CALIBRATION
#define CAL2_T (15)   //℃

const uint16_t DO_Table[41] = {
  14460, 14220, 13820, 13440, 13090, 12740, 12420, 12110, 11810, 11530,
  11260, 11010, 10770, 10530, 10300, 10080, 9860, 9660, 9460, 9270,
  9080, 8900, 8730, 8570, 8410, 8250, 8110, 7960, 7820, 7690,
  7560, 7430, 7300, 7180, 7070, 6950, 6840, 6730, 6630, 6530, 6410};

uint8_t Temperaturet;
uint16_t ADC_Raw;
uint16_t ADC_Voltage;
uint16_t DO;

int16_t readDO(uint32_t voltage_mv, uint8_t temperature_c)
{
  #if SINGLE_POINT_CALIBRATION == 00
    uint16_t V_saturation = (uint32_t)CAL1_V + (uint32_t)35 * temperature_c - (uint32_t)CAL1_T * 35;
    return (voltage_mv * DO_Table[temperature_c] / V_saturation);
  #else
    uint16_t V_saturation = (int16_t)((int8_t)temperature_c - CAL2_T) * ((uint16_t)CAL1_V - CAL2_V) / ((uint8_t)CAL1_T - CAL2_T) + CAL2_V;
    return (voltage_mv * DO_Table[temperature_c] / V_saturation);
  #endif
}

// pH SENSOR---------------------------------------/
DFRobot_LCD lcd(16, 2); //16 characters and 2 lines of show

#define PH_PIN 25 
float voltage,phvalue,temperature = 25;

float acidVoltage = 2000; //buffer solution at 4.o 
float neutralVoltage = 1390;   //buffer solution at 7.o 

const int Sensor_MINvalue1 = 6.5; // +-0.5 from ph 4.0
const int Sensor_MAXvalue1 = 7.5;



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


    // LCD Begin //
  lcd.init();
  lcd.setCursor(4,0);
  lcd.print("Welcome");
  delay(1000);
  lcd.clear();

  lcd.setCursor(3,0);
  lcd.print("Reading ");
  lcd.setCursor(3,1);
  lcd.print("pH & DO");
  delay(1000);
  lcd.clear();

  lcd.setCursor(3,0);
  lcd.print("Please Wait");
  delay(1000);
  lcd.clear();

}



void loop()
{
  //pH SENSOR
    static unsigned long timepoint = millis();

    if(millis()-timepoint>1000U){
      timepoint = millis();// timepoint = read temperature 

      voltage = analogRead (PH_PIN)/4095.0*3300; 

      float slope = (7.0-4.0)/((neutralVoltage-1500)/30 - (acidVoltage-1500)/3.0);
      float intercept = 7.0 - slope*(neutralVoltage-1500)/3.0;

      phvalue = slope*(voltage-1500)/3.0 + intercept;   // y=k*x + b [formula]

      if( phvalue < Sensor_MAXvalue1 && phvalue > Sensor_MINvalue1 ){
         Serial.print("Voltage:");
         Serial.print(voltage,1);
         Serial.print("PH:");
         Serial.println(phvalue,2);

          //LCD setting 16x2
         lcd.setCursor(0,0);
         lcd.print("PH: ");
        
         lcd.print(phvalue,2);

         lcd.setCursor(0,1);
         lcd.print("Volt: ");
         lcd.print(voltage/1000,2);} 
      else {
        lcd.init();
        lcd.setCursor(0,0);
        lcd.print("DOSING REQUIRED");
        delay(1000);
        lcd.clear();    
    }
    Temperaturet = (uint8_t)READ_TEMP;
    ADC_Raw = analogRead(DO_PIN);
    ADC_Voltage = uint32_t(VREF) * ADC_Raw / ADC_RES;

    Serial.print("Temperaturet:\t" + String(Temperaturet) + "\t");
    Serial.print("ADC RAW:\t" + String(ADC_Raw) + "\t");
    Serial.print("ADC Voltage:\t" + String(ADC_Voltage) + "\t");
    Serial.println("DO:\t" + String(readDO(ADC_Voltage, Temperaturet)) + "\t"); 

    //LCD setting 16x2
    lcd.setCursor(9,0);
    lcd.print((readDO(ADC_Voltage, Temperaturet))/1000);
    lcd.print("mg/L");
    delay(5000);
    lcd.clear();    
}
return;
} 

  

Hello everyone, the code above has been uploaded before but it was about a different question, upon check I now face a new issue my DO sensor is reading a negative value, I'm not sure if the code is wrong or I have copied it wrongly as I used this person as an example for the code

this is the reference i used 'Analog Dissolved Oxygen Sensor Tutorial | DIY Arduino D.O. Meter - YouTube'

Installation and Troubleshooting is for Problems with the Arduino IDE itself NOT your project. It says so in the description of the section. Therefore I have moved your post here.

You might want to look at this How to get the best from this from this Forum before you proceed any further.

Could be a wiring or hardware problem, too. Please post the relevant details and explain what you have done to solve the problem.

I have placed the do at pin A1 as was showed in the tutorial, the connection is correct I have tried to calibrate it but I cant seem to get it to have a stable voltage when calibrating could that be the issue?

this is the code for calibration

#include <Arduino.h>

#define VREF 5000//VREF(mv)
#define ADC_RES 1024 //ADC Resolution

uint32_t raw;
void setup()
{ 
  Serial.begin(115200);
 }

void loop()
{
  raw=analogRead(A1);
  Serial.println("raw:\t"+String(raw)+"\tVoltage(mv)"+String(raw*VREF/ADC_RES));
  delay(1000);
}

Did you connect all the grounds?

yup i connected the ground to ground and vcc to vcc

Please post close up, focused photo of your setup.

Unstable values suggest an unstable or broken (floating) connection. Use your multimeter to check continuity, and if necessary, redo the sensor wiring.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.