#include <Adafruit_SSD1306.h>
// Initialize OLED display object
Adafruit_SSD1306 display(128, 64, &Wire);
void setup() {
Wire.begin(); // Initialize I2C communication
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize OLED display with I2C address 0x3C
display.clearDisplay(); // Clear the display buffer
pinMode(A0, INPUT); // Define A0 pin as input for soil moisture sensor
pinMode(7, OUTPUT); // Define pin 7 as output for controlling an LED (assuming you want to indicate soil moisture status)
}
void loop() {
// Clear previous display content
display.clearDisplay();
// Display "Soil moisture value: " text on OLED display
display.setTextSize(2); // Set text size
display.setTextColor(SSD1306_WHITE); // Set text color
display.setCursor(0, 0); // Set cursor position
display.println("Soil moisture value:");
// Read analog value from A0 pin (assuming it's connected to the soil moisture sensor)
int soilMoisture = analogRead(A0);
// Display soil moisture value on OLED display
display.setTextSize(2); // Larger text size for the value
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 16); // Set cursor position for the value
display.println(soilMoisture);
// Update OLED display
display.display();
// Check soil moisture level
if (soilMoisture < 250) {
digitalWrite(7, LOW); // Turn on LED or perform any other action
} else {
digitalWrite(7, HIGH); // Turn off LED or perform any other action
}
delay(1000); // Delay for one second (adjust as necessary)
}
No comments:
Post a Comment