Soil Moisture Sensor Project

The Soil Moisture Sensor project demonstrates how to use an Arduino Mega to monitor soil moisture levels using an analog sensor. Soil moisture sensors are essential for applications such as agriculture, gardening, or automated plant watering systems, where precise measurement of soil moisture content is critical.

How It Works

An analog temperature module converts temperature into a voltage the Arduino Mega 2560 can sample directly. Two parts dominate hobby kits: the LM35 precision sensor and NTC thermistor modules such as the KY-013.

The LM35 is the easier of the two because its output is linear by design: exactly 10 mV per °C, referenced to 0 V, so 25 °C produces 250 mV. No calibration curve is required — divide the measured millivolts by 10 and you have degrees Celsius.

A thermistor is a resistor whose value falls as it warms. The KY-013 places a 10 kΩ NTC in a divider with a fixed 10 kΩ resistor, so the module outputs a voltage that changes with temperature but not linearly. Converting it properly needs the Steinhart–Hart relationship, or its simplified B-parameter form, using the thermistor's nominal resistance at 25 °C and its B coefficient (around 3950 for common parts).

The Arduino Mega 2560 samples with a 10-bit ADC, so analogRead() returns 0–1023 across 5 V.

Components Needed

  • Arduino Mega 2560
  • Soil Moisture Sensor
  • Jumper Wires
  • Arduino Mega 2560
  • Power Supply

Wiring to the Arduino Mega 2560

Connect the module's signal pin to A0, VCC to the 5 V rail and GND to ground. With a 5 V supply the output spans 0–5 V, matching the ADC range directly.

Any of A0–A15 (sixteen channels) will work; A0 is used here by convention.

Keep the sensor away from the board's voltage regulator and any motor driver. Both dissipate heat and will bias readings upward by several degrees if the sensor sits next to them.

Module pinArduino Mega 2560 pinFunction
S / OUTA0Analog voltage proportional to temperature
VCC / +5VSupply
GND / −GNDCommon ground

Build and Upload

Open the Arduino IDE and create a new sketch.

Copy and paste the provided Arduino code into the sketch.

Upload the code to the Arduino Mega.

Open the Serial Monitor in the Arduino IDE (set to 9600 baud).

Observe the soil moisture percentage readings displayed.

Verify that the readings change based on the soil moisture level.

Example Code

Reading an LM35 on the Arduino Mega 2560 with averaging to suppress ADC noise. Upload it with the board set to Arduino Mega 2560 and open the Serial Monitor at 9600 baud.

Reading an LM35 on the Arduino Mega 2560 with averaging to suppress ADC noise
const int SENSOR_PIN = A0;
const float VREF = 5.0;      // ADC reference voltage
const int   ADC_MAX = 1023;   // 10-bit converter
const int   SAMPLES = 16;

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

float readCelsius() {
  long total = 0;
  for (int i = 0; i < SAMPLES; i++) {
    total += analogRead(SENSOR_PIN);
    delay(5);
  }
  float counts  = (float)total / SAMPLES;
  float volts   = counts * VREF / ADC_MAX;
  return volts * 100.0;            // LM35: 10 mV per degree C
}

void loop() {
  float c = readCelsius();
  Serial.print("Temperature: ");
  Serial.print(c, 1);
  Serial.print(" C  /  ");
  Serial.print(c * 9.0 / 5.0 + 32.0, 1);
  Serial.println(" F");
  delay(1000);
}

Applications

A analog temperature sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:

  • Room and greenhouse climate logging
  • Over-temperature cut-outs for motors, power supplies and battery packs
  • Thermostat control for heaters, fans and incubators
  • Cold-chain monitoring where a readable margin of ±1 °C is acceptable
  • Compensation inputs for other sensors whose output drifts with temperature

Working with the Arduino Mega 2560

The Arduino Mega 2560 is built around the ATmega2560 and runs on 5 V logic with 8 KB of SRAM and 256 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.

The Mega’s 8 KB of SRAM is four times the Uno’s, so larger buffers and lookup tables are practical.

I²C lives on D20/D21 rather than A4/A5 — wiring copied from an Uno tutorial will not work unchanged.

Four hardware UARTs mean a serial sensor can have its own port instead of fighting SoftwareSerial.

Arduino Mega 2560 characteristicValueWhy it matters here
Logic voltage5 VMatches most hobby modules directly
ADC resolution10-bit (0–1023)Sets how finely an analog reading can be resolved
Analog inputsA0–A15 (sixteen channels)Determines how many analog sensors can share the board
PWM outputsD2–D13 and D44–D46Needed for brightness, speed and tone control
I²C pinsD20 (SDA) and D21 (SCL)Fixed by hardware — wiring copied from another board may not match
Interrupt pinsD2, D3, D18, D19, D20 and D21Required for counting fast or asynchronous events
Serialfour independent hardware UARTsMonitor runs at 9600 baud by default

Troubleshooting

Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:

  • Readings sit around 0 or jump wildly — the signal pin is on a digital pin rather than an analog one.
  • Temperature reads several degrees high — the sensor is picking up heat from the regulator or a nearby driver; move it away from the board.
  • Values drift slowly over minutes — self-heating. Sample periodically rather than continuously.
  • A thermistor module reads nonsense when treated as linear — the KY-013 needs the B-parameter conversion, not the LM35 formula.
  • Code written for an ESP board gives odd analog values — the Arduino Mega 2560 uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
  • An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Mega 2560 I²C is on D20 (SDA) and D21 (SCL).

Taking It Further on the Arduino Mega 2560

Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Mega 2560 specifically:

The Mega’s sixteen analog inputs make it the right board when several of these sensors must run at once. Where an Uno would need an external multiplexer, the Mega simply reads A0 through A15 directly.

Four hardware UARTs mean a GPS, a serial display and a debug console can coexist without SoftwareSerial, which is unreliable above 38400 baud and blocks interrupts while it transmits.

With 8 KB of SRAM there is room to buffer readings and do real processing — a rolling average over several hundred samples, or holding a full display frame in memory, both of which are impractical on an Uno.

Notes and Practical Limits

Averaging sixteen samples, as the sketch does, trades response speed for stability and removes most of the least-significant-bit jitter. Each ADC step is roughly 4.9 mV, which corresponds to about 0.5 °C with an LM35 — the practical resolution limit without an external amplifier.

For better than ±1 °C, or for readings sent over any distance, prefer a digital sensor such as the DS18B20 or DHT22. Digital parts transmit numbers rather than voltages, so cable resistance and connector corrosion stop mattering.