Smart Pillbox Based on 51 Microcontroller: Design of Precise Timing Reminder and Medication Management System

Introduction With the aging population and the increase in chronic disease patients, how to help users take their medication regularly has become an important issue in health management. Traditional pillboxes lack intelligent features, and patients often forget to take their medication or take the wrong medication, affecting treatment outcomes. The smart pillbox based on the 51 microcontroller effectively addresses these issues through features such as timed reminders, medication classification management, and weight detection. This article will detail the hardware design, software implementation, and core code of the system, and discuss its value in practical applications.

System Architecture and Hardware Design The core controller of the smart pillbox is the STC89C51 microcontroller, which has an 8-bit processor, 4KB RAM, and 32KB Flash memory, capable of meeting the system’s basic data processing and storage needs. The hardware modules mainly include: 1. Timed Reminder Module • DS1302 Clock Chip: Provides a high-precision real-time clock (RTC) that supports year, month, day, hour, minute, and second timing functions, maintaining time continuity even during power outages through a backup battery. • Buzzer and LED Alarm Device: Triggers sound and light signals to remind users to take their medication at the set time. 2. Medication Management Module • HX711 Pressure Sensor: Detects the remaining amount of medication through weight measurement, triggering an alarm when the weight is 0. • Infrared Sensing Module: Detects whether the pillbox is opened; if the user takes medication, the alarm is automatically canceled. 3. User Interaction Module • LCD1602 Display: Displays the current time, medication classification information, remaining quantity, and reminder status in real-time. • Key Input: Supports user settings for medication times and adjustments to medication quantities. 4. Environmental Monitoring Module • DHT11 Temperature and Humidity Sensor: Monitors the internal environment of the pillbox to prevent medication deterioration due to abnormal temperature and humidity.

Software Design and Function Implementation The system software is developed in C language, with code writing and debugging completed in the Keil uVision5 compilation environment. Core functions include: 1. Timed Reminder Logic

#include <reg51.h><br/>#include <reg52.h><br/>#include <intrins.h><br/>#define uchar unsigned char<br/>#define uint  unsigned int<br/>sbit BEEP = P2^0;  // Buzzer pin<br/>sbit LED = P2^1;   // LED pin<br/>// DS1302 clock chip related definitions<br/>sbit DS1302_CE = P1^0;<br/>sbit DS1302_IO = P1^1;<br/>sbit DS1302_SCLK = P1^2;<br/>uchar alarmTime[3][2] = {{08,00}, {12,00}, {18,00}};  // Preset 3 medication times<br/>void Delay(uint ms) {<br/>    uint i, j;<br/>    for(i = ms; i > 0; i--)<br/>        for(j = 110; j > 0; j--);<br/>}<br/>void DS1302_Write(uchar addr, uchar dat) {    // Code to write clock data (implementation omitted)}<br/>uchar DS1302_Read(uchar addr) {    // Code to read clock data (implementation omitted)}<br/>void Alarm_Check() {<br/>    uchar hour, minute;<br/>    hour = DS1302_Read(0x02);  // Read current hour<br/>    minute = DS1302_Read(0x01);  // Read current minute<br/>    for (int i = 0; i < 3; i++) {<br/>        if (hour == alarmTime[i][0] && minute == alarmTime[i][1]) {<br/>            BEEP = 0;  // Turn on buzzer<br/>            LED = 0;   // Turn on LED<br/>            Delay(5000);  // Reminder lasts for 5 seconds<br/>            BEEP = 1;  // Turn off buzzer<br/>            LED = 1;   // Turn off LED<br/>        }    }}<br/>void main() // Main function entry<br/>{<br/>    while(1) {<br/>        Alarm_Check();  // Loop to check reminder time<br/>        // Other functional code (e.g., medication management, environmental monitoring)<br/>    }}

2. Medication Weight Detection and Alarm<br/>#include "HX711.h"  // HX711 driver header file<br/>#define WEIGHT_THRESHOLD 100  // Medication weight threshold (in grams)<br/>void Check_Weight() {<br/>    uint weight = HX711_Get_Weight();  // Read current weight<br/>    if (weight <= WEIGHT_THRESHOLD) {<br/>        BEEP = 0;  // Buzzer alarm<br/>        LED = 0;   // LED alarm<br/>        Delay(3000);  // Alarm lasts for 3 seconds<br/>        BEEP = 1;<br/>        LED = 1;<br/>    }}<br/>3. Power Failure Protection and Data Storage The system uses EEPROM to store user-set medication times and quantities, ensuring data is not lost after a power failure:

#include "eeprom.h"<br/>void Save_Settings() {<br/>    for (int i = 0; i < 3; i++) {<br/>        EEPROM_Write(0x10 + i*2, alarmTime[i][0]);  // Store hour<br/>        EEPROM_Write(0x11 + i*2, alarmTime[i][1]);  // Store minute<br/>    }}<br/>void Load_Settings() {<br/>    for (int i = 0; i < 3; i++) {<br/>        alarmTime[i][0] = EEPROM_Read(0x10 + i*2);<br/>        alarmTime[i][1] = EEPROM_Read(0x11 + i*2);<br/>    }}

System Testing and Optimization 1. Function Verification • Timed reminder test: By adjusting the DS1302 time, verify whether the buzzer and LED trigger normally at the preset time. • Weight detection test: Place objects of different weights in the pillbox and observe whether the HX711 output matches the actual values. • Power recovery test: After a power failure, check whether the user-set medication times and quantities are retained. 2. Optimization Directions • Low-power design: Reduce system power consumption through sleep mode to extend battery life. • Multi-language support: Add Chinese and English switching functions on the LCD display to meet different user needs. • Remote monitoring expansion: Integrate Bluetooth or WiFi modules for remote management via a mobile app.

Leave a Comment