Pico water tank in MicroPython
The water tank's level loop on a Raspberry Pi Pico in MicroPython: the level sensor on its ADC, the pump's MOSFET on a PWM output, the float switch polled,…
Control · Intermediate
In a real product: Level controller (Agriculture): A greenhouse header tank holds its level for the drip lines, on a board a grower can reprogram.
The same PI level loop as the Uno's, in MicroPython on a Raspberry Pi Pico. The tank's sensor runs from the Pico's 3.3 V and gives a share of it, so the level reads the same as on 5 V. Open the World tool to watch it.
What you'll learn
machine.ADCandread_u16on a ratiometric sensormachine.PWMat 1 kHz into a MOSFET board: duty as pump speed- A polled control loop with
time.sleep_ms, reading Serial without blocking (select.poll) - PI control with conditional integration, in a few lines of Python
How it works
LEVEL is on GP26 (ADC0): read_u16 gives 0–65535 for 0–3.3 V, and the sensor gives 4% empty to 94% at the 250 mm rim. The loop runs every 20 ms: it filters the reading, works out KP·error + integral as a duty from 0 to 1, and writes it to the pump's PWM on GP15. The integral only grows while the pump is not already flat out the same way, so the fill from empty does not wind it up. The float switch on GP14 stops the pump above 230 mm.
Lines typed into the serial monitor change it: kp=0.08, ki=0.03, sp=120. The loop checks for them with select.poll, so waiting for input never stops the control.
Try this
- Run with the World tool open, then open the drain from the tank's card: watch the loop make up the extra outflow.
- Type
ki=0: the steady-state error comes back. Typeki=0.05again to remove it. - Raise
KPuntil the level starts to ring. The pump's 0.3 s spin-up is what limits it. - Move the valve with its slider while it runs: the loop has to find a new duty for each opening.
main.py
# Water tank level on a Raspberry Pi Pico in MicroPython: a PI loop with anti-windup, polled every 20 ms.
# The pump's MOSFET SIG on GP15 (PWM at 1 kHz), the level sensor's LEVEL on GP26 (ADC0), the FULL float switch on GP14.
# The tank's sensor runs from the Pico's 3.3 V, so LEVEL is a share of 3.3 V: 4% empty, 94% at the 250 mm rim.
from machine import ADC, PWM, Pin
import sys, select, time
KP, KI = 0.05, 0.05 # duty (0..1) per mm, and per mm a second
setpoint = 150.0 # mm
sensor = ADC(26)
full = Pin(14, Pin.IN)
pump = PWM(Pin(15))
pump.freq(1000)
def read_level():
share = sensor.read_u16() / 65535
return (share - 0.04) / 0.9 * 250
def command(text):
global KP, KI, setpoint
key, _, value = text.partition('=')
if key == 'kp': KP = float(value)
elif key == 'ki': KI = float(value)
elif key == 'sp': setpoint = float(value)
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
level = read_level()
integral = 0.0
n = 0
while True:
while poll.poll(0):
line = sys.stdin.readline().strip()
if line:
command(line)
level += (read_level() - level) * 0.3
error = setpoint - level
out = KP * error + integral
# Integrate only while the pump is not already flat out the same way.
if not (out >= 1 and error > 0) and not (out <= 0 and error < 0):
integral = min(1.0, max(0.0, integral + KI * error * 0.02))
duty = min(1.0, max(0.0, KP * error + integral))
if full.value():
duty = 0.0 # the float switch: never overflow
pump.duty_u16(int(duty * 65535))
n += 1
if n % 5 == 0:
print('setpoint:%d level:%.1f duty:%.2f' % (setpoint, level, duty))
time.sleep_ms(20)