Turbidity sensor coding

How to write turbidity sensor coding for arduino??can someone help me..

They are 12 euros at AliExpress.
G = GND
A = analog output
D = digital output, the switching point is set by the potentionmeter.
V = 5V, 30mA

Use a 5V arduino board, and read the analog signal. Try the sensor in clear and dirty water and see what values you get for the analog value.

hook you Analog output form the sensor to Arduingo A0 and Ground to G provide power, open up a serial monitor at 9600 Baud and you should be able to see what is being sent with this code:
output should read somewhere between 0 ~ 1024 on the serial display

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

Hi,

I am using one of these sensors for teaching purposes, and I considered that:

  1. The output voltage (Vout) of the sensor is inversely proportional to turbidity, so,
  2. The clear water have a 0% turbidity, and
  3. Turbidity of 100% means a fully opaque object,

So, I proposed the following equation for turbidity (in relative percent units):

Turbidity = 100 - (Vout/Vclear)*100

I also incorporated a push button to calibrate de sensor with clear water, in order to get Vclear (output voltage for clear water). I also used EEPROM library to store and recover the last calibration Vclear value in ROM.

Here is the full sketch:

#include <EEPROM.h> // to store last calibration value (blanco, Vclear)
#include <Wire.h> // for LCD display (with I2S interphase)
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3, POSITIVE);

int sensor = 0; // variable for averaging
int n = 25; // number of samples to average
int sensorValue = 0;
float voltage = 0.00;
float turbidity = 0.00;
float Vclear = 2.85; // Output voltage to calibrate (with clear water).

int buttoncalib = 2; // The pin location of the push sensor for calibration. Connected to Ground and
// pin D2.

int pushcalib = 1; // variable for pin D2 status.

void setup()
{
Serial.begin(9600);
pinMode(buttoncalib, INPUT_PULLUP); //initializes digital pin 2 as an input with pull-up resistance.

// LCD display
lcd.begin (16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Turbidity Sensor");

//Serial display
Serial.println("Hi... welcome to your turbidity sensor");

EEPROM.get(0, Vclear); // recovers the last Vclear calibration value stored in ROM.
delay(3000); // Pause for 3 seg
}

void loop()
{
pushcalib = digitalRead(2); // push button status

if (pushcalib == HIGH) {
// If the push button is not pushed, do the normal sensing routine:
for (int i=0; i < n; i++){
sensor += analogRead(A1); // read the input on analog pin 1 (turbidity sensor analog output)
delay(10);
}
sensorValue = sensor / n; // average the n values
voltage = sensorValue * (5.000 / 1023.000); // Convert analog (0-1023) to voltage (0 - 5V)

turbidity = 100.00 - (voltage / Vclear) * 100.00; // as relative percentage; 0% = clear water;

EEPROM.put(0, Vclear); // guarda el voltaje de calibración actualmente en uso.

// Serial display
Serial.print(sensorValue);
Serial.print(", ");
Serial.print(voltage,3);
Serial.print(", ");
Serial.print(turbidity,3);
Serial.print(", ");
Serial.println(Vclear,3);

// Display LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Volts=");
lcd.print(voltage,2);
lcd.setCursor(0,1);
lcd.print("Turbidity=");
lcd.print(turbidity,2);
lcd.print("%");

sensor = 0; // resets for averaging

} else {

// Calibration routine, when push button is pushed:

Serial.println("Put the sensor in clear water to calibrate...");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Calibrating...");
delay(2000);

for (int i=0; i < n; i++){
sensor += analogRead(A1); // read the input on analog pin 1:
delay(10);
}
sensorValue = sensor / n;
Vclear = sensorValue * (5.000 / 1023.000); // Converts analog (0-1023) to voltage (0-5V):
sensor = 0;
EEPROM.put(0, Vclear); // stores Vclear in ROM
delay(1000);
lcd.clear();
}
delay(1000); // Pause for 1 seconds. // sampling rate
}

Thanks mcarmona. The code works well and accurately!

mcarmona:

  1. The output voltage (Vout) of the sensor is inversely proportional to turbidity, so,
  2. The clear water have a 0% turbidity, and
  3. Turbidity of 100% means a fully opaque object,

Turbidity sensors measure scattered light, not transmitted light. These units are primarily for washing machines and dishwashers to sense when the rinse cycle is "complete".
Looks like the Chinese "TSW-20" is a clone of the Thermometrics TSW-10:
https://www.amphenol-sensors.com/en/component/edocman/218-thermometrics-turbidity-sensors-tsw-10-datasheet/viewdocument?Itemid=8483
One really odd thing: the schematic of the TSW-10 sensor in the datasheet shows that it has three pins, none of which is Ground?!? The schematic shows that Pin 1 is +5V, Pin 2 is the output, and Pin 3 is the cathode of the LED. Pin 2 has a 4.7kOhm resistor to Ground and Pin 3 has a 470 Ohm resistor to Ground.
My guess is that they drew the schematic wrong and that Pin 3 is Ground instead of the cathode of the LED.

I searched alot and found out the best turbidity sensor for arudino

It has sufficient documentation and sample code. Quick plug and play for my garden monitoring project.

Im happy! Thanks for all the info guys

mcarmona - A button for calibration is mentioned. What kind of button is used and how is it connected?

mcarmona:
Hi,

I am using one of these sensors for teaching purposes, and I considered that:

  1. The output voltage (Vout) of the sensor is inversely proportional to turbidity, so,
  2. The clear water have a 0% turbidity, and
  3. Turbidity of 100% means a fully opaque object,

So, I proposed the following equation for turbidity (in relative percent units):

Turbidity = 100 - (Vout/Vclear)*100

I also incorporated a push button to calibrate de sensor with clear water, in order to get Vclear (output voltage for clear water). I also used EEPROM library to store and recover the last calibration Vclear value in ROM.

Here is the full sketch:

#include <EEPROM.h> // to store last calibration value (blanco, Vclear)
#include <Wire.h> // for LCD display (with I2S interphase)
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3, POSITIVE);

int sensor = 0; // variable for averaging
int n = 25; // number of samples to average
int sensorValue = 0;
float voltage = 0.00;
float turbidity = 0.00;
float Vclear = 2.85; // Output voltage to calibrate (with clear water).

int buttoncalib = 2; // The pin location of the push sensor for calibration. Connected to Ground and
// pin D2.

int pushcalib = 1; // variable for pin D2 status.

void setup()
{
Serial.begin(9600);
pinMode(buttoncalib, INPUT_PULLUP); //initializes digital pin 2 as an input with pull-up resistance.

// LCD display
lcd.begin (16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Turbidity Sensor");

//Serial display
Serial.println("Hi... welcome to your turbidity sensor");

EEPROM.get(0, Vclear); // recovers the last Vclear calibration value stored in ROM.
delay(3000); // Pause for 3 seg
}

void loop()
{
pushcalib = digitalRead(2); // push button status

if (pushcalib == HIGH) {
// If the push button is not pushed, do the normal sensing routine:
for (int i=0; i < n; i++){
sensor += analogRead(A1); // read the input on analog pin 1 (turbidity sensor analog output)
delay(10);
}
sensorValue = sensor / n; // average the n values
voltage = sensorValue * (5.000 / 1023.000); // Convert analog (0-1023) to voltage (0 - 5V)

turbidity = 100.00 - (voltage / Vclear) * 100.00; // as relative percentage; 0% = clear water;

EEPROM.put(0, Vclear); // guarda el voltaje de calibración actualmente en uso.

// Serial display
Serial.print(sensorValue);
Serial.print(", ");
Serial.print(voltage,3);
Serial.print(", ");
Serial.print(turbidity,3);
Serial.print(", ");
Serial.println(Vclear,3);

// Display LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Volts=");
lcd.print(voltage,2);
lcd.setCursor(0,1);
lcd.print("Turbidity=");
lcd.print(turbidity,2);
lcd.print("%");

sensor = 0; // resets for averaging

} else {

// Calibration routine, when push button is pushed:

Serial.println("Put the sensor in clear water to calibrate...");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Calibrating...");
delay(2000);

for (int i=0; i < n; i++){
sensor += analogRead(A1); // read the input on analog pin 1:
delay(10);
}
sensorValue = sensor / n;
Vclear = sensorValue * (5.000 / 1023.000); // Converts analog (0-1023) to voltage (0-5V):
sensor = 0;
EEPROM.put(0, Vclear); // stores Vclear in ROM
delay(1000);
lcd.clear();
}
delay(1000); // Pause for 1 seconds. // sampling rate
}

Thanks a lot, can you also share the schematic of the system or just for the sensor without LCD display pls?