Moisture Sensor

The moisture sensor in your Photon kit can measure the amount of moisture in soil or similar materials.

The moisture sensor has two gold-plated legs that you would insert into the soil. Moisture in the soil will allow electricity to conduct from one sensor leg to the other. More moisture allows more electricity to conduct. The sensor can indirectly measure the amount of moisture in the soil by measuring the the amount of electricity being conducted.

Your moisture sensor has an attached 3-pin terminal connector that you will use to attach 3 jumper wires (red, black, and yellow). The back of the sensor has labels for the 3 terminals: VCC, GND, SIG. Use a small flat-head screwdriver to slightly loosen each screw in the connector (turn counterclockwise to loosen – "lefty loosey"). Insert a red jumper wire into the connector opening for VCC. Then use the screwdriver to tighten that screw until the wire is firmly held in place (turn clockwise to tighten – "righty tighty"). Repeat to connect the black wire to GND and to connect the yellow wire to SIG.

Now you can plug the other end of the jumper wires directly into the Photon – or into a breadboard (and then use 3 more jumper wires to complete the connection to the Photon).

Moisture Sensor

Photon Pin

VCC (Red) - Power

any I/O pin

GND (Black) - Ground

GND

SIG (Yellow) - Data

any analog I/O pin

NOTE: The moisture sensor power wire (VCC) is being connected to a second I/O pin for power, instead of 3.3V or V-USB (5V). The reason is that your code should only supply power to the moisture sensor when you need to take a reading. After the reading, your code should turn the power off to the sensor. A constant flow of electricity across the sensor legs could cause them to corrode over time (because of natural salts and other minerals in the soil). The gold-plating on the legs resists corrosion, but it is still better to use your code to control when the sensor is actually powered.

IMPORTANT: The moisture sensor data wire (SIG) must connect to an analog I/O pin, such as: A0, A1, A2, A3, A4, A5.

Experiment 3 of the online SparkFun Photon Experiment Guide shows how to connect the moisture sensor directly to the Photon (but you could also connect it to a breadboard and then use additional jumper wires to connect to the Photon). Here is the connection diagram for Experiment 3:

Once the moisture sensor is connected to the Photon, you can insert the sensor legs into the soil where you need to take measurements.

Code for Moisture Sensor

Library

The moisture sensor does not require any special code library.

Global Variables

In the global variables, you should declare variables for the pins being used for the sensor power and sensor data. The example below declares variables called "moisturePower" and "moistureData" (but you could use a different variable name).

// if necessary, change data pin numbers to match your wiring
int moisturePower = D6;
int moistureData = A2;

You will probably also want to declare an integer variable to store the reading from the moisture sensor. This will make it easier to do something based on the measurement. The example below declares a variable called "moistureReading" (but you could use a different variable name).

// global variable
int moistureReading;

setup( ) function

Within the setup() function, you have to include a statement to set the pin mode for the moisture sensor power pin variable. You should also turn off the power to the moisture sensor when the device first starts.

pinMode(moisturePower, OUTPUT);
digitalWrite(moisturePower, LOW);

IMPORTANT: Do not set a pin mode for the moisture sensor data variable.

Measuring Amount of Moisture

Code for measuring the amount of moisture would be most likely placed within the loop() function or within a custom function.

Measuring the amount of moisture always takes four steps: (1) turn on the power to the sensor, (2) wait for a small time delay (to allow electricity to conduct through the soil), (3) read the measurement, and (4) turn off the power to the sensor.

digitalWrite(moisturePower, HIGH);
delay(100);
moistureReading = analogRead(moistureData);
digitalWrite(moisturePower, LOW);

// add code to do something based on value of moistureReading

The measurement will be a value ranging from 0-4095.

When there is less moisture detected, the reading will be lower. When there is more moisture detected, the reading will be higher.

You will need to add code to do something with the moisture reading (such as: display it on the OLED screen, send the data to your web app, turn on LED if the reading is less than a certain value, etc.).

TIP: You can test out the moisture sensor by holding both of the sensor legs between your thumb and index finger. The moisture in your skin will conduct electricity across the sensor legs. Don't worry, the amount of electricity will be very small – you won't even feel it. The moisture reading will be low, but it should be detectable.

RECOMMENDATION: Depending on the specific purpose of your device, you may need to gather some data under different conditions to figure out which moisture values to use in your code to make decisions about what the device should do.

Last updated