capacitive_soil_moisture_sensing
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
capacitive_soil_moisture_sensing [2019/06/14 10:49] – created 192.168.1.179 | capacitive_soil_moisture_sensing [2019/08/17 17:29] (current) – 192.168.1.250 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | + | ===== Capacitive Soil Moisture Sensing ===== | |
- | I picked up some inexpensive soil moisture monitors from Banggood. | + | I picked up some inexpensive soil moisture monitors from Banggood. |
The sketch and schematic is in the Arduino Projects directory at: | The sketch and schematic is in the Arduino Projects directory at: | ||
Line 7: | Line 7: | ||
</ | </ | ||
+ | |||
+ | The sample code is also here:\\ | ||
+ | < | ||
+ | / | ||
+ | This is an example for reading the analog sensor output of a | ||
+ | capacitive soil moisture probe and displaying the result on a | ||
+ | 128x64 OLED display over the I2C bus. | ||
+ | |||
+ | This example requires the SSD1306 display driver by Adafruit: | ||
+ | https:// | ||
+ | |||
+ | Video detailing the capacitive soil moisture measurement probe: | ||
+ | https:// | ||
+ | |||
+ | *********************************************************************/ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define OLED_RESET 4 | ||
+ | Adafruit_SSD1306 display(OLED_RESET); | ||
+ | |||
+ | // make sure the SSD1306 driver is configured for 64 line height | ||
+ | #if (SSD1306_LCDHEIGHT != 64) | ||
+ | # | ||
+ | #endif | ||
+ | |||
+ | const int wetProbe | ||
+ | const int dryProbe | ||
+ | |||
+ | |||
+ | // Analog Sensor Input Pin | ||
+ | int sensorInput | ||
+ | |||
+ | // Variables | ||
+ | int validSensorReading | ||
+ | int sensorReading; | ||
+ | int sensorResult; | ||
+ | |||
+ | |||
+ | void setup() | ||
+ | |||
+ | Serial.begin(9600); | ||
+ | |||
+ | display.begin(SSD1306_SWITCHCAPVCC, | ||
+ | display.clearDisplay(); | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | |||
+ | sensorReading = analogRead(sensorInput); | ||
+ | if (abs(validSensorReading - sensorReading) > 10) { | ||
+ | validSensorReading = sensorReading; | ||
+ | } | ||
+ | |||
+ | Serial.print ("Old Sensor Reading: "); | ||
+ | Serial.println (validSensorReading); | ||
+ | Serial.print ("New Sensor Reading: "); | ||
+ | Serial.println (sensorReading); | ||
+ | |||
+ | sensorResult = map(validSensorReading, | ||
+ | |||
+ | Serial.print (" | ||
+ | Serial.println (sensorResult); | ||
+ | Serial.println (); | ||
+ | |||
+ | |||
+ | // display the correct soil moisture level on the display | ||
+ | // lower voltages represent more wet levels | ||
+ | switch (sensorResult) { | ||
+ | case 0: | ||
+ | displayTextProbe(" | ||
+ | break; | ||
+ | case 1: | ||
+ | displayTextProbe(" | ||
+ | break; | ||
+ | case 2: | ||
+ | displayTextProbe(" | ||
+ | break; | ||
+ | case 3: | ||
+ | displayTextProbe(" | ||
+ | break; | ||
+ | case 4: // same as case 3, due to how map works. | ||
+ | displayTextProbe(" | ||
+ | break; | ||
+ | } | ||
+ | delay(500); | ||
+ | } | ||
+ | |||
+ | void displayTextProbe(const char * sensorDisplay) { | ||
+ | display.setTextSize(2); | ||
+ | display.setTextColor(WHITE); | ||
+ | display.setCursor(0, | ||
+ | display.clearDisplay(); | ||
+ | display.println(" | ||
+ | display.println(sensorDisplay); | ||
+ | display.display(); | ||
+ | delay(1); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | {{:: |
capacitive_soil_moisture_sensing.1560523767.txt.gz · Last modified: 2019/06/14 10:49 by 192.168.1.179