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
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 pin | Raspberry Pi Pico pin | Wire color | Why |
|---|---|---|---|
| VCC | 3V3(OUT) | red | The module runs from 3.3 to 5 V; from 3.3 V its output never goes above what the pin can read. |
| GND | GND | blue | Common ground. |
| AOUT | GP26 (physical pin 31) | green | The analog output: lower in wet soil, higher in dry air. It goes to an analog input. |
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 working code for the Raspberry Pi Pico
Library: none, the sketch reads AOUT with analogRead(). This is the sketch the circuit above runs.
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);
}
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% WetCommon 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.