0% found this document useful (0 votes)
118 views3 pages

Print Page - Turbidity Sensor Coding

The document discusses coding for a turbidity sensor using an Arduino. It provides code to read the analog output of the sensor, calibrate it by storing a clear water reading in EEPROM, and calculate turbidity as a percentage based on the sensor output voltage. The code displays the sensor readings and calculations on the serial monitor and an LCD.

Uploaded by

Subhasis Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views3 pages

Print Page - Turbidity Sensor Coding

The document discusses coding for a turbidity sensor using an Arduino. It provides code to read the analog output of the sensor, calibrate it by storing a clear water reading in EEPROM, and calculate turbidity as a percentage based on the sensor output voltage. The code displays the sensor readings and calculations on the serial monitor and an LCD.

Uploaded by

Subhasis Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/18/2018 Print Page - Turbidity sensor coding

Arduino Forum
Using Arduino => Programming Questions => Topic started by: robywhite on May 06, 2016, 05:17
am

Title: Turbidity sensor coding


Post by: robywhite on May 06, 2016, 05:17 am

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

Title: Re: Turbidity sensor coding


Post by: Koepel on May 06, 2016, 06:01 am

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.

Title: Re: Turbidity sensor coding


Post by: zhomeslice on May 06, 2016, 06:09 am

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

Code: [Select]

/*
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
}

Title: Re: Turbidity sensor coding


Post by: mcarmona on Oct 20, 2016, 07:15 pm

Hi,

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

https://forum.arduino.cc/index.php?action=printpage;topic=398900.0 1/3
9/18/2018 Print Page - Turbidity sensor coding

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)
https://forum.arduino.cc/index.php?action=printpage;topic=398900.0 2/3
9/18/2018 Print Page - Turbidity sensor coding

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
}

SMF 2.1 Beta 1 © 2014, Simple Machines

https://forum.arduino.cc/index.php?action=printpage;topic=398900.0 3/3

You might also like