Sewage treatment plant

A microcontroller project to be used in wastewater treatment plant for uses like irrigation and purification

IntermediateFull instructions provided12 hours287
Sewage treatment plant

Things used in this project

Hardware components

DIYables water level sensor
×1
Resistor 1k ohm
Resistor 1k ohm
×6
LED (generic)
LED (generic)
×3
Adafruit Waterproof DS18B20 Digital temperature sensor
Adafruit Waterproof DS18B20 Digital temperature sensor
×1
Gravity: Analog Spear Tip pH Sensor / Meter Kit
DFRobot Gravity: Analog Spear Tip pH Sensor / Meter Kit
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Arduino UNO
Arduino UNO
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Tinkercad
Autodesk Tinkercad
ArduiTooth
Serial Bluetooth Terminal

Story

Read more

Schematics

Temperature sensor Circuit

Water level sensor Schematic view

Water level Sensor ( simulated circuit )

Ultrasonic was used to apply the functionality of the water level sensor module in tinker cad since the module can't be found in tinkercad

Micro servo motor

Bluetooth Connection

To create a wireless connection between the board and Bluetooth-enabled devices.

Code

Temperature sensor code

Arduino
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Celsius = 0;
float Fahrenheit = 0;
void setup() {
  //BITHAL
  sensors.begin();
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(9600);
	pinMode(sensorPower, OUTPUT);
	digitalWrite(sensorPower, LOW);
	// Set LED pins as an OUTPUT
	pinMode(redLED, OUTPUT);
	pinMode(yellowLED, OUTPUT);
	pinMode(greenLED, OUTPUT);
}
void loop() {
  sensors.requestTemperatures();
  Celsius = sensors.getTempCByIndex(0);
  Fahrenheit = sensors.toFahrenheit(Celsius);
  Serial.print(Celsius);
  Serial.print(" C  ");
  Serial.print(Fahrenheit);
  Serial.println(" F");
  if (Celsius <= 35 && Celsius >= 20) {
    digitalWrite(5, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  if (Celsius < 37 && Celsius > 35) {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
  }
  if (Celsius < 20 && Celsius > 17) {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
  }
  if (Celsius <= 100 && Celsius > 37) {
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(3, HIGH);
  }
  if (Celsius < 17 && Celsius >= 0) {
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(3, HIGH);
  }
  delay(1000);
}

Water level Sensor

Arduino
A code that was used to program the water level sensor module
/* Water level Sensor code
/* Calibration values */
int lowerThreshold = 420;
int upperThreshold = 520;

// Sensor pins
#define sensorPower 9
#define sensorPin A0


// Value for storing water level
int val = 0;

// Declare pins to which LEDs are connected
int redLED = 6;
int yellowLED = 7;
int greenLED = 8;

void setup() {
	Serial.begin(9600);
	pinMode(sensorPower, OUTPUT);
	digitalWrite(sensorPower, LOW);
	
	// Set LED pins as an OUTPUT
	pinMode(redLED, OUTPUT);
	pinMode(yellowLED, OUTPUT);
	pinMode(greenLED, OUTPUT);

	// Initially turn off all LEDs
	digitalWrite(redLED, LOW);
	digitalWrite(yellowLED, LOW);
	digitalWrite(greenLED, LOW);
}

void loop() {
	int level = readSensor();

	if (level == 0) {
		Serial.println("Water Level: Empty");
		digitalWrite(redLED, LOW);
		digitalWrite(yellowLED, LOW);
		digitalWrite(greenLED, LOW);
	}
	else if (level > 0 && level <= lowerThreshold) {
		Serial.println("Water Level: Low");
		digitalWrite(redLED, HIGH);
		digitalWrite(yellowLED, LOW);
		digitalWrite(greenLED, LOW);

    }
	}
	else if (level > lowerThreshold && level <= upperThreshold) {
		Serial.println("Water Level: Medium");
		digitalWrite(redLED, LOW);
		digitalWrite(yellowLED, HIGH);
		digitalWrite(greenLED, LOW);
	}
	else if (level > upperThreshold) {
		Serial.println("Water Level: High");
		digitalWrite(redLED, LOW);
		digitalWrite(yellowLED, LOW);
		digitalWrite(greenLED, HIGH);
    
	}
	delay(1000);
}

//This is a function used to get the reading
int readSensor() {
	digitalWrite(sensorPower, HIGH);
	delay(10);
	val = analogRead(sensorPin);
	digitalWrite(sensorPower, LOW);
	return val;
}

Micro servo motor

Arduino
#include <Servo.h>

int servoPin = 11;
Servo servo;
int angle = 0;  // servo position in degrees

void setup() {
    servo.attach(servoPin);
}

void loop() {

    // scan from 0 to 180 degrees
    for(angle = 0; angle < 180; angle++) {
        servo.write(angle);
        delay(15);
    }
    
    // now scan back from 180 to 0 degrees
    for(angle = 180; angle > 0; angle--) {
        servo.write(angle);
        delay(15);
    }
}

PH sensor

Arduino
#define SensorPin A2            //pH meter Analog output to Arduino Analog Input 0
#define Offset 15.76      //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
#define uart  Serial
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex = 0;
void setup(void)
{
  pinMode(LED, OUTPUT);
  uart.begin(9600);
  uart.println("pH meter experiment!");    //Test the uart monitor
}
void loop(void)
{
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue, voltage;
  if (millis() - samplingTime > samplingInterval)
  {
    pHArray[pHArrayIndex++] = analogRead(SensorPin);
    if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;
    voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;
    pHValue = -5.882 * voltage + Offset;
    samplingTime = millis();
  }
  if (millis() - printTime > printInterval)  //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  {
    uart.print("Voltage:");
    uart.print(voltage, 2);
    uart.print("    pH value: ");
    uart.println(pHValue, 2);
    digitalWrite(LED, digitalRead(LED) ^ 1);
    printTime = millis();
  }
}
double avergearray(int* arr, int number) {
  int i;
  int max, min;
  double avg;
  long amount = 0;
  if (number <= 0) {
    uart.println("Error number for the array to avraging!/n");
    return 0;
  }
  if (number < 5) { //less than 5, calculated directly statistics
    for (i = 0; i < number; i++) {
      amount += arr[i];
    }
    avg = amount / number;
    return avg;
  } else {
    if (arr[0] < arr[1]) {
      min = arr[0]; max = arr[1];
    }
    else {
      min = arr[1]; max = arr[0];
    }
    for (i = 2; i < number; i++) {
      if (arr[i] < min) {
        amount += min;      //arr<min
        min = arr[i];
      } else {
        if (arr[i] > max) {
          amount += max;  //arr>max
          max = arr[i];
        } else {
          amount += arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount / (number - 2);
  }//if
  return avg;
}

Bluetooth module

Arduino
The component is used to enable wireless communication between the Arduino board and any Bluetooth-enabled device. It operates on the Bluetooth 2.0 specification and supports both the Bluetooth Serial Port Profile (SPP) and the Bluetooth Master-Slave configuration. It enables the establishment of a reliable and convenient wireless connection between devices.
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

char w;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  delay(1000); //wait to start the device properly
}

void loop() {
  if (mySerial.available()) {
    w = mySerial.read();
    Serial.println(w);  //PC
    delay(10);
    //commands with Serial.println(); show on pc serial monitor
  }

  if (Serial.available()) {
    w = Serial.read();
    mySerial.println(w); //Phone
    delay(10);
    //commands with mySerial.println(); show on the device app
  }

//shown on pc
Serial.println("This is a test run");
Serial.println("Congratulations! Device Connected!");
Serial.println();

//shown on device app
mySerial.println("This is a test run");
mySerial.println("Congratulations! Device Connected!");
mySerial.println();

delay(2000);
}

The integrated complete code ( Group )

Arduino
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#define ONE_WIRE_BUS 2
#define sensorPower 9
#define sensorPin A0
#define SensorPin A2            //pH meter Analog output to Arduino Analog Input 0
#define Offset 15.76      //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
#define uart  Serial
OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);
SoftwareSerial mySerial(10, 11); // RX, TX
char w;
float Celsius = 0;
float Fahrenheit = 0;
/* Water level Sensor code
/* Change these values based on your calibration values */
int lowerThreshold = 420;
int upperThreshold = 520;
// Value for storing water level
int val = 0;
int ran=0;
int ok =0;
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex = 0;
Servo servo;
int angle = 0;  // servo position in degrees

// Declare pins to which LEDs are connected
int redLED = 6;
int yellowLED = 7;
int greenLED = 8;

//Aref
int servoPin = 11;

void setup() {
  pinMode(LED, OUTPUT);
  uart.begin(9600);
  uart.println("pH meter experiment!");
//Aref
  servo.attach(servoPin);

//Abdullah

	Serial.begin(9600);
	pinMode(sensorPower, OUTPUT);
	digitalWrite(sensorPower, LOW);
	
	// Set LED pins as an OUTPUT
	pinMode(redLED, OUTPUT);
	pinMode(yellowLED, OUTPUT);
	pinMode(greenLED, OUTPUT);

	// Initially turn off all LEDs
	digitalWrite(redLED, LOW);
	digitalWrite(yellowLED, LOW);
	digitalWrite(greenLED, LOW);
  sensors.begin();
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
  delay(1000); //wait to start the device properly
}

void loop() {
	int level = readSensor();
  sensors.requestTemperatures();
  Celsius = sensors.getTempCByIndex(0);
  Fahrenheit = sensors.toFahrenheit(Celsius);
  Serial.print(Celsius);
  Serial.print(" C  ");
  Serial.print(Fahrenheit);
  Serial.println(" F");
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue, voltage;
	if (level == 0) {
		Serial.println("Water Level: Empty");
		digitalWrite(redLED, LOW);
		digitalWrite(yellowLED, LOW);
		digitalWrite(greenLED, LOW);
	}
	else if (level > 0 && level <= lowerThreshold) {
		Serial.println("Water Level: Low");
		digitalWrite(redLED, HIGH);
		digitalWrite(yellowLED, LOW);
		digitalWrite(greenLED, LOW);
    if (ran ==0){
	  	for(angle = 0; angle < 180; angle++) {
        		servo.write(angle);
        		delay(15);
		}
    ran+=1;
    ok=0;
    }
	}
	else if (level > lowerThreshold && level <= upperThreshold) {
		Serial.println("Water Level: Medium");
		digitalWrite(redLED, LOW);
		digitalWrite(yellowLED, HIGH);
		digitalWrite(greenLED, LOW);
  
	}
	else if (level > upperThreshold) {
		Serial.println("Water Level: High");
		digitalWrite(redLED, LOW);
		digitalWrite(yellowLED, LOW);
		digitalWrite(greenLED, HIGH);
    if(ok==0){
      for(angle = 180; angle > 0; angle--) {
              servo.write(angle);
              delay(15);
		}
    ok+=1;
    ran =0;
    }
	}
  if (Celsius <= 35 && Celsius >= 20) {
    digitalWrite(5, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  if (Celsius <= 37 && Celsius > 35) {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
  }
  if (Celsius < 20 && Celsius >= 17) {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
  }
  if (Celsius <= 100 && Celsius > 37) {
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(3, HIGH);
  }
  if (Celsius < 17 && Celsius >= 0) {
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(3, HIGH);
  }
	delay(1000);
  if (millis() - samplingTime > samplingInterval)
  {
    pHArray[pHArrayIndex++] = analogRead(SensorPin);
    if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;
    voltage = avergearray(pHArray, ArrayLenth) * 4.48 / 1024;
    pHValue = -5.882 * voltage + Offset;
    samplingTime = millis();
  }
  if (millis() - printTime > printInterval)  //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  {
    uart.print("Voltage:");
    uart.print(voltage, 2);
    uart.print("    pH value: ");
    uart.println(pHValue, 2);
    digitalWrite(LED, digitalRead(LED) ^ 1);
    printTime = millis();
  }
  if (mySerial.available()) {
    w = mySerial.read();
    Serial.println(w);  //PC
    delay(10);
    //commands with Serial.println(); show on pc serial monitor
  }

  if (Serial.available()) {
    w = Serial.read();
    mySerial.println(w); //Phone
    delay(10);
    //commands with mySerial.println(); show on the device app
  }

//shown on pc
Serial.println("This is a test run");
Serial.println("Congratulations! Device Connected!");
Serial.println();

//shown on device app
mySerial.println("This is a test run");
mySerial.println("Congratulations! Device Connected!");
mySerial.println();

delay(2000);
}

//This is a function used to get the reading
int readSensor() {
	digitalWrite(sensorPower, HIGH);
	delay(10);
	val = analogRead(sensorPin);
	digitalWrite(sensorPower, LOW);
	return val;
}
double avergearray(int* arr, int number) {
  int i;
  int max, min;
  double avg;
  long amount = 0;
  if (number <= 0) {
    uart.println("Error number for the array to avraging!/n");
    return 0;
  }
  if (number < 5) { //less than 5, calculated directly statistics
    for (i = 0; i < number; i++) {
      amount += arr[i];
    }
    avg = amount / number;
    return avg;
  } else {
    if (arr[0] < arr[1]) {
      min = arr[0]; max = arr[1];
    }
    else {
      min = arr[1]; max = arr[0];
    }
    for (i = 2; i < number; i++) {
      if (arr[i] < min) {
        amount += min;      //arr<min
        min = arr[i];
      } else {
        if (arr[i] > max) {
          amount += max;  //arr>max
          max = arr[i];
        } else {
          amount += arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount / (number - 2);
  }//if
  return avg;
}

Credits

Abdullah Alsoufi Ali Alsoufi

Abdullah Alsoufi Ali Alsoufi

1 project • 0 followers
Ivy Nabirye Wayero Mungati

Ivy Nabirye Wayero Mungati

1 project • 0 followers
AREF MOHAMMED ALWAN ALI

AREF MOHAMMED ALWAN ALI

1 project • 0 followers
joy massouh

joy massouh

1 project • 0 followers
NARENDRAN

NARENDRAN

20 projects • 22 followers
BITHAL PATTNAIK

BITHAL PATTNAIK

0 projects • 0 followers
Thanks to Passion Tech KLM.

Comments