Before connecting an actual float or pump, build a model that proves what the controller means by “empty,” “between levels” and “full.” This lesson uses two ordinary switches as simulated float contacts. It teaches consistency checking: a high-level sensor claiming water is present while the lower sensor claims it is absent deserves attention.

Define the contacts before writing code
A dry contact is a switch that supplies no voltage of its own. SPST means single-pole single-throw, one independent on/off contact. In this model, closed means wet and open means dry. Those meanings are a design convention, not a universal property of float switches. A purchased float may reverse its contact behavior when its ring is inverted or mounted differently; characterize the exact part before replacing our model switches.
The board is an original Raspberry Pi Pico using RP2040, with its onboard LED on GP25. The program is written for MicroPython 1.26.0. A Pico W routes its onboard LED differently, so that substitution requires a deliberate code change. GPIO means general-purpose input/output, the programmable signal pins; the GP numbers below differ from physical header positions.
Parts and equipment
- Original Raspberry Pi Pico RP2040 with MicroPython 1.26.0; two single-pole single-throw (SPST) dry-contact switches for a tabletop model.
- Two 10 kΩ resistors, breadboard, short leads, and a card marked LOW and HIGH. No liquid, pump, relay or mains circuit is required.
Every connection
| Pin or part | Connection and purpose |
|---|---|
| LOW model | GP14 physical 19 → LOW node; 10 kΩ from LOW to 3V3 OUT physical 36; LOW switch from node to ground. |
| HIGH model | GP15 physical 20 → HIGH node; separate 10 kΩ from HIGH to 3.3 V; HIGH switch from node to ground. |
| Ground | Both switches return to Pico GND physical 18 or another documented GND pin. |
| Indication | Original Pico onboard LED is GP25, used only to indicate the impossible stable combination. USB supplies the board; no actuator is connected. |
Follow the current
Each 10 kΩ pullup makes an open switch read 3.3 V. Closing a switch brings the input near ground and draws 3.3/10000 = 0.33 mA through that resistor. Both closed draw approximately 0.66 mA, excluding the board. The inputs are active-low: electrical zero represents our logical wet condition. The program uses not pin.value() to express that conversion explicitly.
A wire broken between a switch and ground also produces an open reading. This circuit cannot distinguish “dry” from every open-wire fault. The two-sensor consistency check catches some contradictions, not all faults. For example, a broken LOW wire can produce an impossible combination only when HIGH still indicates wet. If both wires are open, the pattern looks like an empty tank.
The complete state table
| LOW wet | HIGH wet | Meaning in this model |
|---|---|---|
| No | No | Below the lower level |
| Yes | No | Between the two levels |
| Yes | Yes | At or above the high level |
| No | Yes | Inconsistent: inspect mounting, wiring and contacts |
The inconsistency rule assumes one connected body of liquid, with the LOW sensor actually below HIGH. Separate chambers, unusual flow paths or reversed labels invalidate that assumption. A state table is useful precisely because it forces the geometry and meanings to be stated before software acts.
Debounce the pair, not an imagined level
Mechanical contacts bounce, repeatedly opening and closing during a transition. The program samples the pair every 10 ms and requires six consecutive identical pairs. From the first observation to the sixth is at least 50 ms. Processing overhead can lengthen that interval; it does not make the interval shorter. A candidate that changes resets the consecutive-sample count.
Filtering the pair prevents independently accepted contact histories from being combined without checking their simultaneous sampled state. It does not guarantee that real fluid movement is slow, nor that every short contradiction will be retained. A pattern lasting less than the debounce window may be discarded. The console begins UNKNOWN and reports only accepted pairs. The LED illuminates only for a stable inconsistency; it is an error indicator, not a “tank full” lamp.
Complete program
"""Two dry-contact switches simulate wet float contacts. No actuator output."""
def classify(low_wet, high_wet):
return {(False, False): 'BELOW LOW', (True, False): 'BETWEEN',
(True, True): 'AT HIGH', (False, True): 'FAULT: IMPOSSIBLE'}[(low_wet, high_wet)]
class Tank:
def __init__(self):
self.candidate = self.accepted = None
self.samples = 0
def sample(self, pair):
if pair != self.candidate:
self.candidate, self.samples = pair, 1
else:
self.samples += 1
if self.samples >= 6 and pair != self.accepted:
self.accepted = pair
return classify(*pair)
return None
def main():
from machine import Pin
from time import sleep_ms
low, high = Pin(14, Pin.IN), Pin(15, Pin.IN)
led = Pin(25, Pin.OUT, value=0)
tank = Tank()
print('UNKNOWN until a pair has stayed stable for at least 50 ms')
while True:
state = tank.sample((not low.value(), not high.value()))
if state is not None:
print(state)
led.value(state == 'FAULT: IMPOSSIBLE')
sleep_ms(10)
if __name__ == '__main__':
main()
Exercise all four cases
- Label the switches LOW and HIGH on a cardboard tank drawing. With power disconnected, wire each pullup and switch. Install the specified MicroPython firmware and save the listing as main.py.
- Open both switches, then power the board. After the initial UNKNOWN message, expect BELOW LOW with the LED off.
- Close LOW and leave HIGH open. Expect BETWEEN. Close HIGH as well: expect AT HIGH. Open HIGH and then LOW to model draining.
- With both open, close HIGH first. Hold it for a second. Expect FAULT: IMPOSSIBLE and the onboard LED on. Open HIGH again and confirm the fault indication clears after a stable plausible state.
- Try quick switch movement near the filter threshold. Record which changes are accepted. A stopwatch is insufficient to verify a precise 50 ms boundary; use a signal generator or logic analyser for timing characterization if available.
From a model to a real sensor
A real float choice requires contact ratings suitable for small sensing currents, material compatibility with the liquid, mounting orientation and a documented electrical interface. This article does not specify or certify a wetted assembly. Test the detached contact first with a continuity meter and record what “wet” actually closes. Do not connect an industrial sensor’s powered output directly to a Pico input without checking its voltage and output type.
A pump controller would additionally need a safe startup policy, minimum run/off times, fault latching, manual override behavior and properly rated switching hardware. None is present here. The finished result of this lesson is a four-state model whose explanations remain visible even when the sensors disagree.
Host tests exercise every pair, startup filtering and interrupted candidates using the supplied Tank class. This circuit has not been physically tested. Record switch type, mounting assumptions and observed transitions when you build it; do not label a state-machine test as a water test.
Challenge: does “both dry” prove the tank is empty?
No. It is a plausible interpretation of the two contacts, but broken wires or incorrectly mounted switches can imitate it. Additional supervision is required if that distinction matters.
Sources and related lessons
Continue with why a voltage divider drops under load, measuring current through a resistor with a voltmeter, and the interactive circuit lessons.



