Arduino UNO R4 WiFi with HC-SR04 ultrasonic sensor for real-time water level monitoring and web dashboard

Water Level Indicator Project: Real-Time Monitoring

The 3 AM Panic That Started Everything

My water tank overflowed at 3 AM. Again.

Water cascading down three floors, flooded my neighbor’s balcony, and I woke up to angry knocking. Standing there in soggy slippers, I realized something: we’re monitoring our smartphones’ battery levels obsessively, but ignoring thousands of liters of water above our heads.

That night, I built my first water level indicator. It wasn’t prettyโ€”loose wires, a breadboard taped to the tank lid, and code held together with hope. But it worked.

Two years later, I’ve deployed these systems in residential buildings and agricultural fields. This isn’t theory from a textbook. This is what actually works when water matters.

What started as a midnight frustration became a refined system tested in 30+ real-world installations. Here’s exactly how to build one that won’t fail you.

This is a practical IoT (Internet of Things) project that combines sensors, microcontrollers, and WiFi connectivity. New to IoT? Start with our complete IoT basics guide to understand the fundamentals.


Practice Small, Deploy Big: The Smart Way to Build

Here’s what confuses most beginners: This tutorial describes a real 1,000-liter rooftop water tank system. But you shouldn’t start there.

The Two-Phase Approach

Phase 1: Learn with a Small Container (This Weekend)

  • Use: Plastic bucket, food container, or flower pot (15cm depth works perfectly)
  • Why: Test wiring, understand code, fix mistakes on your deskโ€”not on a rooftop
  • Time: 2-3 hours from unboxing to working dashboard
  • Risk: Zero. If something breaks, it’s a โ‚น55 sensor, not a flooded house

Phase 2: Deploy on Real Tank (Next Weekend)

  • Use: Your actual water tank (100-2,000 liter capacity)
  • Why: You’ve already debugged wiring, WiFi, and code
  • Time: 1 hour mounting and calibration
  • Confidence: High. You know exactly how it works

Why the Code Shows “15cm”

You’ll notice the code has these values:

This is intentional. These are practice values for a 15cm deep container.

For your real tank: You’ll replace them during calibration (detailed in the Calibration section below). My actual rooftop tank uses:

The Practice Container Setup

I recommend: A 15cm deep food container. Why? Transparent sides let you see water level visually while sensor measures it. Perfect for learning.

Testing vs Real Deployment: What Changes

AspectPractice Container (15cm)Real Tank (150-200cm)
SensorHC-SR04 (โ‚น55)JSN-SR04T waterproof (โ‚น280)
MountingTape sensor to container lidPVC bracket, weatherproof
PowerUSB cable from laptop5V adapter, weatherproof box
WiringBreadboard jumper wiresSoldered connections
CalibrationSENSOR_OFFSET=2, MAX_WATER=13SENSOR_OFFSET=15, MAX_WATER=185

The Learning Path

Test with a container first (2-3 hours), then deploy on tank (1 hour). This saves hours of rooftop debugging.

I’ve taught 15 people to build this. The 8 who practiced with containers first succeeded in 4-5 hours total. The 7 who went straight to tanks took 8-12 hours and 3 gave up temporarily.

The main point

This tutorial shows my real 185cm deep tank system. The code starts with 15cm values because that’s your training wheels.

This weekend: Test with whatever 15cm deep container you have.

Next weekend: Deploy on your actual tank with confidence.

Both use identical hardware and code. Only the calibration values change.


Watch the Complete Video Tutorial

Before diving into the technical details, watch this step-by-step video guide:

What’s covered:

  • Complete wiring demonstration
  • Arduino IDE setup and code upload
  • Live dashboard testing with water
  • Budget alternative (Nano + ESP-01)

Quick navigation:

0:49 – Wiring | 2:11 – Code Upload | 3:46 – Testing | 5:44 – Budget Option

๐Ÿ’ก Prefer reading? The complete written guide continues below.


Why Your Water Tank Needs a Brain

Here’s what happened before I automated mine:

  • Tank overflow: 3-4 times monthly (wasted ~2,000 liters)
  • Manual checks: Climbing to the terrace daily
  • No alerts: Discovered issues only after damage was done

After automation:

  • Zero overflows in 18 months
  • Remote monitoring from my phone while traveling
  • Real-time alerts when levels hit thresholds

This is what IoT (Internet of Things) looks like in real lifeโ€”sensors, connectivity, and automation working together. Learn more about how IoT works and its applications.

The math is brutal: A 1,000-liter tank overflowing twice monthly wastes 24,000 liters yearly. That’s โ‚น600-1,200 down the drain, literally.

HC-SR04 ultrasonic sensor connected to Arduino UNO R4 WiFi via four jumper wiresโ€”this simple wiring setup powers the entire water level monitoring system


The HC-SR04 Reality Check: What They Don’t Tell You

Everyone recommends the HC-SR04. But here’s what 50+ installations taught me:

What Actually Works

Distance Sweet Spot: 10-300 cm is advertised. Reality? 20-250 cm is reliable.

I mounted one at 5 cm from the water surface once. Readings went haywireโ€”jumping between 3 cm and 187 cm randomly. The sensor needs breathing room. Now I never go below 15 cm minimum distance.

Temperature Drama: My rooftop tank in May (45ยฐC ambient) showed readings 8% off compared to January. Sound speed changes with temperature. The fix? I added a temperature correction factor:

Suddenly, accuracy jumped from ยฑ5 cm to ยฑ1.5 cm across seasons.

Mounting Position Matters More Than Code

I’ve tested six mounting configurations:

PositionAccuracyIssue
Dead center, perpendicular to waterยฑ1.5 cmPerfect (use this)
15ยฐ angleยฑ4 cmReflections bounce off sidewalls
Near tank wall (<5 cm)ยฑ8 cmWall echoes confuse sensor
Above agitated waterยฑ12 cmWaves scatter ultrasonic beam

Pro tip: Use a PVC pipe bracket to ensure the sensor points straight down. I use a laser level during installationโ€”sounds excessive, but 2ยฐ misalignment = 7 cm error at 2-meter depth.


Building the System: Arduino R4 WiFi Setup

Why Arduino UNO R4 WiFi?

After building 30+ installations, I standardized on the Arduino UNO R4 WiFi. Here’s why (see complete microcontroller comparison guide for other options):

  • Built-in WiFi: No external modules, no flaky ESP-01 connections
  • Rock-solid stability: I’ve had zero random crashes in 18 months
  • 5V logic: Direct HC-SR04 compatibility, no level shifters
  • Better support: Official Arduino libraries just work

Yes, it’s โ‚น2,200 vs โ‚น350 for alternatives. But I’ve saved 15+ hours of debugging per project. Time is money.

Core Components (Tested Over 500+ Days)

Essential Parts:

  • Arduino UNO R4 WiFi: โ‚น2,200
  • HC-SR04 Ultrasonic Sensor: โ‚น55 (indoor) OR JSN-SR04T: โ‚น280 (outdoor/waterproof)
  • Jumper Wires: โ‚น40
  • 5V 2A Power Adapter: โ‚น150
  • Enclosure (waterproof box): โ‚น120

Total: โ‚น2,565


Wiring Guide: Pin Connections & Critical Mistakes

Arduino UNO R4 WiFi board connected to HC-SR04 ultrasonic sensor with red black yellow and brown jumper wires on yellow background
HC-SR04 ultrasonic sensor connected to Arduino UNO R4 WiFi via four jumper wiresโ€”this simple wiring setup powers the entire water level monitoring system

Pin Connections (HC-SR04 to Arduino R4 WiFi)

HC-SR04 PinArduino R4 WiFi PinWire Color (Recommended)Function
VCC5VRedPower supply
GNDGNDBlackGround
TRIGDigital Pin 9YellowTrigger pulse
ECHODigital Pin 8Blue/BrownEcho response

Add 100ยตF Capacitor (Critical)

Solder it across VCC and GND at the sensor. Without it, readings jump ยฑ18cm. With it, stable at ยฑ1.2cm. Costs โ‚น5, takes 2 minutes, eliminates 90% of erratic readings from pump motor noise.

Three Common Mistakes

1. Using 3.3V Pin

Never use 3.3Vโ€”sensor fails beyond 85cm. Always connect VCC to the 5V pin near USB port. At 3.3V, it works initially but dies when water drops below 80cm.

2. Wires Over 30cm Long

I used 80cm wiresโ€”readings fluctuated ยฑ8cm. Shortened to 25cm, dropped to ยฑ1.5cm. Long wires pick up motor noise. Keep sensor wires short, use USB extension for power if needed.

3. Swapped TRIG/ECHO

If Serial Monitor shows constant “0 cm,” swap Pin 9 and Pin 8 wires. Both pins look identical, easy mistake in poor light.

Power Supply

Old phone chargers failโ€”they drop to 4.6V under load. Sensor needs 4.8V minimum. Use 5V 2A adapter (โ‚น150) or quality power bank.

Test Before Installing

Hold sensor 50cm over bucket, should read ~48-52cm. Raise to 100cm, should read ~98-102cm. If stuck at zero, fix wiring on deskโ€”not on rooftop at midnight.

Permanent Setup

Testing: Use jumper wires. Rooftop: Solder to sensor, screw terminals on Arduino. Pump vibration loosens headers over months.


Before You Upload: WiFi Setup & Dashboard Test

Step 1: Configure Your WiFi Credentials

Remember before copying the code:

Find these lines near the top of the code and change them:

Change to YOUR actual WiFi details:

โš ๏ธ Important WiFi Requirements:

  • WiFi name is case-sensitive – “MyWiFi” is different from “mywifi”
  • Use 2.4GHz WiFi only – Arduino R4 doesn’t support 5GHz networks
  • Avoid special characters in WiFi name if possible (like @, #, %, &)
  • No spaces at beginning/end of WiFi name

Step 2: Upload Code & Check Serial Monitor

Just after uploading code:

  1. Open Serial Monitor (Tools โ†’ Serial Monitor or Ctrl+Shift+M)
  2. Set baud rate to 115200 (bottom-right dropdown)
  3. Wait 10-15 seconds – Serial Monitor takes time to connect and show output

โœ… Expected Serial Monitor Output:

If you see this output โœ… WiFi is working! Continue to Step 3.

If you see “โœ— WiFi Failed!” โŒ Check:

  • WiFi name spelled exactly right (case matters)
  • Password is correct
  • Router has 2.4GHz band enabled
  • Arduino is within WiFi range

Step 3: Test Dashboard (Sensor Connection Options)

Water level indicator project dashboard at 192.168.1.150 showing Distance 0cm, Water Level 0cm with red EMPTY status, Percentage 0%, empty tank visualization, and green live update indicator on purple gradient background
Water level indicator project real-time dashboard displays three metrics: Distance (0cm), Water Level (0cm with red EMPTY indicator), and Percentage (0%). The tank visualization shows empty state with live updates every second (green pulsing dot). These zero readings confirm WiFi is working correctly during testing phaseโ€”sensor calibration adds accurate values later.

Why test dashboard first? This separates WiFi problems from wiring problems. If dashboard loads, you know WiFi works correctly.

How to access dashboard:

  1. Open any web browser on PC or Android mobile
  2. Make sure your device is connected to the SAME WiFi (in this example: “raj”)
  3. Type in address bar: http://192.168.1.150
  4. Press Enter

iPhone/iPad Not Working? Quick Fixes

Fix 1: Disable Private WiFi Address (Most Common)

iPhone/iPad blocks local device access by default.

Steps:

  1. Settings โ†’ WiFi โ†’ Tap (i) next to network name
  2. Toggle OFF “Private WiFi Address”
  3. Reconnect to WiFi
  4. Try dashboard again

โœ… This fixes 90% of iPhone issues


Fix 2: Router AP Isolation Blocking Devices

If NO devices can talk to Arduino, router is blocking them.

Check router settings:

  1. Open router admin page: 192.168.1.1 or 192.168.0.1
  2. Find Wireless Settings โ†’ Advanced
  3. Disable “AP Isolation” or “Client Isolation”
  4. Save and reboot router

Common router locations:

  • TP-Link: Wireless โ†’ Wireless Settings โ†’ Uncheck “AP Isolation”
  • D-Link: Setup โ†’ Wireless โ†’ Advanced โ†’ Disable Isolation
  • Airtel/Jio: Network โ†’ WLAN โ†’ Disable “Client Isolation”

What You’ll See on Dashboard

Option A: Testing WITHOUT Sensor Connected

Dashboard shows all zeros when sensor isn’t connected – this is normal!

Expected readings:

  • Distance: 0 cm
  • Water Level: 0 cm with “EMPTY” status
  • Percentage: 0%
  • Tank visualization: Empty (gray box)

โœ… This is CORRECT! The dashboard is working. Sensor values will appear after you connect the HC-SR04.


Option B: Testing WITH Sensor Already Connected

If you already connected the sensor before testing dashboard, you’ll see actual distance readings instead of zeros.

Expected readings:

  • Distance: 45-200 cm (depends on sensor height above surface)
  • Water Level: May show some value (not necessarily “EMPTY”)
  • Percentage: 20-80% (calculated from distance)
  • Tank visualization: Partially filled

โš ๏ธ Important: This doesn’t mean there’s water!

The sensor is measuring the distance to the nearest surface:

  • If mounted over empty container: Measures distance to container bottom
  • If held in air: Measures distance to desk/floor/hand
  • If pointed at wall: Measures distance to wall

Example:

  • Sensor mounted 50cm above empty container bottom
  • Dashboard shows: Distance 50cm, Water Level 35cm, 70% full
  • This is normal! You’ll calibrate actual values in the Calibration section

What matters now: Dashboard is loading and updating values in real-time. This confirms WiFi and web server are working correctly.

The Code: Production-Ready for Arduino R4 WiFi

Here’s the exact code running on my rooftop right now. It’s survived power cuts, WiFi drops, and a curious crow that unplugged it twice.


What Makes This Code Different

1. Median Filter = Stable Readings

Instead of one measurement, it takes 5 readings and picks the middle value. This eliminates random spikes from electrical noise or reflections.

Before filtering: 45cm, 127cm, 38cm, 156cm, 42cm
After filtering: 42cm (accurate)

2. WiFi Auto-Reconnect

Most tutorials skip this. When WiFi drops (and it will), the code automatically reconnects within 5 seconds. Uptime went from 70% to 99.2%.


The Sensor Showdown: Real Testing Data

I tested five sensor types over 90 days in identical conditions. Here’s what actually survived:

Sensor TypeCostAccuracyReliabilityOutdoor LifespanVerdict
HC-SR04โ‚น55ยฑ3.2 cm87% uptime9 monthsGood for indoor/covered
JSN-SR04Tโ‚น280ยฑ2.1 cm98% uptime28+ monthsBest for outdoor
Float Switchโ‚น120N/A (on/off)92% uptime11 monthsMotor control only
Pressure (MS5803)โ‚น950ยฑ0.8 cm96% uptime22+ monthsOverkill for tanks
Capacitive DIYโ‚น40ยฑ8.5 cm71% uptimeFailed at day 34Experiment only

JSN-SR04T Winner: Took direct monsoon rain, temperature swings (12ยฐC to 46ยฐC), and a crow attack. Still running at ยฑ2 cm accuracy after 28 months.


Problems Nobody Talks About (And Real Fixes)

Problem 1: Readings Jump Around Wildly

Symptom: 45 cm, 127 cm, 38 cm, 156 cm in consecutive readings.

3-Part Fix:

  1. Hardware: Add 100ยตF capacitor across sensor VCC and GND
  2. Software: Use median filter (already in code above)
  3. Power: Use quality 5V 2A adapter, not random phone chargers

Result: Standard deviation dropped from 18 cm to 1.2 cm.

Problem 2: WiFi Randomly Disconnects

Symptom: Dashboard works for 2 hours, then stops updating.

Fix: WiFi status check in loop (already in code above). Reconnects within 5 seconds.

Before: 70% uptime
After: 99.2% uptime over 18 months

Problem 3: Web Page Loads Too Slowly

Symptom: Dashboard takes 8-12 seconds to load.

Fix: Combine strings into fewer client.print() calls (implemented in code above).

Result: Load time dropped to under 2 seconds.


Calibration: The 5-Minute Process

Out of the box, sensors aren’t accurate. Here’s how I calibrate every installation.

Step-by-Step

1. Empty Container/Tank Measurement

Empty your container (or tank) completely. Note the sensor reading.
Example: Sensor shows 200 cm (for tank) or 15 cm (for practice container)

2. Full Container/Tank Measurement

Fill to maximum. Note the reading.
Example: Sensor shows 15 cm (tank) or 2 cm (container)

3. Calculate Your Constants

For practice container (15cm depth):

For real tank (200cm depth):

4. Update Code

Replace these lines in the code:

5. Verify Mid-Point

Fill to exactly 50% (measure manually). System should report approximately half the MAX_WATER_LEVEL value. If off by more than 5 cm, recheck sensor mounting angle.


Cost Breakdown: What This Actually Costs

Basic Monitoring Setup

ComponentApprox. Price (โ‚น)Notes
Arduino UNO R4 WiFi~1,350Core brain
HC-SR04 Sensor~75Indoor use
Jumper Wires (40pcs)~40Quality matters
USB Cable~60For programming
5V 2A Adapter~150Reliable power
Small Enclosure~120Weatherproofing
TotalApprox. โ‚น1800

Outdoor/Waterproof Setup

ComponentApprox. Price (โ‚น)Notes
Arduino UNO R4 WiFi~1,350Core brain
JSN-SR04T (waterproof)~280Outdoor rated
Jumper Wires~40Quality matters
USB Cable~60For programming
5V 2A Adapter~150Reliable power
IP65 Junction Box~180Weatherproof enclosure
Cable Glands~60Seal wire entry points
TotalApprox. โ‚น2,200

Your Build Checklist

Ready to build? Follow this exact sequence (learned through 30+ builds):

Phase 1: Bench Testing (Day 1 โ€“ 1 hour)

  •  Connect HC-SR04 to Arduino R4 WiFi (TRIGโ†’Pin 9, ECHOโ†’Pin 8, VCCโ†’5V, GNDโ†’GND)
  •  Upload basic code (remove WiFi parts first, just Serial output)
  •  Test with Serial Monitorโ€”hold sensor over bucket, verify readings
  •  Readings stable ยฑ2 cm? Move to Phase 2

Phase 2: WiFi Setup (Day 1 โ€“ 30 minutes)

  •  Add WiFi credentials to code
  •  Set static IP matching your network (change 192.168.1.150 to match your router’s range)
  •  Upload and open Serial Monitor
  •  Note the IP address displayed
  •  Open browser, navigate to that IP
  •  Dashboard loads? Move to Phase 3

Phase 3: Tank Installation (Day 2 โ€“ 1 hour)

  •  Mount sensor on tank lid (center, pointing straight down)
  •  Use PVC pipe or 3D printed bracket for stability
  •  Measure empty tank reading
  •  Fill tank, measure full reading
  •  Calculate and update SENSOR_OFFSET and MAX_WATER_LEVEL in code
  •  Re-upload code
  •  Verify accuracy at 25%, 50%, 75% levels

Phase 4: Weatherproofing (Day 2 โ€“ 30 minutes)

  •  Place Arduino in weatherproof enclosure
  •  Use cable glands for wire entry (prevents moisture)
  •  Silicone seal around sensor mounting (if needed)
  •  Test everything still works after enclosure

Total active time: 3-4 hours spread over 2 days


Troubleshooting: When Things Go Wrong

Issue: “WiFi Failed! Check credentials” in Serial Monitor

Cause: Wrong SSID/password or router out of range

Fix:

  • Double-check WiFi name (case-sensitive!)
  • Verify password
  • Move Arduino closer to router temporarily
  • Check if router is on 2.4GHz (Arduino R4 WiFi doesn’t support 5GHz)

Issue: Sensor always reads 0 cm

Cause: Wiring issue or dead sensor

Fix:

  • Verify VCC connected to 5V (not 3.3V)
  • Check all four connections are solid
  • Swap TRIG and ECHO if accidentally reversed
  • Test with a different HC-SR04 to rule out sensor failure

Issue: Dashboard shows but doesn’t update

Cause: Auto-refresh not working or browser caching

Fix:

  • Hard refresh browser (Ctrl+Shift+R)
  • Check Serial Monitorโ€”are new readings coming in?
  • Try different browser (Chrome works best)
  • Clear browser cache

Issue: Readings fluctuate wildly (ยฑ20 cm)

Cause: Electrical noise, poor mounting, or obstacles

Fix:

  • Add 100ยตF capacitor across sensor VCC/GND
  • Ensure sensor points straight down (check with level)
  • Remove any obstacles in sensor’s path
  • Use shielded cable if near motor/pump

Final Word: Just Build It

Financial ROI: Preventing two overflows pays for the entire system.

Real ROI: Peace of mind when traveling, no more terrace climbs at midnight, learning embedded systems hands-on.

I spent โ‚น2,625 and gained a skill that led to paid IoT consulting work. The best learning happens when you see “WiFi Connected!” in Serial Monitor for the first time.

Start simple: HC-SR04 + Arduino R4 WiFi + basic code. Test in a bucket this weekend.

Expand gradually: Outdoor sensor next month. Multi-tank setup when you’re confident.

Get stuck? Drop a comment below. I check daily and genuinely enjoy troubleshooting these builds. Share photos of your setupโ€”I’m maintaining a gallery of reader projects.

The tank overflow at 3 AM changed how I approach automation. What will your trigger be?

Now stop reading. Start building. Your water tank is waiting.

Frequently Asked Questions

Can I use this with ESP32 instead of Arduino R4 WiFi?

Yes, but you’ll need to modify the WiFi library from <WiFiS3.h> to <WiFi.h> and adjust pin numbers. Compare all microcontroller boards here to choose the best for your project.

How accurate is the HC-SR04 sensor?

In my testing: ยฑ3.2 cm for indoor use, ยฑ2.1 cm for JSN-SR04T waterproof variant.

Does this work with saltwater or dirty water?

Yes, ultrasonic sensors measure distance to surface, not water quality. They work with any liquid.

ย What’s the maximum WiFi range?

Arduino R4 WiFi works reliably up to 30 meters from router through walls, 50+ meters in open space.

Can I monitor multiple tanks with one Arduino?

Technically yes (Arduino has multiple digital pins), but I recommend separate Arduinos for reliability and easier troubleshooting.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *