Board and part

Raspberry Pi Pico Soil Moisture Sensor Simulator: Wiring, Code and a Live Circuit

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

  • 3.3 V logic
  • No library: analogRead()
  • AOUT on GP26 (physical pin 31)
  • Powered from 3V3(OUT)
Raspberry Pi Pico · 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 Raspberry Pi Pico

Three wires: power from 3V3(OUT), ground, and AOUT to GP26 (physical pin 31). 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 pinRaspberry Pi Pico pinWhy
VCC3V3(OUT)The module runs from 3.3 to 5 V; from 3.3 V its output never goes above what the pin can read.
GNDGNDCommon ground.
AOUTGP26 (physical pin 31)The analog output: lower in wet soil, higher in dry air. It goes to an analog input.

Power

The module takes its power from 3V3(OUT), so AOUT never rises above 3.3 V, which is the most an analog pin on the Raspberry Pi Pico can read. GP26 to GP28 are the ADC inputs (A0 to A2): the converter is twelve bits against the 3.3 V rail, and analogRead() returns ten bits, 0 to 1023, as arduino-pico does; analogReadResolution(12) gives 0 to 4095.

Pins to leave alone on the Raspberry Pi Pico

The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.

On a real bench

Solder headers on the Pico and press it into the breadboard across the center channel; the Soil Moisture Sensor goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.

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 Raspberry Pi Pico GP26 (physical pin 31)
const int SOIL_PIN = 26;   // AOUT, GP26 (physical pin 31)
const int DRY_RAW = 791;   // the reading in air
const int WET_RAW = 394;   // the reading in water

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

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 Raspberry Pi Pico

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 a Raspberry Pi Pico?

Power the module from 3V3(OUT), join its GND to GND, and wire AOUT to GP26 (physical pin 31), 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.