Issue with turbidity and pH level readings

Good day comrades!

I and a few friends are currently constructing a simple Arduino mechanism to determine the turbidity and pH levels of particular liquids, but thus far we have encountered a singular, glaring issue—the turbidity values discerned are always wrong. For instance, when used on clean water, whose turbidity value should only be between 0 and 2), the LED display shows negative 40. These values don’t show any orderly pattern as well, and are random, with the only constant being their negativity (-35, -76, &c.). Could this be an issue with the sensor (DFRobot Analog Turbidity Sensor) itself or with our code? The code file is linked below. We would appreciate it very much if someone could review our codes and tell us what the problem is. Many thanks!

P. S. Our pH levels are erroneous as well, though in a much more “consistent” manner, if you will. The detected values always get a deduction of 40, no matter what they are. 7 becomes -33, 10 becomes -30, and so on.

Turbidity Meter Code
Turbidity Meter Code cont.


pH Meter Code (2)

@coolcreper257

Welcome.

Please post code, in code tags </>, not pictures of code. Please read the advice below, especially the 'how to get the best out of this forum' link.

Thank you.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

  • Your OS and version can be valuable information, please include it along with extra security you are using.
  • Always list the version of the IDE you are using and the board version if applicable.
  • Use quote or add error messages as an attachment NOT a picture.
  • How to insert an image into your post. ( Thanks @Robin2 )
  • Add your sketch where applicable but please use CODE TAGS ( </> )
  • Add a SCHEMATIC were needed even if it is hand drawn
  • Add working links to any specific hardware as needed (NOT links to similar items)
  • Remember that the people trying to help cannot see your problem so give as much information as you can

COMMON ISSUES

  • Ensure you have FULLY inserted the USB cables.
  • Check you have a COMMON GROUND where required. ( Thanks @Perry)
  • Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.
  • Try other computers where possible.
  • Try other USB leads where possible.
  • You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI
  • There may be a problem with the board check or remove your wiring first.
  • Remove any items connected to pins 0 and 1.

COMPUTER RELATED

  • Close any other serial programs before opening the IDE.
  • Ensure you turn off any additional security / antivirus just to test.
  • There may be a problem with the PC try RESTARTING it.
  • You may be selecting the wrong COM port.
  • Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.
  • Clear your browsers CACHE.
  • Close the IDE before using any other serial programs.
  • Preferably install IDE’s as ADMINISTRATOR or your OS equivalent

ARDUINO SPECIFIC BOARDS

  • CH340/341 based clones do not report useful information to the “get board info” button.
  • NANO (Old Types) some require you to use the OLD BOOTLOADER option.
  • NANO (ALL Types) See the specific sections lower in the forum.
  • NANO (NEW Types) Install your board CORE’s.
  • Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.
  • Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

  • On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini
  • On Linux ~/ArduinoCreateAgent-1.1/config.ini
  • On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.
Language problem ?
Try a language closer to your native language:

Thanks to all those who helped and added to this list.
[/quote]

Hi,
Welcome to the forum.
From the relationship graph.
_relationship_diagram

We need to see your code to check your algorithm.

From post #1, I would have thought you would have code that just outputs the raw data and the calculated value.
I cannot see anywhere in your code the use of the equation in the above graph.

Please post your code in code tags as advised by @PerryBebbington

A schematic of your project will also help.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Hey everyone!

Here are the codes we used for our prototypes:

Turbidity Meter

[code]
/*
   Displays text sent over the serial port (e.g. from the Serial Monitor) on
   an attached LCD.
   YWROBOT
  Compatible with the Arduino IDE 1.0
  Library version:1.1
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);
int sensorPin = A0;

void setup()
{
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{
  float sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  float turbidity = map(sensorValue, 0, 640, 100, 0);
  delay(100);
  lcd.setCursor(0, 0);
  lcd.print("Turbidity:");
  lcd.print(turbidity);
  lcd.setCursor(10, 0);
  delay(100);
  if (turbidity < 15)
  {
    lcd.setCursor(0, 1);
    lcd.print(" CLEAR ");
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  else if ((turbidity > 15) && (turbidity < 25))
  {
    lcd.setCursor(0, 1);
    lcd.print(" FAIRLY TURBID ");
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  }
  else if ((turbidity > 25) && (turbidity < 35))
  {
    lcd.setCursor(0, 1);
    lcd.print(" RATHER TURBID ");
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  }
  else if ((turbidity > 34) && (turbidity < 50))
  {
    lcd.setCursor(0, 1);
    lcd.print(" TURBID ");
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH)  ;
    digitalWrite(4, HIGH);
  }
  else
  {
    lcd.setCursor(0, 1);
    lcd.print(" VERY TURBID ");
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
  }
}
[/code]

pH Meter

[code]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
float calibration_value = 21.34;
int phval = 0; 
unsigned long int avgval; 
int buffer_arr[10],temp;

float ph_act;
// for the OLED display

 
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)


void setup() 
{
  Wire.begin();
 Serial.begin(9600);
  lcd.init(); 
  lcd.begin(16, 2);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("   pH Meter      ");
  lcd.setCursor(0, 1);
  lcd.print(" pH Meter    ");
  delay(2000);
  lcd.clear();
}
void loop() {
 for(int i=0;i<10;i++) 
 { 
 buffer_arr[i]=analogRead(A0);
 delay(30);
 }
 for(int i=0;i<9;i++)
 {
 for(int j=i+1;j<10;j++)
 {
 if(buffer_arr[i]>buffer_arr[j])
 {
 temp=buffer_arr[i];
 buffer_arr[i]=buffer_arr[j];
 buffer_arr[j]=temp;
 }
 }
 }
 avgval=0;
 for(int i=2;i<8;i++)
 avgval+=buffer_arr[i];
 float volt=(float)avgval*5.0/1024/6;
 float ph_act = (-5.70 * volt + calibration_value) + 10.8;
 lcd.setCursor(0, 0);
 lcd.print("pH Val:");
 lcd.setCursor(8, 0);
 lcd.print(ph_act);
 delay(1000);
 if ((ph_act) < 7)
 {
    lcd.setCursor(0, 1);
    lcd.print(" ACIDIC ");
 }
 else if ((ph_act) = 7)
 {
    lcd.setCursor(0, 1);
    lcd.print(" NEUTRAL ");
 }
 else if ((ph_act) > 7)
 {
    lcd.setCursor(0, 1);
    lcd.print(" ALKALINE ");
 }
}
[/code]

Here are our schematic diagrams:

Turbidity Meter
269802302_472862164187926_6340046396433957053_n

ph Meter
269930224_1377498689361005_2767256268286574434_n

Hi,

You assume turbidity has a linear relationship to the output voltage of the sensor.
The map function also works in integer variables, not floating.
The graph I posted in post #3 show the response and the equation of the graph.

What units of turbidity are you working in?
With the turbidity code, what value does your IDE monitor show of the raw analog input?

Tom... :grinning: :+1: :coffee: :australia:

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