Skip to content

Soil Moisture Sensor Wireling Tutorial

This tutorial will show you how to acquire moisture readings from the Soil Moisture Sensor Wireling.


Technical Details

This Wireling features the ATtiny25, a high performance, low-power AVR 8-bit controller. Moisture is detected using capacitance to measure dielectric permittivity of the surrounding medium.

Technical Details

ATtiny25 specs

  • High performance, low-power AVR 8-bit microcontroller
  • Advanced RISC Architecture
  • Non-volatile program and data memories

TinyDuino Power Requirements

  • Voltage: 3.0V - 5.5V 
  • Current:
    • 300µA (Active Mode)
    • 0.1µA  (Power-down Mode)

Pins Used

  • A5/SCL - I2C Serial Clock line
  • A4/SDA - I2C Serial Data line

Dimensions

  • 10mm x 10mm (.394 inches x .394 inches)
  • Max Height (from the lower bottom of Wireling to upper top Wireling Connector): 3.70mm (0.15 inches)
  • Weight: 1 gram (.04 ounces)

Notes

  • You can also use this board without the TinyDuino – modifying the connecting cable by removing the opposite connector and stripping/tinning the wires, you can solder the connector cable to any other microcontroller board of your choice!

Materials

Processor Adapter Programming Cable
+
-
-
Arduino USB A to B Cable
Raspberry Pi

In order to interface with the Soil Moisture Sensor Wireling, you'll need a Wireling Cable and the Wireling.h Library (You can download this from GitHub as linked, or from the Library Manager in the Arduino IDE).


Hardware Assembly

For this example, all you need to do is use a Wireling cable to plug the Soil Moisture Wireling into Port 0 and upload the program. No additional libraries are required (aside from Wireling.h which is needed for every Wireling).


Software Setup

For this Wireling, you need the example Arduino sketch included below. Open up the program in the Arduino IDE, and upload it to your board.

Make the correct Tools selections for your development board. If unsure, you can double check the Help page that mentions the Tools selections needed for any TinyCircuits processor.


Upload Program

Upload the program to your development board of choice!

Code
/************************************************************************
 * Soil Moisture Sensor Wireling Example Sketch
 * This program uses four of the Wirelings included with the Starter Kit:
 * Port 0: Soil Moisture Sensor Wireling
 * 
 * This program will display the temperature and moisture detected by the
 * soil moisture sensor Wireling on the serial monitor.
 * 
 * Hardware by: TinyCircuits
 * Written by: Ben Rose and Hunter Hykes for TinyCircuits
 *
 * Initiated: 12/26/2019 
 * Updated: 01/09/2020
 ************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Wireling.h>

// Make compatible with all TinyCircuits processors
#if defined(ARDUINO_ARCH_AVR)
#define SerialMonitorInterface Serial
#elif defined(ARDUINO_ARCH_SAMD)
#define SerialMonitorInterface SerialUSB
#endif

/* * * * * * MOISTURE SENSOR * * * * * * */
#define MOISTURE_PORT 0 
#define MIN_CAP_READ 710 /* Toggle this to raw minimum value */
#define MAX_CAP_READ 975 /* Toggle this to raw maximum value */

#define ANALOG_READ_MAX 1023
#define THERMISTOR_NOMINAL 10000
#define TEMPERATURE_NOMINAL 25
#define B_COEFFICIENT 3380
#define SERIES_RESISTOR 35000

void setup() {
  Wire.begin();
  Wireling.begin();
  delay(10);
  Wireling.selectPort(MOISTURE_PORT);
  SerialMonitorInterface.begin(9600);
}

void loop() {
  SerialMonitorInterface.print("M: ");
  SerialMonitorInterface.print(readMoisture());
  SerialMonitorInterface.print("\tT: ");
  SerialMonitorInterface.println(readTemp());
  delay(50);
}

int readMoisture(){
  Wire.beginTransmission(0x30);
  Wire.write(1);
  Wire.endTransmission();
  delay(5);
  int c=0;
  Wire.requestFrom(0x30, 2);
  if(Wire.available()==2)
  { 
    c = Wire.read();
    c <<= 8;
    c |= Wire.read();
    c = constrain(c, MIN_CAP_READ, MAX_CAP_READ);
    c = map(c, MIN_CAP_READ, MAX_CAP_READ, 0, 100);
  }
  return c;
}

float readTemp() {
  Wire.beginTransmission(0x30);
  Wire.write(2);
  Wire.endTransmission();
  delay(5);
  int c = 0;
  Wire.requestFrom(0x30, 2);
  if (Wire.available() == 2)
  {
    c = Wire.read();
    c <<= 8;
    c |= Wire.read();
    //https://learn.adafruit.com/thermistor/using-a-thermistor thanks!
    uint32_t adcVal = ANALOG_READ_MAX - c;
    uint32_t resistance = (SERIES_RESISTOR * ANALOG_READ_MAX) / adcVal - SERIES_RESISTOR;
    float steinhart = (float)resistance / THERMISTOR_NOMINAL;     // (R/Ro)
    steinhart = log(steinhart);                  // ln(R/Ro)
    steinhart /= B_COEFFICIENT;                   // 1/B * ln(R/Ro)
    steinhart += 1.0 / (TEMPERATURE_NOMINAL + 273.15); // + (1/To)
    steinhart = 1.0 / steinhart;                 // Invert
    steinhart -= 273.15;                         // convert to C
    return steinhart;
  }
  return c;
}

To get some varied readings from the sensor, you can hold your finger across the sensor to simulate moist soil.

Moisture and Temperature values displayed on Serial Monitor

If the values seem off to your application, you can edit the min and max raw sensor reading values in order to get the perfect scale for your project using these lines at the top of the program:


#define MIN_CAP_READ 710 /* Toggle this to raw minimum value */
#define MAX_CAP_READ 975 /* Toggle this to raw maximum value */

Contact Us

If you have any questions or feedback, feel free to email us at info@tinycircuits.com or make a post on the forum.

Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it!

Thanks for making with us!


Downloads