const int turbidityPin = A0; // Analog pin for turbidity sensor
void setup() {
Serial.begin(9600); // Initialize Serial Monitor
}
void loop() {
int sensorValue = analogRead(turbidityPin); // Read analog value from turbidity sensor
float voltage = sensorValue * (5.0 / 1024.0); // Convert analog value to voltage
float turbidity = mapTurbidity(voltage); // Map voltage to turbidity value
Serial.print("Turbidity: ");
Serial.print(turbidity);
Serial.println(" NTU");
delay(5); // Delay before next reading
}
float mapTurbidity(float voltage) {
// Map voltage to turbidity value based on calibration data
// Replace these values with your sensor's calibration data
float turbidity = -1120.4 * sq(voltage) + 5742.3 * voltage - 4353.8;
// Ensure turbidity value is within a certain range
if (turbidity < 0) {
turbidity = 0;
} else if (turbidity > 3000) {
turbidity = 3000;
}
return turbidity;
}