#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int soilPin = 34; // Analog pin connected to soil moisture sensor
const int brightnessPin = 34; // Analog pin connected to potentiometer
int brightness = 255; // Initial brightness value (0-255)
void setup() {
Serial.begin(9600);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Set up the brightness control
pinMode(brightnessPin, INPUT);
}
void loop() {
int soilValue = analogRead(soilPin);
float moisturePercent = map(soilValue, 0, 1023, 0, 100); // Convert analog reading to percentage
// Read brightness from potentiometer
brightness = analogRead(brightnessPin) / 4; // Map 0-1023 to 0-255
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Soil Moisture: ");
display.print(moisturePercent);
display.println("%");
display.display();
display.dim(brightness);
delay(1000); // Delay between readings
}