Wiring guide

How to wire a soil moisture sensor to a Raspberry Pi Pico

To wire a soil moisture sensor to a Raspberry Pi Pico, connect VCC to 3V3(OUT), GND to GND and AOUT to GP26 (physical pin 31). Power the module from 3.3 V so AOUT never goes above what the analog pin can read. The sketch below uses no library beyond what the board includes. It reads AOUT and prints a moisture percentage with the word Dry, Moist or Wet.

  • 3.3 V logic
  • Library: none
  • 3 wires
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.
Pin to pin

Soil moisture sensor to Raspberry Pi Pico wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

Soil moisture sensor pinRaspberry Pi Pico pinWire colorWhy
VCC3V3(OUT)redThe module runs from 3.3 to 5 V; from 3.3 V its output never goes above what the pin can read.
GNDGNDblueCommon ground.
AOUTGP26 (physical pin 31)greenThe analog output: lower in wet soil, higher in dry air. It goes to an analog input.
Voltage

Power and logic levels for soil moisture sensor and Raspberry Pi Pico

Logic level

The Raspberry Pi Pico’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.

Supply

The soil moisture sensor takes 3.3 V from 3V3(OUT). That pin is the Raspberry Pi Pico’s own regulated 3.3 V output.

This pair

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

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.

The sketch

The working code for the Raspberry Pi Pico

Library: none, the sketch reads AOUT with analogRead(). This is the sketch the circuit above runs.

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);
}
Output

What you should see

Open the serial monitor at 115200 baud. Each line shows the moisture as a percentage and a word: Dry under 35 percent, Moist under 65 and Wet above. Drag the sensor’s slider from dry to wet and the line follows.

The two calibration numbers in the sketch depend on the module, so read yours in air and in water and put them in.

The serial monitor prints this, recorded from the circuit above with the soil set to 90 percent wet after 300 ms:

Moisture: 29%  Dry
Moisture: 90%  Wet
Pitfalls

Common mistakes wiring a soil moisture sensor to a 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.

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.