Build it step by step
0 of 7 doneTest the sensor
Print the distance to the serial monitor.
Calibrate
Note the distance with the bottle empty and full.
Convert to millilitres
Print the volume.
Detect drinking
Count only falls in level.
Add the glow
Light the LED if you are behind after half an hour.
Build the cap
Make a sealed compartment in the lid.
Wear it for a day
Compare the totals with what you drank.
How do you build a smart water bottle?
A bottle cap with a ToF sensor measures water level and reminds you to drink with a gentle glow. You need Seeed XIAO ESP32C3, VL53L0X time-of-flight distance sensor, WS2812B single LED module (8 mm / breakout) and Li-Po battery 3.7 V 500 mAh. It takes 7 steps and about 180 minutes.
- Level
- Intermediate
- Time
- About 180 min
- Parts
- 4
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| VL53L0X VIN and GND | XIAO 3V3 and GND | Power |
| VL53L0X SDA | XIAO D4 | I2C data |
| VL53L0X SCL | XIAO D5 | I2C clock |
| LED module VCC, GND | XIAO 3V3, GND | Power |
| LED module DIN | XIAO D1 | Data |
| LiPo battery | XIAO BAT+ and BAT− pads | Power and charging by USB-C |
Code
The sensor in the cap points down at the water surface and reports the distance, which maps to a volume by simple proportion. Measure the distance with the bottle empty and full and enter them as EMPTY_MM and FULL_MM, because bottle shapes differ. Readings are only valid when the bottle is upright and still, so the sketch samples every 2 seconds and ignores big jumps up, which are refills. Keep the electronics sealed from water and food-safe: use a dry compartment with a clear lid and never let any part touch the drink, and do not put the electronics in a dishwasher. The daily target here is only an example, because the right amount of water varies with the person, climate and health, so ask a doctor if you have a medical reason to track it. Use Seeed XIAO ESP32C3 in the Arduino IDE.
Runs on a virtual Seeed XIAO ESP32-C3 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Smart bottle: measure the water level from the cap and glow if you are behind. Libraries: Adafruit VL53L0X, Adafruit NeoPixel.
#include <Wire.h>
#include <Adafruit_VL53L0X.h>
#include <Adafruit_NeoPixel.h>
Adafruit_VL53L0X tof;
Adafruit_NeoPixel led(1, D1, NEO_GRB + NEO_KHZ800);
const int EMPTY_MM = 190; // distance to the bottom when the bottle is empty (measure yours)
const int FULL_MM = 30; // distance when full
const float BOTTLE_ML = 750.0;
const unsigned long HOUR_MS = 3600000UL;
const float GOAL_ML_PER_HOUR = 150.0; // 3 litres over 20 waking hours
float lastMl = -1, drunkThisHour = 0;
unsigned long hourStart = 0, lastMeasure = 0;
float levelMl() {
VL53L0X_RangingMeasurementData_t m;
tof.rangingTest(&m, false);
if (m.RangeStatus == 4) return -1; // out of range
int d = constrain((int)m.RangeMilliMeter, FULL_MM, EMPTY_MM);
return BOTTLE_ML * (EMPTY_MM - d) / (float)(EMPTY_MM - FULL_MM);
}
void glow(byte r, byte g, byte b) { led.setPixelColor(0, led.Color(r, g, b)); led.show(); }
void setup() {
Serial.begin(115200);
Wire.begin(D4, D5);
led.begin(); led.setBrightness(60);
if (!tof.begin()) { Serial.println("Sensor not found"); while (1); }
hourStart = millis();
}
void loop() {
if (millis() - lastMeasure > 2000) { // measure every 2 seconds, with the bottle upright and still
lastMeasure = millis();
float ml = levelMl();
if (ml >= 0) {
if (lastMl >= 0 && ml < lastMl - 5) drunkThisHour += lastMl - ml; // level fell: drank, ignore refills
lastMl = ml;
}
}
if (millis() - hourStart > HOUR_MS) { drunkThisHour = 0; hourStart = millis(); }
bool behind = drunkThisHour < GOAL_ML_PER_HOUR * ((millis() - hourStart) / (float)HOUR_MS);
if (behind && millis() - hourStart > HOUR_MS / 2) glow(0, 40, 60); // gentle blue-green glow after half the hour
else glow(0, 0, 0);
delay(100);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Seeed XIAO ESP32-C3 with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Sensor not found
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Full bottle | About 750 ml shown |
| Half full | About 375 ml |
| Drink 200 ml | Counted within about 10 percent |
| Refill | Not counted as drinking |
If it does not work
Reading jumps
Hold the bottle upright and average several readings.
Always out of range
Check the sensor faces the water without a wall in the way.
Wrong volume
Recalibrate EMPTY_MM and FULL_MM.
LED never glows
Check the data pin and the half-hour condition.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the smart water bottle?
Seeed XIAO ESP32C3, VL53L0X time-of-flight distance sensor, WS2812B single LED module (8 mm / breakout) and Li-Po battery 3.7 V 500 mAh.
How long does the smart water bottle take to build?
About 180 minutes for a intermediate maker, in 7 steps.
How do I know the smart water bottle works?
Full bottle: About 750 ml shown. Half full: About 375 ml. Drink 200 ml: Counted within about 10 percent.
Reading jumps: what should I check?
Hold the bottle upright and average several readings.
Always out of range: what should I check?
Check the sensor faces the water without a wall in the way.
Wrong volume: what should I check?
Recalibrate EMPTY_MM and FULL_MM.
LED never glows: what should I check?
Check the data pin and the half-hour condition.
