Smart Plant Care! Build a Qwiic Soil Moisture Sensor System
Contributors:
Chowdah
Code
The code for this project is really simple, just download the Arduino libraries for the products used and past the code below into the terminal! Make sure to adjust the moisture values to suit your plant's needs. The values used below seem to work pretty well for my money tree, which is a pretty low-fuss plant.
#include <Wire.h>
#include <SparkFun_Qwiic_OLED.h>
#include <res/qw_fnt_5x7.h>
QwiicMicroOLED myOLED;
#define COMMAND_GET_VALUE 0x05
const uint8_t qwiicAddress = 0x28;
uint16_t ADC_VALUE = 0;
const int dryValue = 1023;
const int wetValue = 400;
void setup() {
Serial.begin(115200);
Wire.begin();
while (myOLED.begin() == false) {
Serial.println("OLED not connected, check wiring!");
delay(1000);
}
testForConnectivity();
}
void loop() {
int moistureValue = getMoisture();
displayMoisture(moistureValue);
delay(1000);
}
int getMoisture() {
get_value();
return map(ADC_VALUE, dryValue, wetValue, 0, 100);
}
void get_value() {
Wire.beginTransmission(qwiicAddress);
Wire.write(COMMAND_GET_VALUE);
Wire.endTransmission();
Wire.requestFrom((uint8_t)qwiicAddress, (uint8_t)2);
if (Wire.available() >= 2) {
uint8_t ADC_VALUE_L = Wire.read();
uint8_t ADC_VALUE_H = Wire.read();
ADC_VALUE = (ADC_VALUE_H << 8) | ADC_VALUE_L;
}
}
void displayMoisture(int moistureValue) {
myOLED.erase();
if (moistureValue < 30) {
// Happy Face when well-watered
myOLED.line(20, 0, 30, 0); // Left eye
myOLED.line(40, 0, 50, 0); // Right eye
// Smile: \__/
myOLED.line(20, 20, 30, 10); // Left smile
myOLED.line(30, 10, 40, 10); // Bottom of smile
myOLED.line(40, 10, 50, 20); // Right smile
} else if (moistureValue >= 50 && moistureValue < 70) {
// Neutral Face
myOLED.line(20, 0, 30, 0); // Left eye
myOLED.line(40, 0, 50, 0); // Right eye
myOLED.line(20, 20, 50, 20); // Neutral line
} else {
// Sad Face when dry
myOLED.line(20, 0, 30, 0); // Left eye
myOLED.line(40, 0, 50, 0); // Right eye
// Sad mouth: /--\
myOLED.line(20, 10, 30, 20); // Left sad
myOLED.line(30, 20, 40, 20); // Bottom of sad
myOLED.line(40, 20, 50, 10); // Right sad
}
myOLED.display();
Serial.print("Moisture: ");
Serial.print(moistureValue);
Serial.println("%");
}
void testForConnectivity() {
Wire.beginTransmission(qwiicAddress);
if (Wire.endTransmission() != 0) {
Serial.println("Check connections. No soil sensor found.");
while (1);
}
}