Project tutorial
IoT Moisture Sensor

IoT Moisture Sensor © GPL3+

An IoT Moisture sensor that sends moisture data from an Arduino Nano 33 IoT to the Arduino IoT Cloud

  • 9 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

Earlier this year, my parents had our lawn redone. To keep our lawn fresh and green, my dad had to water the lawn every day but had to make sure he was not overwatering the soil, otherwise, the grass would die. To help my dad, I created this small device that could sense the moisture level of the soil and send messages through the Arduino IoT Cloud to my computer, so we could know when to water the lawn.

Notes:

  • This project is battery powered, so to achieve a secured fit to the Nano 33 IoT, you may need to solder headers or jumper wires onto the battery clip
  • You must be careful when water is poured over this. If you plan on using it in an outdoor environment as I did, make sure you put a static-proof plastic cover over your nano 33 IoT, moisture sensor, and battery to ensure the safety of your device. If you plan on using this indoors, like the cover picture, you do not need to have a cover, but you must be careful when pouring water over the moisture sensor as there are components fixed to it. Water away from the moisture sensor and keep the soil underneath the white line for safety purposes, else you risk damaging your sensor and board.

Building Sequence:

1. Connect a moisture sensor to the Nano 33 IoT board. The red wire goes to 3.3V, the black wire goes to GND, and the yellow wire goes to Analog Pin A1 on the board. You can choose any analog pin you like, however, make sure you change the pin number inside the code. Change the number inside the analogRead function inside the code. A snippet of the lines is below.

//Read data from Analog Pin 1 on Nano 33 IoT and print to Serial Monitor for debugging
 soilMoistureLevel = analogRead(1);
 Serial.println(soilMoistureLevel);

2. Next, connect a cable to your Nano 33 IoT board and create the variables listed below inside your thing

Class Variable

  • string message
  • int moistureLevel
  • CloudPercentage moisturePercent

3. Then, set up your Nano 33 IoT as the device for your thing and enter your network credentials in the fields below.

4. Copy and Paste the code below into the sketch tab of your thing and upload it to your Arduino. Once it has been uploaded, open the serial monitor to check whether the moisture sensor is functioning or not. If it is not, try troubleshooting by resetting the code back to its original state or checking the moisture sensor's pin if you had changed the moisture sensor's pin

5. Next, create a dashboard with a few widgets. Below is the dashboard I use.

Here is the list that shows what variable each widget is connected to

  • PLANT MOISTURE widget ==> moisturePercent
  • MOISTURE VALUE widget ==> moistureValue
  • the graph in the bottom left ==> moisturePercent
  • MESSENGER widget ==> message

6. Reload the dashboard. if you see the data in the moisture value widget fluctuate by a few digits, then you have completed this project. Congratulations! If you have any problems, questions, or advice, please comment below and I will get back to you as soon as possible.

A demonstration of the project is below.

Thanks for reading!

Demonstration

Code

CodeArduino
Code for Nano 33 IoT. You can modify the pin number if you have Analog Pin A1 already used.
#include "thingProperties.h"

int soilMoistureLevel;
int soilMoisturePercent;

int airValue = 830;
int waterValue = 360;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update(); // Update the Cloud's data
  
  //Read data from Analog Pin 1 on Nano 33 IoT and print to Serial Monitor for debugging
  soilMoistureLevel = analogRead(1);
  Serial.println(soilMoistureLevel);
  
  //Convert soilMoistureLevel integer into percentage by using air and water values defined above and mapping with 0 - 100
  soilMoisturePercent = map(soilMoistureLevel, airValue, waterValue, 0, 100);
  Serial.println(soilMoisturePercent); //Print to Serial Monitor for debugging
  
  
  //Update Arduino IoT Cloud values
  moistureLevel = soilMoistureLevel;
  moisturePercent = soilMoisturePercent;
  
  //If the moisture percentage is less than 30%
  if(moisturePercent < 30)
  {
    //Send message to Arduino Cloud messenger on dashboard
    message = "Water Levels Low. Watering Required!";
  }
  
  //If the moisture percentage is greater than 50%
  if(moisturePercent > 50)
  {
    //Send message to Arduino Cloud messenger on dashboard
    message = "Water Levels Sufficient.";
  }
  
  
}


//End of Code

Schematics

Schematic
Circuit Diagram using Paint 3D (since I don't have fritzing)
Iot moisture sensor schematic q02d0ofi2h

Comments

Similar projects you might like

BLE Sharp Dust Sensor - ARTIK IoT Cloud

Project tutorial by Elijah Scheele

  • 7,334 views
  • 1 comment
  • 20 respects

IOT - Smart Jar Using ESP8266, Arduino and Ultrasonic Sensor

Project tutorial by Team Smazee

  • 17,591 views
  • 5 comments
  • 19 respects

DIY Plant Moisture Sensor

Project tutorial by millerman4487

  • 28,851 views
  • 10 comments
  • 45 respects

IoT Node with STM32F4 Discovery, MKR1000 and Azure IoT Hub

Project tutorial by vincent wong

  • 3,407 views
  • 0 comments
  • 6 respects

Your Environmental Data on Arduino IoT Cloud

Project tutorial by 3 developers

  • 39,478 views
  • 36 comments
  • 112 respects

Arduino IoT Cloud Amazon Alexa Integration

Project tutorial by 4 developers

  • 36,042 views
  • 29 comments
  • 88 respects
Add projectSign up / Login