Design of an IoT Pet Feeding System Based on STM32
Abstract
To address the problem of pet owners being unable to feed their pets on time due to busy schedules, this paper designs and implements an IoT pet feeding system based on an STM32 microcontroller. The system uses the STM32F103C8T6 as its main controller and builds a lightweight web server on an ESP01S WiFi module. It integrates a DS18B20 digital temperature sensor for ambient temperature monitoring, an HX711 weighing sensor for food weight detection, a JQ8900 voice module for audible reminders, and an SG90 servo as the actuator for automatic feeding. Through the web page, users can remotely check the feeding status, ambient temperature, and remaining food, and configure scheduled feeding, alarm reminders, and the temperature threshold. Test results show that all functions run stably, with temperature measurement error within ±0.5℃ and weighing error of about 3%, which meets the needs of home use.
Keywords: STM32; IoT; Pet Feeding; WiFi Communication; Sensors
1 Introduction
1.1 Background and Significance
With rapid socio-economic development and the accelerating pace of life, pets have become important members of many families. China now has more than 100 million pets, and the pet market continues to expand. However, modern working life keeps people away from home, and on-time feeding becomes a real problem — irregular feeding harms pet health and worries the owners.
The development of IoT technology offers a new solution: combining sensors, actuators, and network communication enables intelligent, remote management of the feeding process. The significance of this project lies in: (1) removing the time constraint of feeding — pets are fed on schedule even when owners work late or travel; (2) monitoring ambient temperature and prompting timely action in extreme weather; (3) tracking remaining food in real time; (4) improving user experience through voice reminders and a web interface.
1.2 Related Work
Smart pet feeders appeared earlier abroad and are relatively mature: commercial products such as PetSafe's Smart Feed already offer scheduled portion feeding, remote control, and camera monitoring, but at a relatively high price and usage threshold. In China, smart feeders have proliferated with the growth of IoT and the smart-home market, yet there is still room for improvement in functionality, stability, and cost-effectiveness, especially regarding localized services and personalized settings.
1.3 Main Work
This paper covers: overall system design, hardware circuit design and component selection, STM32 firmware development, construction of a lightweight web server on the ESP01S, and system testing and optimization.
2 Overall System Design
2.1 Requirements
- Automatic feeding: dispense food according to a preset time, a countdown, or a remote command;
- Temperature monitoring: track ambient temperature in real time and sound an alarm above the threshold;
- Weight detection: measure the food weight in the bowl and alert when it runs low;
- Voice reminder: play a voice message when food is taken or running low;
- Remote control: view status and trigger feeding from a web page;
- Local display: show time, weight, and feeding count on an OLED screen.
2.2 System Architecture
With the STM32F103C8T6 at its core, the system is organized into five parts: communication (ESP01S), sensing (DS18B20, HX711), actuation (SG90 servo), human–machine interaction (OLED, buttons, JQ8900 voice), and alarm (buzzer). The functional structure is shown in Figure 2-1.

Two feeding modes are supported. In automatic mode, feeding is triggered when a preset alarm time arrives or a countdown ends; in manual mode, the user triggers feeding via a local button or the web page. All trigger sources converge into a single feeding-control routine that also accumulates the feeding count, avoiding state inconsistency caused by independent checks.
2.3 Component Selection
| Module | Model | Key specs | Role |
|---|---|---|---|
| MCU | STM32F103C8T6 | Cortex-M3 / 72 MHz / 64 KB Flash / 20 KB RAM | Control core |
| WiFi | ESP01S | ESP8266, 802.11 b/g/n, built-in TCP/IP stack | AP hotspot + web server |
| Temperature | DS18B20 | ±0.5℃, −55 to +125℃, 1-Wire | Ambient monitoring |
| Weighing | HX711 + load cell | 24-bit ADC, gain 128/64/32 | Food weight detection |
| Servo | SG90 | 9 g, 0°–180°, PWM | Feeding actuator |
| Display | 0.96" OLED | 128×64, SSD1306, I2C | Local display |
| Voice | JQ8900 | MP3 decode, built-in 8Ω/3W amp | Voice broadcast |
| RTC | DS3231 | I2C, battery backup | Timekeeping across power loss |
3 Hardware Design
3.1 MCU Circuit and Resource Allocation
The STM32F103C8T6 is a 32-bit Cortex-M3 microcontroller with rich on-chip peripherals. Pin allocation in this system:
- USART1 (PA9/PA10): ESP01S WiFi module, commands and status data;
- USART2 (PA2/PA3): JQ8900 voice module, playback commands;
- TIM3 (PA0): PWM signal for the servo;
- TIM2: button debouncing;
- PB1/PB10: HX711 SCK and DOUT;
- PB5: DS18B20 1-Wire data line (4.7 kΩ pull-up to VCC required);
- PB8/PB9: OLED I2C interface (SCL/SDA);
- PB12: buzzer control.
One easily overlooked wiring point: the HX711 must be powered from a separate 5 V supply and share a common ground with the MCU; otherwise the weight readings jitter noticeably due to power interference.
3.2 Sensor Modules
(1) DS18B20 temperature sensor. A 1-Wire digital sensor with a range of −55 to +125℃ and ±0.5℃ accuracy between −10 and +85℃. A single data line carries the entire bidirectional communication, which greatly simplifies the circuit. The system monitors the feeding environment; when the temperature exceeds the threshold, the buzzer alarms to remind the owner.
(2) HX711 weighing module. The HX711 is a 24-bit ADC designed for weigh scales, with a programmable gain amplifier (128/64/32). It converts the weak millivolt-level differential signal of a strain-gauge load cell into digital form. This system uses 128× gain; after power-on warm-up, an unloaded calibration records the zero-point RAW value as the baseline for weight computation.
3.3 Communication Module
The ESP01S is based on the ESP8266EX with an integrated TCP/IP stack, supporting Station, SoftAP, and mixed modes. The system configures it in SoftAP mode: after connecting to the device's hotspot, a browser at 192.168.4.1 opens the control page — no external router is needed, so it works in any home.
Timekeeping uses a DS3231 high-accuracy RTC on the ESP01S's GPIO-bit-banged I2C bus. Backed by a coin cell, the DS3231 keeps time across power cycles, so the clock does not need to be reset at every boot.
3.4 Actuator and Interaction
The SG90 servo (9 g, 0°–180°) sits at the food outlet and opens/closes it by rotation. The 0.96-inch OLED (128×64, SSD1306) shows date/time, working mode, food weight, and feeding count. The JQ8900 voice module decodes MP3 files and accepts serial commands; its built-in 8Ω/3W amplifier drives a speaker directly for voice reminders. Four buttons allow local configuration of time, alarms, countdown, and the temperature threshold, and an active buzzer sounds the over-temperature alarm.
4 Software Design
4.1 Main Program Flow
The firmware is modular. As shown in Figure 4-1: after power-on, the OLED, buzzer, HX711, voice module, servo, DS18B20, and ESP01S are initialized in turn; the system then warms up until the sensors stabilize, performs unloaded calibration to obtain the weight zero point, and enters the main loop, which continuously polls temperature, weight, buttons, and communication, scheduling feeding, alarms, display, and voice actions.

4.2 HX711 Weighing and Digital Filtering
Reading the HX711 requires strict timing: pull SCK low, wait for DOUT to go low (data ready), clock out 24 bits, then send one extra pulse to set the next conversion's gain (128×).
Weight signals carry power-supply ripple and mechanical vibration noise, making the raw display jitter. A first-order low-pass filter smooths the samples:
Y(n) = α · X(n) + (1 − α) · Y(n−1)
where X(n) is the current sample and Y(n−1) the previous output. The coefficient α trades off response speed against smoothness; this system uses α = 0.8:
/* First-order low-pass filter: weight_raw = current sample,
weight_filtered = filtered output */
#define FILTER_ALPHA 0.8f
weight_filtered = FILTER_ALPHA * weight_raw
+ (1.0f - FILTER_ALPHA) * weight_filtered;
/* Zero tolerance: treat ±2 g as zero to avoid negative or jittery
readings when the bowl is empty */
if (weight_filtered > -2.0f && weight_filtered < 2.0f) {
weight_filtered = 0;
}
A zero-tolerance mechanism additionally treats readings within ±2 g as zero, preventing negative values when the bowl is empty.
4.3 Temperature Monitoring and Alarm
DS18B20 communication follows the 1-Wire protocol: the master sends a reset pulse and waits for the presence pulse; then issues the skip-ROM and convert-T commands; after conversion, it resets again, skips ROM, issues a read-scratchpad command, and parses the temperature from the 9 returned bytes. The default threshold is 31℃; when exceeded, the buzzer alarms and the web page shows a warning.
4.4 Servo Feeding Control
The SG90 is driven by a 20 ms PWM from TIM3, with a 0.5–2.5 ms pulse width mapped linearly to 0°–180°. A feeding action rotates the servo from 0° to 180° and back, opening and closing the food outlet once, then increments the feeding count. The rotation is stepped incrementally rather than jumping directly to the target angle: this keeps the dispensing smoother and allows an immediate response to a "stop manual feeding" command from the web page at any moment.
4.5 ESP01S Web Server
The STM32 configures the ESP01S via AT commands: communication test, SoftAP mode, hotspot name and password, multiple connections, and a TCP server listening on port 80. When a browser issues an HTTP request, the ESP01S serves a built-in control page whose form submissions forward parameters to the STM32.
Web features include: setting the system time (written to the DS3231), up to 3 alarms (scheduled feeding in automatic mode), a countdown, the temperature threshold, and manual feeding start/stop with the feeding count. A debug page also exposes key runtime parameters for remote troubleshooting.
5 Testing and Analysis
5.1 Functional Tests
| Test | Method | Result |
|---|---|---|
| Temperature | Compare DS18B20 with a reference thermometer at 15–35℃ | Error ≤ ±0.5℃; alarm triggers correctly |
| Weighing | Place a 100 g reference weight after calibration | Reads ≈ 97 g (≈ 3% error) |
| Servo | Programmed 0°→180°→0° sweeps | Accurate and smooth motion |
| Remote control | Phone joins hotspot, opens control page | Real-time data; all commands executed |
5.2 Analysis
The ≈3% weighing error stems mainly from load-cell nonlinearity and calibration bias, acceptable for home use. The 0–2 g idle fluctuation is sensor noise; after low-pass filtering and zero tolerance, the display is essentially stable. Slight reading drift was observed during testing and was significantly reduced by extending the warm-up period and fine-tuning the filter coefficient.
6 Conclusion and Future Work
This paper designed and implemented an STM32-based IoT pet feeding system with automatic feeding, ambient temperature monitoring and alarm, food weight detection, voice reminders, and web-based remote control. The hardware is modular with standard interfaces for easy debugging and maintenance, and the software follows modular design principles for clarity and extensibility. Testing shows stable operation across all functions, meeting the design goals.
Future directions include: (1) a camera module for live monitoring of pets eating; (2) cloud-platform integration for long-term data storage and analysis; (3) a dedicated mobile app; (4) low-food inventory alerts with purchase reminders; (5) improved weighing algorithms for higher accuracy and stability.