The problem addressed in this project may look silly at first, but it isn’t. Of course, at home you don’t really need a no-more-toilet-paper detector, but what about a large building with several toilets? As an example, think of an airport. There are dozens if not hundreds of toilets sprinkled over a very large area. Some toilets are used more than others while usage fluctuates during the day. Therefore, keeping track of all toilets is not an easy task at all.IoT to the Rescue
Fitting a toilet paper detector in every toilet allows monitoring them all from a single place. Now you can service only the toilets that need servicing instead of having a maintenance team running around checking every toilet irrespective of if it needs servicing or not.Measuring Remaining Toilet Paper
Several techniques can be imagined for measuring the amount of toilet paper left in a dispenser. You could e.g. keep track of the weight of the dispenser or count roll rotations or count toilet users and apply some statistics. Installing a camera in a toilet cannot be done as it is probably forbidden in every country in the world. Let’s hope it stays that way.
The solution I came up with was to use a short-range time-of-flight (ToF) sensor to measure the thickness of the layer of paper left on the tube. These sensors are low-cost and can measure distances with millimeter precision, which is much better than we need in this application.Time-of-Flight Sensor
A time-of-flight sensor sends out light pulses and measures the time it takes for the pulse to be reflected from an object back to the sensor. It is a kind of LiDAR. The distance to the object can be calculated from the time it took the pulse to travel to the object and back again.
If we mount a ToF sensor at a short distance from a toilet paper dispenser, we can keep track of the amount of paper left on the roll. When the dispenser holds a new roll, the distance to the sensor will be small. This distance will increase steadily as the paper is being consumed until all of it is gone and only the tube is left.
The tube has an inner diameter of 4 cm or 1.6″. The thickness of the paper layer rolled on the tube can vary from about 20 cm down to 3 cm for a common household roll with a 10 cm diameter. These thickness, distance and diameter values are all in the range of a ToF sensor.Wireless Connectivity and MQTT Support
By adding Wi-Fi connectivity to the sensor, the toilet paper detector can use e.g. MQTT to send the status to a central monitoring station, cloud-based or not. For the Wi-Fi connectivity and MQTT protocol, we will use the WizFi360-EVB-Pico board. This board is equipped with a WizFi360 Wi-Fi module that speaks MQTT. It also features cloud connectivity, making it a perfect solution. It has a Raspberry Pi RP2040 microcontroller which is fully supported by the popular Arduino programming environment thanks to the boards package by Earl F. Philhower, III.
A popular ToF sensor is the VL53L0X from STmicroelectronics which is easily found online mounted on a small module. It communicates over an I²C bus and open-source software support for it is available in the shape of for instance Arduino and Raspberry Pi libraries. The sensor needs only two GPIO ports of a microcontroller, the I²C bus, and a 3V3 power supply.System Design
The RP2040 microcontroller on the WizFi360-EVB-Pico board features two I²C peripherals that can be mapped to several pins. Convenient for me was bus I²C0 on GPIO8 (SDA) and GPIO9 (SCL) as there is a ground pin next to GPIO9. By using GPIO10 for the sensor’s 3V3 power supply, the VL53L0X module can be mounted with minimal effort on the board. The schematic is shown below.
I used the open-source DFRobot_VL53L0X Arduino library to talk to the sensor.
The WizFi360 module is connected to a serial port of the RP2040, which maps to Serial2 in Arduino. It is controlled by sending it AT commands. Only three commands (with parameters) are needed to make it connect to a Wi-Fi network in DHCP mode:
The WizFi360 module also supports MQTT and again only three commands (with parameters) are required to make it connect to an MQTT broker and configure a topic:
From this point on the toilet paper detector can periodically publish its state to the MQTT broker with the command:
Where value is the distance to the toilet paper roll in millimeters.Visual Status Feedback
As a visual status indicator, I used the on-board green LED connected to GPIO25. When toilet paper is plentiful, it will flash only once every time an MQTT message is published. When the measured distance becomes too large, i.e. when we’re running out of paper, it flashes four times for every MQTT message published.
The LED is also used to indicate system status. When everything is working fine, it will only flash briefly when publishing a message. If the LED flashes continuously, a serious problem has been encountered:
- Bursts of three flashes: WizFi360 module not found.
- Bursts of four flashes: no Wi-Fi connection.
- Bursts of five flashes: no MQTT connection.
The system can sleep most of the time to save energy and wake up only every five minutes or so as there is no point in being overactive. In some cases, it might be convenient to use the toilet light or a presence detector to switch on the detector only when needed.Summarizing
With the help of a WIZnet WizFi360-EVB-Pico board featuring Wi-Fi connectivity and MQTT and cloud support, a VL53L0X time-of-flight (ToF) sensor module and the Arduino programming environment one can quickly build a practical IoT sensor that can measure short distances and detect objects and transmits its status using MQTT. This protocol is supported by most home automation systems and IoT cloud providers, making it easy to create a dashboard for system status monitoring.
In this project we used the system to detect the quantity of toilet paper remaining on a roll in a toilet. Such a system is very practical in large buildings with many toilets like airports and conference centers. However, the system described above can be useful in many more applications, to name a few:
- Container or (waste) bin empty/full detection, practical for stock keeping or maintenance scheduling.
- Object detection like a car driving up to a gate.
- Entry/exit detection and counting.
- Door or window open or closed detection.
Applications are only limited by your imagination.