Learn
Reading a Float Switch Sensor on a Raspberry Pi 4B (Water-Level Alarm)
Wire a mechanical float switch to a Raspberry Pi 4B through an external 10kΩ pull-up and read its digital state in Python — a fixed-trip-point liquid sensor for tank-level alarms, not a continuous water-level measurement, plus how to toggle it live to test your script.
Note: the Float Switch Sensor is a Premium-tier component in the simulator — you’ll need a Premium or VIP plan to use it there.
Try this directly in the free Raspberry Pi 4B simulator — no hardware or signup required. New to the GPIO header? Start with the interactive pinout guide.
What you’ll need
- Raspberry Pi 4B
- Float Switch Sensor
- Breadboard
- Resistor (set to 10kΩ — the switch's own external pull-up)
Step by step
- Drag Raspberry Pi 4B and a Breadboard onto the Canvas.
- Drag the Float Switch Sensor onto the Canvas — like the Servo or HC-SR04, it connects only through drawn wires, not a breadboard.
- Hover over its 2 pins to confirm the labels: Signal and GND.
- Drag a Resistor onto the breadboard and select it — in its settings panel, set Resistance to 10 and the unit to kΩ.
- Wire GND → a Pi GND pin.
- Wire Signal → a Pi GPIO pin you'll configure as an input (e.g. physical pin 37), and also Signal → one leg of the 10kΩ Resistor. Wire the Resistor's other leg → a Pi 3.3V pin — this is the external pull-up the switch needs; without it, Signal has no defined level at all when the switch is open.
- Go to the Code tab and write a script that polls the pin:
import RPi.GPIO as GPIOimport asyncio GPIO.setmode(GPIO.BOARD)SIGNAL = 37 # adjust to match your wiring GPIO.setup(SIGNAL, GPIO.IN) while not should_stop(): level = GPIO.input(SIGNAL) print("Float UP — contact closed, tank full" if level == 0 else "Float DOWN — contact open, tank not full") await asyncio.sleep(0.5)- Click Start. The Console should print "Float DOWN — contact open, tank not full" repeatedly — this is the switch's default state (open, matching a real normally-open float switch at rest with no liquid lifting it).
- While the script is still running, click directly on the Float Switch Sensor's own body on the Canvas (not one of its 2 pins). Its floating ball shifts position and its status text flips to CLOSED — and on the very next poll, the Console switches to printing "Float UP — contact closed, tank full". Click it again to flip back.
- This click-to-toggle works at any time — before Start, or live while the simulation is running — exactly like the Tact Switch's own press. Unlike a real mechanical float, nothing here "resets" on its own: it stays in whichever state you last set it until you click it again.
What “working correctly” looks like
- At rest (never clicked), the switch reads OPEN and GPIO.input() returns 1 — the pull-up resistor holds the pin HIGH with nothing shorting it.
- Clicking the switch's body flips its own visual (the floating ball's position and its OPEN/CLOSED status text) immediately, and the very next GPIO.input() read reflects it — CLOSED reads 0 (the switch's own 0Ω contact overrides the 10kΩ pull-up), OPEN reads 1.
- Toggling works identically whether the simulation is stopped or running — no need to click Stop first to change the float's simulated position.
- The state persists across multiple reads without being clicked again — this is a toggle, not a momentary press like the Tact Switch's own button.
If something’s wrong
- GPIO.input() always reads 1 (HIGH), even after clicking the switch → make sure you clicked directly on the switch's own body, not one of its 2 pin markers — the pins are small targets near the bottom edge and easy to miss on a first click.
- GPIO.input() always reads 0 (LOW), even when the switch's own status text says OPEN → double-check the Resistor's other leg actually reaches a real Pi 3.3V pin (not left dangling, and not accidentally wired to GND) — a pull-up with no real 3.3V source behind it can't hold the line HIGH.
- Nothing changes when clicked at all → confirm GND is genuinely wired back to the same Pi ground the pull-up's own reference uses — an isolated GND leaves the whole net with no defined state regardless of the switch's own position.
- Wanted a continuous water-level reading instead of just full/not-full → this component is deliberately digital-only, matching a real mechanical float switch's own fixed trip point — for a continuous analog reading, a different sensor (with its own real analog output) would be needed; this simulator doesn't model one for float switches specifically.