Using the Simple Soil Moisture Sensor with Arduino involves wiring up the sensor to your micro:bit, installing the Adafruit_Microbit library, and running the provided example code.
- micro:bit 3V to sensor 3V (red wire)
- micro:bit GND to sensor GND (black wire)
- micro:bit 0 to sensor OUT (white wire)
Library Installation
You can install the necessary libraries for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit_GFX, and select the Adafruit GFX library:
If asked about dependencies, click "Install all".
Then, search for Adafruit_Microbit and select the Adafruit microbit library.
If asked about dependencies, click "Install all".
If the "Dependencies" windows do not come up, then you already have the dependencies installed.
If the dependencies are already installed, you must make sure you update them through the Arduino Library Manager before loading the example!
// SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT
/* micro:bit Simple Soil Sensor Demo */
#include <Adafruit_Microbit.h>
Adafruit_Microbit_Matrix microbit;
int sensorPin = 0;
int moisture = 0;
const uint8_t
frown_bmp[] =
{ B00000,
B01010,
B00000,
B01110,
B10001, };
const uint8_t
smile_bmp[] =
{ B00000,
B01010,
B00000,
B10001,
B01110, };
void setup() {
Serial.begin(115200);
while ( !Serial ) delay(10);
Serial.println("micro:bit simple soil sensor");
microbit.begin();
}
void loop() {
// read the value from the sensor:
moisture = analogRead(sensorPin);
Serial.print("Soil moisture: ");
Serial.println(moisture);
if (moisture > 200) {
microbit.show(smile_bmp);
} else {
microbit.show(frown_bmp);
}
delay(5000);
}
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see the micro:bit recognized. Every five seconds, the analog reading from the sensor will be printed to the Serial Monitor. If the reading is greater than 200, the LED matrix shows a happy face. Otherwise, it shows a sad face, letting you know that the soil is dry.
Advanced Sensing
One disadvantage to the soil sensor is that the soil probes can oxidize over time. There is a "hack" you can do to prolong the life of the probes. You can connect the sensor 3V pad to a digital pad on the micro:bit and turn it "on" only when you're getting an analog reading.
- micro:bit 2 to sensor 3V (red wire)
- micro:bit GND to sensor GND (black wire)
- micro:bit 0 to sensor OUT (white wire)
// SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT
/* micro:bit Advanced Soil Sensor Demo */
#include <Adafruit_Microbit.h>
Adafruit_Microbit_Matrix microbit;
int sensorPin = 0;
int onPin = 2;
int moisture = 0;
const uint8_t
frown_bmp[] =
{ B00000,
B01010,
B00000,
B01110,
B10001, };
const uint8_t
smile_bmp[] =
{ B00000,
B01010,
B00000,
B10001,
B01110, };
void setup() {
Serial.begin(115200);
while ( !Serial ) delay(10);
Serial.println("micro:bit advanced soil sensor");
microbit.begin();
pinMode(onPin, OUTPUT);
}
void loop() {
digitalWrite(onPin, HIGH);
delay(50);
// read the value from the sensor:
moisture = analogRead(sensorPin);
Serial.print("Soil moisture: ");
Serial.println(moisture);
if (moisture > 200) {
microbit.show(smile_bmp);
} else {
microbit.show(frown_bmp);
}
digitalWrite(onPin, LOW);
delay(5000);
}
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see the micro:bit recognized. Every five seconds, pin 2 will be set HIGH to power up the sensor. Then, the analog reading from the sensor will be printed to the Serial Monitor. If the reading is greater than 200, the LED matrix shows a happy face. Otherwise, it shows a sad face, letting you know that the soil is dry. After the reading, pin 2 is set LOW to turn off power to the sensor.
Page last edited July 14, 2025
Text editor powered by tinymce.