CxD Archive
Internet of Things Project
Internet of Things Project
  • IoT Project Background
  • IoT Project Challenge
  • IoT Project Outline
    • 1-1 Smartphone Research
    • 1-2 Smart Thermostat Research
    • 1-3 Smart Device Research
    • 1-4 Introduction to Photon Kit
    • 1-5 Build Smart Lightbulb
    • 1-6 Generate Project Ideas
    • 1-7 Build Smart Thermostat
    • 2-1 Evaluate Project Ideas
    • 2-2 Plan User Research
    • 2-3 Compile Research Data
    • 2-4 Construct Personas
    • 2-5 Construct Journey Map
    • 2-6 Define Value Proposition
    • 2-7 Define Design Requirements
    • 3-1 Generate Solution Ideas
    • 3-2 Evaluate and Refine Solution Ideas
    • 3-3 Create System Model of Solution
    • 3-4 Create Wireflow for Web App
    • 3-5 Create Storyboard for Solution
    • 3-6 Evaluate Solution Design
    • 3-7 Present Project Proposal
    • 4-1 Track Progress and Issues
    • 4-2 Build Smart Device
    • 4-3 Create Photon App
    • 4-4 Create Web App
    • 4-5 Conduct Integration Testing
    • 4-6 Create Product Demo Video
    • 4-7 Create Product Marketing Website
    • 5-1 Evaluate Smart Device and Web App
    • 5-2 Evaluate Product Marketing Website
    • 5-3 Improve Solution Based on Evaluations
    • 6-1 Create Project Poster
    • 6-2 Present Project to Public
    • 6-3 Write Personal Reflection
  • References for Wiring & Coding
    • Creating Circuits
    • Wiring Programming Language
    • Particle Cloud
    • Hello World App for Photon
    • LED Light
    • Push Button
    • Micro OLED Display
    • Humidity and Temp Sensor
    • Speaker
    • Servo Motor
    • Motion Sensor
    • Magnetic Switch
    • Potentiometer
    • Photocell
    • Moisture Sensor
    • Accelerometer
    • RFID Reader
    • Fingerprint Scanner
    • GPS Receiver
    • Camera
    • Microphone
    • Force Sensitive Resistor
    • Keypad
    • Continuous Rotation Servo Motor
  • Glossary
  • Updated Project Guidebook
  • Updated Code Guidebok
Powered by GitBook
On this page
  • Code for Moisture Sensor
  • Library
  • Global Variables
  • setup( ) function
  • Measuring Amount of Moisture
Export as PDF
  1. References for Wiring & Coding

Moisture Sensor

PreviousPhotocellNextAccelerometer

Last updated 7 years ago

The 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.

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.

moisture sensor
Experiment 3