/*
* ๐ฑ INTELLIGENT PLANT POT WITH TAMAGOTCHI EMOTIONS
* ๐ฏ Project: Smart plant monitoring system with emotional feedback
* ๐จโ๐ Target: High school students - STEM+ Education
* ๐ Concepts: Sensor integration, conditional logic, emotional AI
*/
// ๐ LIBRARY INCLUSIONS
#include <DHT.h> // ๐ก๏ธ For DHT11 temperature/humidity sensor
#include <Wire.h> // ๐ I2C communication protocol
#include <Adafruit_SSD1306.h> // ๐บ OLED display control
#include <Adafruit_GFX.h> // ๐จ Graphics functions for display
// ๐ง HARDWARE PIN CONFIGURATION
#define DHT_PIN 2 // ๐ก๏ธ Digital pin for DHT11 sensor
#define LDR_PIN A0 // โ๏ธ Analog pin for light sensor
#define SOIL_MOISTURE_PIN A1 // ๐ง Analog pin for soil moisture
#define LED_WATER 3 // ๐ด Red LED - irrigation needed
#define LED_LIGHT 4 // ๐ข Green LED - lighting needed
#define LED_FAN 5 // ๐ต Blue LED - ventilation needed
// ๐ SENSOR TYPE DEFINITION
#define DHT_TYPE DHT11 // ๐ก๏ธ Specify we're using DHT11 (not DHT22)
// ๐ฎ SENSOR OBJECT INITIALIZATION
DHT dht(DHT_PIN, DHT_TYPE); // ๐ก๏ธ Create DHT sensor object
// ๐บ OLED DISPLAY CONFIGURATION
Adafruit_SSD1306 display(128, 64, &Wire, -1); // ๐ฅ๏ธ 128x64 pixel display
// ๐พ VARIABLE DECLARATIONS
float temperature; // ๐ก๏ธ Stores temperature in Celsius
float airHumidity; // ๐จ Stores air humidity percentage
int soilMoisture; // ๐ง Stores soil moisture level
int lightIntensity; // โ๏ธ Stores light intensity level
void setup() {
// ๐ INITIALIZATION SEQUENCE
Serial.begin(9600); // ๐ Start serial communication for debugging
Serial.println("๐ฑ Smart Plant Pot Initializing...");
// ๐ SENSOR INITIALIZATION
dht.begin(); // ๐ก๏ธ Start DHT11 sensor
Serial.println("โ
DHT11 Sensor Ready");
// ๐ก LED PIN CONFIGURATION
pinMode(LED_WATER, OUTPUT); // ๐ด Water indicator LED
pinMode(LED_LIGHT, OUTPUT); // ๐ข Light indicator LED
pinMode(LED_FAN, OUTPUT); // ๐ต Fan indicator LED
Serial.println("โ
LED Outputs Configured");
// ๐ฅ๏ธ OLED DISPLAY INITIALIZATION
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("โ OLED Display Connection Failed!");
while(1); // ๐ Halt execution if display fails
}
Serial.println("โ
OLED Display Ready");
// ๐ฏ WELCOME MESSAGE
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Smart Plant Pot");
display.println("Initializing...");
display.println("๐ฑ Tamagotchi Mode");
display.display();
delay(2000); // โณ Show welcome message
Serial.println("๐ System Ready! Monitoring Plant Conditions...");
}
void loop() {
// ๐ MAIN PROGRAM LOOP - Runs continuously
// ๐ STEP 1: READ ALL SENSORS
readSensorData();
// ๐ญ STEP 2: ANALYZE PLANT EMOTION (Combining all 3 sensors)
String plantEmotion = analyzePlantEmotion(temperature, soilMoisture, lightIntensity);
// โก STEP 3: CONTROL OUTPUT DEVICES
controlOutputDevices(soilMoisture, lightIntensity, temperature);
// ๐บ STEP 4: UPDATE DISPLAY WITH CURRENT STATUS
updateDisplay(temperature, soilMoisture, lightIntensity, plantEmotion);
// โณ WAIT BEFORE NEXT READING
delay(3000); // ๐ Update every 3 seconds
}
/*
* ๐ FUNCTION: readSensorData()
* ๐ฏ PURPOSE: Read values from all three sensors
* ๐ง PROCESS:
* - Read temperature & humidity from DHT11
* - Read soil moisture from analog sensor
* - Read light intensity from LDR
* - Map raw values to meaningful ranges
*/
void readSensorData() {
// ๐ก๏ธ READ TEMPERATURE & AIR HUMIDITY
temperature = dht.readTemperature(); // โ๏ธ๐ฅ Get temperature in Celsius
airHumidity = dht.readHumidity(); // ๐จ Get air humidity percentage
// ๐ ๏ธ ERROR HANDLING FOR DHT SENSOR
if (isnan(temperature) || isnan(airHumidity)) {
Serial.println("โ Failed to read from DHT sensor!");
return; // ๐ Skip this reading cycle
}
// ๐ง READ SOIL MOISTURE
int soilRaw = analogRead(SOIL_MOISTURE_PIN); // ๐ Read raw analog value
soilMoisture = map(soilRaw, 0, 1023, 0, 100); // ๐บ๏ธ Convert to percentage
soilMoisture = 100 - soilMoisture; // ๐ Invert (0% = dry, 100% = wet)
// โ๏ธ READ LIGHT INTENSITY
int lightRaw = analogRead(LDR_PIN); // ๐ Read raw analog value
lightIntensity = map(lightRaw, 0, 1023, 0, 100); // ๐บ๏ธ Convert to percentage
// ๐ DEBUG OUTPUT
Serial.print("๐ก๏ธ Temp: "); Serial.print(temperature); Serial.print("ยฐC");
Serial.print(" | ๐ง Soil: "); Serial.print(soilMoisture); Serial.print("%");
Serial.print(" | โ๏ธ Light: "); Serial.print(lightIntensity); Serial.println("%");
}
/*
* ๐ญ FUNCTION: analyzePlantEmotion()
* ๐ฏ PURPOSE: Determine plant's emotional state using ALL THREE sensors
* ๐ LOGIC: Uses combined conditions from temperature, soil moisture, AND light
* ๐ก DESIGN: Each emotion requires specific combination of all three factors
*/
String analyzePlantEmotion(float temp, int soil, int light) {
/*
* ๐ HAPPY: Ideal conditions across all three parameters
* Conditions: Perfect temperature + Good moisture + Balanced light
*/
if (temp >= 20.0 && temp <= 26.0 &&
soil >= 40 && soil <= 70 &&
light >= 40 && light <= 80) {
return "HAPPY ๐"; // ๐ Perfect conditions!
}
/*
* ๐ก ANGRY: Extreme heat with dry soil and excessive light
* Conditions: Too hot + Very dry + Too bright
*/
if (temp > 32.0 && soil < 20 && light > 85) {
return "ANGRY ๐ก"; // ๐ฅ Hot, dry, and overwhelmed by light
}
/*
* ๐ช SAD: Unfavorable cold with dryness and darkness
* Conditions: Too cold + Dry + Too dark
*/
if (temp < 15.0 && soil < 25 && light < 25) {
return "SAD ๐ช"; // โ๏ธ Cold, thirsty, and in darkness
}
/*
* ๐ฅต STRESSED: Heat stress with overwatering and strong light
* Conditions: Very hot + Overwatered + Intense light
*/
if (temp > 30.0 && soil > 80 && light > 75) {
return "STRESSED ๐ฅต"; // ๐ฆ Hot, drowning, and light-stressed
}
/*
* ๐ฅฑ THIRSTY: Dry conditions with moderate temperature and light
* Conditions: Dry soil + Normal temp + Normal light
*/
if (soil < 20 && temp >= 18.0 && temp <= 28.0 && light >= 30 && light <= 70) {
return "THIRSTY ๐ฅฑ"; // ๐ง Specifically needs water
}
/*
* ๐จ COLD_STRESS: Low temperature with adequate moisture but poor light
* Conditions: Cold + Good moisture + Insufficient light
*/
if (temp < 12.0 && soil >= 40 && soil <= 80 && light < 40) {
return "COLD_STRESS ๐จ"; // โ๏ธ Chilly with other needs met
}
/*
* ๐ OVERHEATED: High temperature with reasonable moisture and light
* Conditions: Hot + Moderate moisture + Moderate light
*/
if (temp > 28.0 && soil >= 30 && soil <= 60 && light >= 50 && light <= 80) {
return "OVERHEATED ๐"; // ๐ฅ Needs cooling but otherwise okay
}
/*
* ๐ DARK_DEPRESSION: Extremely low light with cool temperature and dry soil
* Conditions: Very dark + Cool + Dry
*/
if (light < 15 && temp < 20.0 && soil < 30) {
return "DARK_DEPRESSION ๐"; // ๐ Multiple deficiencies
}
/*
* ๐ CRITICAL: Extreme conditions threatening plant survival
* Conditions: Dangerous temp + Critical moisture + Problematic light
*/
if ((temp > 38.0 || temp < 5.0) && (soil < 10 || soil > 95) && (light < 10 || light > 95)) {
return "CRITICAL ๐"; // ๐จ Emergency situation
}
/*
* ๐ CONTENT: Acceptable conditions across all parameters
* Conditions: Within tolerable ranges for all three factors
*/
if (temp >= 18.0 && temp <= 28.0 &&
soil >= 30 && soil <= 80 &&
light >= 30 && light <= 85) {
return "CONTENT ๐"; // ๐ Acceptable conditions
}
// ๐ญ DEFAULT: Conditions don't match any specific emotion
return "NEUTRAL ๐";
}
/*
* โก FUNCTION: controlOutputDevices()
* ๐ฏ PURPOSE: Control LEDs based on sensor readings
* ๐ก LOGIC: Independent control for each output device
*/
void controlOutputDevices(int soil, int light, float temp) {
// ๐ง WATERING CONTROL: Activate if soil is too dry
digitalWrite(LED_WATER, soil < 30); // ๐ด Red LED ON if soil dry
// ๐ก LIGHTING CONTROL: Activate if environment is too dark
digitalWrite(LED_LIGHT, light < 20); // ๐ข Green LED ON if dark
// ๐ช๏ธ VENTILATION CONTROL: Activate if temperature is too high
digitalWrite(LED_FAN, temp > 28.0); // ๐ต Blue LED ON if hot
// ๐ STATUS FEEDBACK
Serial.print("๐ก Outputs - ");
Serial.print("Water: "); Serial.print(soil < 30 ? "ON" : "OFF");
Serial.print(" | Light: "); Serial.print(light < 20 ? "ON" : "OFF");
Serial.print(" | Fan: "); Serial.println(temp > 28.0 ? "ON" : "OFF");
}
/*
* ๐บ FUNCTION: updateDisplay()
* ๐ฏ PURPOSE: Show sensor data and plant emotion on OLED
* ๐จ DESIGN: Clear, organized layout with visual appeal
*/
void updateDisplay(float temp, int soil, int light, String emotion) {
display.clearDisplay(); // ๐งน Clear previous content
display.setTextSize(1); // ๐ Set text size
display.setTextColor(SSD1306_WHITE); // โช White text on black background
// ๐ SENSOR DATA SECTION
display.setCursor(0, 0); // ๐ฏ Position cursor at top-left
display.println("PLANT MONITOR SYSTEM");
display.println("-------------------");
display.print("Temp: "); display.print(temp, 1); display.println(" C");
display.print("Soil: "); display.print(soil); display.println(" %");
display.print("Light: "); display.print(light); display.println(" %");
display.println("-------------------");
// ๐ญ EMOTION DISPLAY SECTION
display.setTextSize(1); // ๐ Slightly larger for emotion
display.setCursor(0, 40); // ๐ฏ Position for emotion display
display.print("EMOTION: ");
display.setTextSize(1); // ๐ Standard size for emotion text
display.println(emotion);
// ๐ก STATUS INDICATORS
display.setCursor(0, 55);
display.print("W:"); display.print(digitalRead(LED_WATER) ? "ON " : "OFF");
display.print(" L:"); display.print(digitalRead(LED_LIGHT) ? "ON " : "OFF");
display.print(" F:"); display.print(digitalRead(LED_FAN) ? "ON" : "OFF");
display.display(); // ๐ฅ๏ธ Update physical display
}