Example Code for Arduino-Read Turbidity Data
Introduction
This example demonstrates how to read and print the current turbidity value via serial port on a Uno R3.
Hardware Preparation
- DFRduino UNO R3 (or similar) × 1
- Turbidity Sensor × 1
- [Gravity: 4Pin Sensor Adapter] (https://www.dfrobot.com/product-1626.html)× 1
- Micro USB Cable × 1
- Gravity-4P Cable (PH2.0 to 2.54 DuPont female) × 1
Software Preparation
Wiring Diagram
The sensor communicates with MCU via TTL and transmits the data through serial port after detecting different turbidity data.
Black=GND Blue=TX Red=VCC White=RX
Recommended connection diagram


Here we connect the sensor to the UNO board using Gravity: 4Pin Sensor Adapter and Gravity-4P Cable (PH2.0 to 2.54 DuPont female). The connections are as shown in the table below.
| Sensor | Adapter | UNO |
|---|---|---|
| GND | GND | GND |
| VCC | VOUT | VCC |
| RX | IO1 | 11(TX) |
| TX | IO2 | 12(RX) |
Other Preparation Work


The two probes should be oppositely fixed on a transparent container.
Sample Code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); // RX, TX
unsigned char str[5] = { }; //串口接收数据
unsigned char col;
unsigned int distance = 0;
unsigned char a[5] = {
0x18,0x05, 0x00, 0x01 ,0x0D
};
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
mySerial.write(a, 5);
while (!mySerial.available());
while (mySerial.available() > 0) //检测是否有串口数据
{
for (int i = 0; i < 5; i++)
{
str[i]=mySerial.read();
delay(5);
}
Serial.println(str[3],DEC);
mySerial.flush();
}
delay(500);
}
Result
This example demonstrates how to read and print the current turbidity value via serial port on a Uno R3.
Was this article helpful?
