Project 2.3.4: Bluetooth Pump Automation
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project combines a soil moisture sensor, a relay-controlled pump, and BLE commands to create an automated watering demo.
The system uses moisture thresholds, timed pump operation, and a rest interval so the pump does not chatter on and off continuously.
A similar design could be used in a smart planter or irrigation test rig where the operator still wants manual control from a phone.
Required Components
![]() Soil moisture sensor |
Tube and water container |
![]() Raspberry Pi Pico 2 W |
![]() 1-channel relay module |
![]() Small DC water pump |
![]() External pump power supply |
![]() Breadboard and jumper wires |
Phone with BLUETOOTH app |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor VCC | 3.3V | Physical pin 36 | Sensor power |
| Soil sensor GND | GND | Physical pin 38 | Common ground |
| Soil sensor AOUT | GPIO 26 | GPIO 26 / physical pin 31 | ADC0 input |
| Relay IN | GPIO 16 | GPIO 16 / physical pin 21 | Relay control signal |
| Relay module GND | Pico GND and external supply GND | Physical pin 38 for Pico GND | Shared ground unless the relay module is fully isolated |
| Relay module VCC | External 5V supply if required by the module | Not a Pico GPIO | Check the relay module label |
| External pump supply positive | Relay COM to NO path | Not a Pico GPIO | Pump positive should be switched by the relay |
| Pump negative | External pump supply negative | Not a Pico GPIO | Do not route pump current through the Pico |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ │
│ GPIO 16 ───────────┤──── Relay IN
│ │
│ 3.3V ───────────┤──── Soil Sensor VCC
│ GND ───────────┤──── Soil Sensor GND
│ GND ───────────┤──── Relay GND
│ │
└─────────────────────┘
Relay Module External Supply
┌──────────┐ ┌────────────┐
│ COM ────┼────────────┤ (+) │
│ NO ────┼──┐ │ │
│ VCC ────┼──┼─────────┤ (+) (if │
│ GND ────┼──┼──┐ │ needed) │
└──────────┘ │ │ │ │
│ │ └────────────┘
│ │ │
│ └───────────┘
│ Pump (+)
│
└────────────── Pump (-) ──── External Supply (-)
Step-by-Step Assembly
Step 1: Place the Raspberry Pi Pico 2W
Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.
Step 2: Position the Soil Moisture Sensor
Place the soil moisture probe so the sensing end can enter the soil sample. Identify VCC, GND, and AOUT / AO / Signal on the sensor module.
Step 3: Connect Soil Sensor VCC
Connect the soil sensor VCC pin to 3.3V.
Step 4: Connect Soil Sensor GND
Connect the soil sensor GND pin to GND.
Step 5: Connect Soil Sensor AOUT
Connect the soil sensor AOUT, AO, or Signal pin to GPIO 26 (ADC0).
Step 6: Place the Relay Module
Place the relay module where its control pins and screw terminals are easy to reach. Identify VCC, GND, IN, COM, NO, and NC before wiring.
Step 7: Connect Relay IN
Connect relay IN to GPIO 16.
Step 8: Connect Relay GND
Connect relay GND to Pico GND and to the external pump supply GND unless the relay module is fully isolated.
Step 9: Connect Relay VCC
Connect relay VCC to the external supply voltage required by your relay module.
Step 10: Wire the Pump Positive Path
Connect the external pump supply positive wire to relay COM. Connect relay NO to the pump positive wire.
Step 11: Connect the Pump Negative Wire
Connect the pump negative wire directly to the external pump supply negative wire.
Step 12: Check the Pump Area
Keep the Pico, breadboard, relay control wiring, USB cable, and jumper wires away from water. Test the relay click before connecting the pump, and never let the pump run dry.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] Soil sensor VCC connects to 3.3V
- [x] Soil sensor GND connects to GND
- [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
- [x] Relay IN connects to GPIO 16
- [x] Relay VCC connects to the correct external relay supply
- [x] Relay GND, Pico GND, and external pump supply GND share a common reference where required
- [x] Pump positive path uses relay COM and NO
- [x] Pump negative connects to external pump supply negative
- [x] No pump current is routed through the Pico
- [x] No loose jumper wires
Intermediate Note This project uses BLE on the Raspberry Pi Pico 2W. Use a BLE app such as nRF Connect, LightBlue, or a BLE UART terminal to connect to
AIDER_PumpAutoand send commands. Test dry soil and wet soil before setting moisture thresholds. The code uses dry and wet reference readings to estimate moisture percent.Safety Note Do not power the pump from the Pico. Use an external pump supply, keep electronics away from water, and stop testing immediately if the pump runs dry.
Testing Individual Components
Before running the full project, test each part separately. This makes it easier to find wiring, library, or code problems.
Hardware Setup
- Build the soil sensor circuit and relay control circuit first
- Keep the water container and pump physically away from the Pico and breadboard
Test the Input Sensor
- Run a short ADC script and record dry and wet sensor values
- Use those values to understand how to calibrate the moisture percentage
Test the Output Device
- Test the relay by listening for a click or by switching a safe LED load first
- Only move to the pump after the relay logic is confirmed
Test Communication
- Upload the final code and connect to
AIDER_PumpAuto - Send
status,auto,manual on,manual off, andwater nowto confirm each command path works
Run the Full System
- Capture dry and wet references with the soil sensor
- Enable auto mode and observe whether the pump starts only when the soil is below the dry threshold
- Confirm the pump stops after the programmed runtime and does not restart immediately because of the rest timer
Save the Project
- Save the working code and note the calibration values used for your soil sample
- Dry the workspace after water testing is complete
Additional Testing and Calibration Checks
Calibration and tuning notes
- Capture a dry reference while the probe is in dry soil or in air, then capture a wet reference in clearly moist soil
- Observe how the moisture percentage changes and adjust the dry threshold only after calibration is complete
- Check that the pump does not start repeatedly during one drying event. If it does, increase
MIN_REST_MSor widen the threshold gap - For safe classroom testing, replace the real pump with an LED load first, then switch to the pump once the logic is verified
Quick testing checklist
- [ ] Soil sensor raw reading changes with moisture level
- [ ] Relay switches correctly before pump testing
- [ ] Pump is powered from an external supply
- [ ] BLE app finds
AIDER_PumpAuto - [ ] Auto mode waters only below the dry threshold and rearms above recovery
- [ ] Manual on/off and
water nowcommands work correctly
Full Project Code
After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below into a new file, or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.
from machine import ADC, Pin
import bluetooth
import time
from ble_uart import BLEUART
DEVICE_NAME = "AIDER_PumpAuto"
RELAY_ON = 0
RELAY_OFF = 1
PUMP_RUN_MS = 5000
MIN_REST_MS = 30000
soil = ADC(26)
relay = Pin(16, Pin.OUT)
ble = bluetooth.BLE()
ble.active(True)
uart = BLEUART(ble, name=DEVICE_NAME)
dry_reading = 52000
wet_reading = 22000
dry_threshold = 35
recovery_threshold = 45
auto_mode = True
pump_running = False
pump_stop_at = 0
ready_to_water = True
last_cycle_ms = time.ticks_add(time.ticks_ms(), -MIN_REST_MS)
command_queue = []
def send_line(message):
print(message)
uart.write((message + "\n").encode())
def on_rx(data):
command = data.decode("utf-8").strip().lower()
if command:
command_queue.append(command)
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def moisture_percent(raw_value):
span = dry_reading - wet_reading
if span == 0:
return 0
percent = int(((dry_reading - raw_value) * 100) / span)
return clamp(percent, 0, 100)
def set_pump(turn_on, timed=False):
global pump_running, pump_stop_at, last_cycle_ms, ready_to_water
relay.value(RELAY_ON if turn_on else RELAY_OFF)
pump_running = turn_on
pump_stop_at = 0
if turn_on:
now = time.ticks_ms()
pump_stop_at = time.ticks_add(now, PUMP_RUN_MS) if timed else 0
last_cycle_ms = now
ready_to_water = False
def stop_pump(reason):
global pump_running
relay.value(RELAY_OFF)
pump_running = False
send_line("Pump OFF: {}".format(reason))
def start_timed_cycle(reason):
set_pump(True, timed=True)
send_line("Pump ON: {}".format(reason))
uart.on_rx(on_rx)
relay.value(RELAY_OFF)
send_line("Bluetooth pump automation ready")
while True:
now = time.ticks_ms()
raw_value = soil.read_u16()
moisture = moisture_percent(raw_value)
if pump_running and time.ticks_diff(now, pump_stop_at) >= 0:
stop_pump("timer finished")
if moisture >= recovery_threshold:
ready_to_water = True
if auto_mode and ready_to_water and not pump_running:
if moisture <= dry_threshold and time.ticks_diff(now, last_cycle_ms) >= MIN_REST_MS:
start_timed_cycle("soil below dry threshold")
if command_queue:
command = command_queue.pop(0)
if command == "auto":
auto_mode = True
send_line("Mode: AUTO")
elif command == "manual on":
auto_mode = False
set_pump(True, timed=False)
send_line("Mode: MANUAL | Pump ON until manual off")
elif command == "manual off":
auto_mode = False
stop_pump("manual off")
elif command == "water now":
auto_mode = False
start_timed_cycle("manual timed cycle")
elif command == "status":
mode = "AUTO" if auto_mode else "MANUAL"
state = "ON" if pump_running else "OFF"
send_line("Mode:{} Pump:{} Moisture:{}%".format(mode, state, moisture))
send_line("Dry ref:{} Wet ref:{} Armed:{}".format(dry_reading, wet_reading, ready_to_water))
elif command == "dry":
dry_reading = raw_value
send_line("Dry reference captured: {}".format(dry_reading))
elif command == "wet":
wet_reading = raw_value
send_line("Wet reference captured: {}".format(wet_reading))
elif command == "help":
send_line("Commands: auto, manual on, manual off, water now, status, dry, wet, help")
else:
send_line("Unknown command. Send help.")
time.sleep_ms(150)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| Calibration values | Store dry and wet sensor references | Soil moisture thresholds depend on calibration, not guesswork |
dry_threshold and recovery_threshold |
Define when the system should water and when conditions are safely moist again | This hysteresis reduces rapid switching |
PUMP_RUN_MS and MIN_REST_MS |
Time the watering cycle and enforce a rest gap | Helps protect the pump and stabilise the system |
| Auto and manual command paths | Allow either automatic operation or phone-controlled manual testing | Students can debug safely without changing wiring |
Expected Result
Once the soil sensor is calibrated, the pump runs automatically only when the soil is below the dry threshold, the minimum rest time has expired, and the moisture has recovered above the re-arm threshold since the last cycle. The phone can also keep the pump on in manual mode, trigger one timed cycle with water now, and request the current system status.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| Pump never starts | Relay wiring, pump power, or calibration values are wrong | Test the relay separately, confirm external pump power, and capture dry and wet references again |
| Pump starts too often | The rest interval is too short or the threshold is too aggressive | Increase MIN_REST_MS or widen the gap between the dry and recovery thresholds after observing real readings |
| Pump runs but no water moves | The intake is dry, the tubing is blocked, or the pump is wired incorrectly | Stop the test immediately, refill the water source, and inspect the tubing and pump polarity |
| Phone cannot find the BLE device | The Pico is not advertising or the BLE helper files are missing | Confirm ble_uart.py and ble_advertising.py are saved on the Pico, restart the board, and scan again from nRF Connect or LightBlue |
| Commands do not change the system state | The phone is connected to the wrong service or the command text is different from the expected command | Reconnect to the BLE UART service and send the exact command shown in the test section |





