Skip to content

Project 1.8.18: Online Tank Level Chart

Beginner Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython

Pico 2 W Diagram

Pico 2 W Pinout


Overview

Build a water tank monitor that shows current tank level and a short recent history chart on a local web page.

This project demonstrates ADC calibration and a simple browser-based bar chart.

The final result should show the current level as a percentage, a visual tank graphic, and a recent history display.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Water level sensor
Water level sensor
Breadboard
Breadboard
Jumper wires
Jumper wires

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
Water sensor VCC 3.3V Physical pin 36
Water sensor GND GND Physical pin 38
Water sensor AOUT GPIO 26 GPIO 26 / physical pin 31 ADC input

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2 W

Step 1

Step 2: Place the Water Level Sensor

Keep the Pico, breadboard, USB cable, and jumper wires away from the water container.

Step 2

Step 3: Connect Water Sensor VCC

Connect water sensor VCC to 3.3V.

Step 3

Step 4: Connect Water Sensor GND

Connect water sensor GND to GND.

Step 4

Step 5: Connect Water Sensor Signal

Connect water sensor AOUT to GPIO 26.

Step 5


Testing Individual Components

Water Level ADC Test

from machine import ADC, Pin
import time

adc = ADC(Pin(26))

while True:
    print(adc.read_u16())
    time.sleep(0.5)

Full Project Code

import network
import socket
import time
from machine import ADC, Pin

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'

adc = ADC(Pin(26))
DRY = 2000
WET = 55000
history = []
MAX_HISTORY = 15

def get_level():
    raw = adc.read_u16()
    if raw >= WET:
        return raw, 100
    if raw <= DRY:
        return raw, 0
    return raw, int((raw - DRY) / (WET - DRY) * 100)

def web_page(level, hist):
    bars = ''
    for value in hist:
        bars += '<div style="background:#4fc3f7;width:{}%;height:24px;margin:3px 0;">{}%</div>'.format(value, value)
    return '''<!DOCTYPE html>
<html>
<head>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <meta http-equiv='refresh' content='3'>
    <title>Tank Level Chart</title>
</head>
<body style='font-family:Arial;padding:20px'>
    <h1 style='text-align:center'>Online Tank Level Chart</h1>
    <h2 style='text-align:center'>{}%</h2>
    <div style='height:200px;width:100px;border:3px solid #333;border-radius:0 0 18px 18px;margin:20px auto;position:relative;overflow:hidden;background:#f5f5f5;'>
        <div style='position:absolute;bottom:0;width:100%;height:{}%;background:#4fc3f7'></div>
    </div>
    <h3>Recent History</h3>
    {}
</body>
</html>'''.format(level, level, bars)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

print('Connecting to Wi-Fi...')
for _ in range(15):
    if wlan.isconnected():
        break
    time.sleep(1)

if not wlan.isconnected():
    raise RuntimeError('Wi-Fi connection failed')

ip_address = wlan.ifconfig()[0]
print('Connected. Open http://{} in your browser'.format(ip_address))

address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)

while True:
    client, client_address = server.accept()
    client.recv(1024)
    raw, level = get_level()
    history.append(level)
    if len(history) > MAX_HISTORY:
        history.pop(0)
    response = web_page(level, history)
    client.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n'.encode())
    client.sendall(response.encode())
    client.close()

How the Code Works

Code Section What It Does Why It Matters
Calibration values Store dry and wet sensor readings Converts raw values into percentage
get_level() Converts ADC reading into tank level Browser chart uses percentage
history list Stores recent tank levels Shows recent changes
Bar chart HTML Builds a visual history Makes changes easier to see

Expected Result

The browser page shows current tank level, a tank fill graphic, and recent history bars. Changing the water level updates the display.


Troubleshooting

Problem Possible Cause Solution
Always shows 0% or 100% Calibration values do not match sensor Print raw values and adjust DRY and WET
History never changes Level reading stays the same Change the water level and wait for refreshes
Water is too near electronics Unsafe workspace setup Move Pico and breadboard farther from container