#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display width and height, in pixels
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Data wire for DS18B20 is connected to GPIO 4
#define ONE_WIRE_BUS 4
// Analog pin for IR sensor (simulated as TDS sensor)
const int tdsSensorPin = 35;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize I2C communication with specified SDA and SCL pins
Wire.begin(32, 33);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the buffer
display.clearDisplay();
// Set text size and color
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Display static text
display.setCursor(0, 10); // Start at top-left corner
display.println(F("Hello, World!"));
// Update the display with the new data
display.display();
// Start up the DS18B20 library
sensors.begin();
}
void loop() {
// Request temperature readings
sensors.requestTemperatures();
// Fetch temperature in Celsius
float temperatureC = sensors.getTempCByIndex(0);
// Read the analog value from the IR sensor (simulated as TDS sensor)
int analogValue = analogRead(tdsSensorPin);
// Convert the analog value to voltage (ESP32 has 12-bit ADC, 0-4095 range)
float voltage = analogValue * (3.3 / 4095.0);
// Convert voltage to TDS (Total Dissolved Solids)
// This conversion formula will be a placeholder; in a real TDS sensor, you would use a specific conversion
float tdsValue = voltage * 500.0; // Example conversion factor
// Calculate electrical conductivity (EC)
// EC is often directly related to TDS, a common approximation: EC (uS/cm) = TDS (ppm) * 2
float ecValue = tdsValue * 2;
// Display temperature and EC on Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print(" °C, ");
Serial.print("EC: ");
Serial.print(ecValue);
Serial.println(" uS/cm");
// Display temperature and EC on the OLED display
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println(F("Hello, World!"));
display.setCursor(0, 20);
display.print(F("Temp: "));
display.print(temperatureC);
display.println(F(" C"));
display.setCursor(0, 40);
display.print(F("EC: "));
display.print(ecValue);
display.println(F(" uS/cm"));
display.display();
// Wait for a bit before the next reading
delay(1000);
}