#include <Wire.h> // Library to communication I2C
#include <Adafruit_GFX.h> //Library to control the OLED display
#include <Adafruit_SSD1306.h> // Library to OLED SSD1306
#include <OneWire.h> //Library to communication with to the water temperature sensor DS18B20
#include <DallasTemperature.h> //Lybrary to control the water temperature sensor DS18B20
//Start the configurations of sensors and actuator
OneWire ourWire(4); //Indicate the pin 4 to ESP32 for reading of sensor DS18B20
DallasTemperature DS18B20(&ourWire); //Decalre the variable or object for the sensor.
Adafruit_SSD1306 display(128, 64, &Wire);
int sensorPin = 34; // Pin of ESP32 for reading of the turbidity sensor.
int NTU; //Variable to save the turbidity
float temp; //Variable to save the water temperatura
void setup()
{
Serial. begin(115200); // Start the serial monitor and the respective baudios.
Wire.begin(); //Start the communication I2C to the OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Start the OLED and the direction 0x3C
display.clearDisplay(); // Clear the OLED
DS18B20.begin(); //Start the water sensor DS18B20
}
void loop(){
Readturbidity (); //Go to the function that read the turbidity
displayturbidity (); // Go to the fuction that show the NTU on the OLED
ReadTempWater (); // Go to the function that read the water temperature
displayTempWater (); // Go to the function that show the water temperature on the OLED
}
void Readturbidity ()
{
int reading = analogRead(sensorPin); // Save the analog value from the turbidity sensor
float voltage = reading * (3.3 / 4095.0); // Scale the digital value to voltage 3.3V with the 12bits resolution
// NTU(V)≈343.2V2−1446.2V+1523.6 This it the polynomial that represents the turbidity in the water
NTU = (343.2*(voltage*voltage) - 1446.2*voltage + 1523.6); // Make the polynomial replacing the voltage
if (NTU < 0) NTU = 0; // Avoid the negatives values
// Show in the setrial monitor
Serial.print("Pin Reading: "); Serial.print(reading);
Serial.print(" Voltage: "); Serial.print(voltage);
Serial.print(" V NTU: "); Serial.print(NTU); Serial.print("\n");
delay (2000);
}
void displayturbidity ()
{
display.clearDisplay();
display.setTextSize(1,2);
display.setTextColor(WHITE);
display.setCursor(34,2);
display.println("Turbidity");
display.setTextSize(3);
display.setCursor(29,20);
display.println(NTU);
display.setTextSize(3);
display.setCursor(32,44);
display.println("NTU");
display.display();
delay(3000);
}
void ReadTempWater ()
{
DS18B20.requestTemperatures(); //Send the command to read the temperature
temp= DS18B20.getTempCByIndex(0); //Obtain the temperature in ºC
Serial.print("Temperature= ");
Serial.print(temp);
Serial.println(" °C");
delay(1000);
}
void displayTempWater ()
{
display.clearDisplay();
display.setTextSize(1,2);
display.setTextColor(WHITE);
display.setCursor(34,2);
display.println("Temperature");
display.setTextSize(3);
display.setCursor(21,20);
display.println(temp);
display.setTextSize(1);
display.setCursor(0,44);
display.print((char)247); // degree symbol
display.println("C Degree Celcius");
display.display();
delay(3000);
}
No hay comentarios:
Publicar un comentario