Turbidity Sensor Particle Sensor Project
The Turbidity Sensor Particle Sensor project showcases how to interface a turbidity sensor with an Arduino Mega to measure water clarity based on suspended particle levels. Turbidity sensors are crucial in environmental monitoring and water quality assessments.
How It Works
A turbidity sensor measures how cloudy a liquid is by shining infrared light through it and measuring how much reaches a detector on the other side. Suspended particles scatter and absorb that light, so the more turbid the water, the less light arrives. Output is conventionally expressed in NTU — Nephelometric Turbidity Units.
The common hobby module, typically sold as the SEN0189, places an IR LED and a phototransistor facing each other across a gap that sits in the liquid. Its analog output is inversely related to turbidity: clear water gives the highest voltage, around 4.1–4.2 V on a 5 V supply, and the voltage falls as cloudiness rises.
The Arduino Mega 2560 samples with a 10-bit ADC, so analogRead() returns 0–1023 across 5 V. The relationship between voltage and NTU is not linear; the manufacturer provides a quadratic fit, and for drinking-water work that curve matters. For "is this water getting dirtier" monitoring, the raw voltage trend is often enough.
Temperature affects the reading, and so does anything that coats the optical surfaces. Biofilm growth on the windows is the main long-term failure mode in real installations.
Components Needed
- Arduino Mega 2560
- Turbidity Sensor Module
- Jumper Wires
- Arduino Mega 2560
- Power Supply
Wiring to the Arduino Mega 2560
Connect the analog output to A0, VCC to 5 V and GND to ground. The output stays within the 5 V ADC range of this board, so it connects directly.
Only the probe end is waterproof. The small circuit board that comes with the sensor must stay dry — mount it above the waterline and route the cable so condensation cannot run down into it.
Calibrate against known samples. Record the voltage in clear water and again in water with a measured amount of a standard such as formazin, then interpolate. Without at least a clear-water reference the numbers mean very little.
| Module pin | Arduino Mega 2560 pin | Function |
|---|---|---|
| A (analog out) | A0 | Voltage — falls as turbidity rises |
| D (digital out) | D2 | Threshold trip, set by onboard pot |
| VCC | 5V | Supply — the board expects 5 V |
| GND | GND | Common ground |
Build and Upload
Open the Arduino IDE and create a new sketch.
Copy and paste the provided Arduino code into the sketch.
Upload the code to the Arduino Mega.
Open the Serial Monitor in the Arduino IDE (set to 9600 baud).
Observe the turbidity sensor readings displayed in the Serial Monitor.
Dip the turbidity sensor into water to measure changes in turbidity levels.
Example Code
Reading turbidity, converting to NTU and classifying water clarity. Upload it with the board set to Arduino Mega 2560 and open the Serial Monitor at 9600 baud.
const int TURBIDITY_PIN = A0;
const float VREF = 5.0;
const int ADC_MAX = 1023;
const float DIVIDER = 1.0;
float readVolts() {
long total = 0;
for (int i = 0; i < 30; i++) { total += analogRead(TURBIDITY_PIN); delay(5); }
return (total / 30.0) * VREF / ADC_MAX * DIVIDER;
}
void setup() { Serial.begin(9600); }
void loop() {
float v = readVolts();
// Manufacturer's quadratic fit, clamped at clear water
float ntu;
if (v > 4.2) ntu = 0;
else ntu = -1120.4 * v * v + 5742.3 * v - 4352.9;
if (ntu < 0) ntu = 0;
Serial.print("V=");
Serial.print(v, 2);
Serial.print(" NTU=");
Serial.print(ntu, 0);
Serial.print(" ");
if (ntu < 5) Serial.println("clear");
else if (ntu < 50) Serial.println("slightly cloudy");
else if (ntu < 200) Serial.println("cloudy");
else Serial.println("very turbid");
delay(1000);
}
Applications
A turbidity sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Drinking water quality monitoring and filter performance checks
- Washing machine and dishwasher load sensing, to decide rinse cycles
- Aquarium and pond clarity monitoring
- Wastewater and effluent discharge supervision
- Detecting sediment events in streams and boreholes
Working with the Arduino Mega 2560
The Arduino Mega 2560 is built around the ATmega2560 and runs on 5 V logic with 8 KB of SRAM and 256 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The Mega’s 8 KB of SRAM is four times the Uno’s, so larger buffers and lookup tables are practical.
I²C lives on D20/D21 rather than A4/A5 — wiring copied from an Uno tutorial will not work unchanged.
Four hardware UARTs mean a serial sensor can have its own port instead of fighting SoftwareSerial.
| Arduino Mega 2560 characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 5 V | Matches most hobby modules directly |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | A0–A15 (sixteen channels) | Determines how many analog sensors can share the board |
| PWM outputs | D2–D13 and D44–D46 | Needed for brightness, speed and tone control |
| I²C pins | D20 (SDA) and D21 (SCL) | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | D2, D3, D18, D19, D20 and D21 | Required for counting fast or asynchronous events |
| Serial | four independent hardware UARTs | Monitor runs at 9600 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- The voltage never changes — the probe is not actually submerged, or air bubbles are sitting on the optical windows.
- NTU comes out negative — the quadratic fit is being used outside its valid range; clamp at zero as the sketch does.
- Readings drift over weeks — biofilm is growing on the windows. Clean them; this is routine maintenance, not a fault.
- The value changes when the lights go on — stray light is reaching the detector. Shield the probe.
- The board died after a splash — only the probe is waterproof; the PCB is not.
- Code written for an ESP board gives odd analog values — the Arduino Mega 2560 uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
- An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Mega 2560 I²C is on D20 (SDA) and D21 (SCL).
Taking It Further on the Arduino Mega 2560
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Mega 2560 specifically:
The Mega’s sixteen analog inputs make it the right board when several of these sensors must run at once. Where an Uno would need an external multiplexer, the Mega simply reads A0 through A15 directly.
Four hardware UARTs mean a GPS, a serial display and a debug console can coexist without SoftwareSerial, which is unreliable above 38400 baud and blocks interrupts while it transmits.
With 8 KB of SRAM there is room to buffer readings and do real processing — a rolling average over several hundred samples, or holding a full display frame in memory, both of which are impractical on an Uno.
Notes and Practical Limits
Bubbles are the single biggest source of spurious readings. Mount the probe where flow is smooth and away from any point where water falls or splashes, and let readings settle before trusting them.
Turbidity tells you about suspended solids only. Water can be perfectly clear and still be unsafe, so never present an NTU figure as a potability measurement.