Board and part

Arduino Mega Soil Moisture Sensor Simulator: Wiring, Code and a Live Circuit

This is a soil moisture sensor wired to an Arduino Mega 2560 and read live in your browser. The sketch turns the analog output into a percentage and prints Dry, Moist or Wet.

  • 5 V logic
  • No library: analogRead()
  • AOUT on pin A0
  • Powered from 5V
Arduino Mega · Soil Moisture Sensorlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the Soil Moisture Sensor to the Arduino Mega

Three wires: power from 5V, ground, and AOUT to pin A0. In the editor, drag the sensor’s slider from dry to wet.

Every connection in the circuit above, with the board’s own pin names.

Soil Moisture Sensor pinArduino Mega pinWhy
VCC5VThe module runs from 3.3 to 5 V; at 5 V the output stays inside the analog pin’s range.
GNDGNDCommon ground.
AOUTpin A0The analog output: lower in wet soil, higher in dry air. It goes to an analog input.

Power

The module takes its power from 5V. Its output stays within 0 to 5 V, the converter’s range. Ten bits against 5 V, 0 to 1023, on any of A0 to A15.

Pins to leave alone on the Arduino Mega

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

On a real bench

The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The Soil Moisture Sensor uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

Soil moisture on Arduino Mega pin A0
const int SOIL_PIN = A0;   // AOUT, pin A0
const int DRY_RAW = 522;   // the reading in air
const int WET_RAW = 260;   // the reading in water

void setup() {
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(SOIL_PIN);
  int percent = map(raw, DRY_RAW, WET_RAW, 0, 100);
  percent = constrain(percent, 0, 100);
  Serial.print("Moisture: ");
  Serial.print(percent);
  Serial.print("%  ");
  if (percent < 35) {
    Serial.println("Dry");
  } else if (percent < 65) {
    Serial.println("Moist");
  } else {
    Serial.println("Wet");
  }
  delay(250);
}

analogRead() converts the output voltage to a number. The module gives a lower voltage the wetter the soil is, so DRY_RAW is the larger reading, taken in air, and WET_RAW the smaller one, taken in water.

map() turns that span into 0 to 100 percent and constrain() keeps stray readings inside it. Dry, Moist and Wet are just thresholds on the percentage, at 35 and 65.

The two constants depend on the module and on the supply, so calibrate them: read the sensor in air and in a glass of water, and put those two numbers in.

Pitfalls

Common mistakes with the Soil Moisture Sensor on the Arduino Mega

Using the digital output for a level

The DO pin on the module is only a comparator with a trimmer: it says wet or dry and nothing in between. Use AOUT on an analog pin for a real reading.

Leaving the probe powered all the time

A resistive probe corrodes when current flows through it for months. Power it from a pin and switch it on only while reading, or use a capacitive sensor, which has no exposed metal.

Skipping calibration

Every module and every supply gives different readings in air and in water. Measure both and set DRY_RAW and WET_RAW to them, or the percentage is off.

FAQ

Questions and answers

How do I connect a soil moisture sensor to an Arduino Mega 2560?

Power the module from 5V, join its GND to GND, and wire AOUT to pin A0, an analog input. Read it with analogRead().

Why does the reading fall when the soil gets wetter?

Water makes the soil conduct, which pulls the module’s output voltage down. Dry air gives the highest reading, so a percentage needs map() with the dry value first.

Resistive or capacitive: which should I buy?

Capacitive. A resistive probe is cheap but its bare metal corrodes in the soil; a capacitive one is sealed and lasts. Both wire the same way.

How often should I read it?

Soil changes over minutes, so once every few seconds is plenty, and for a watering project once a minute is enough.

Build your own Soil Moisture Sensor project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.