Automated Pill Dispenser
A sensor-verified automatic medication dispenser with dual stepper motor carousel and shutter mechanism.
info
Author: Dinu Alexandra
GitHub Project Link: GitHub Repository
Description
The device dispenses pills from a 7-day rotating carousel according to a preset schedule. A stepper motor rotates the pill box to the correct day, while a second stepper motor controls a shutter disc that opens to release pills by gravity. An IR sensor confirms dispensing, a Hall sensor verifies carousel position, an ultrasonic sensor detects user proximity, and a temperature/humidity sensor monitors storage conditions. The user sets schedules via 3 buttons and an OLED display. A servo motor locks the lid to prevent unauthorized access. All dispensing events are logged to a MicroSD card.
Motivation
Elderly people and patients with complex treatments frequently forget to take their medication on time. Commercial automated dispensers are expensive and often lack verification mechanisms. This project provides an affordable solution that verifies every step of the dispensing process through multiple sensors, monitors storage conditions, and alerts the user through audio and visual feedback — all without requiring internet connectivity.
Architecture
The system has three main subsystems connected to the STM32 Nucleo-U545RE-Q:
- Sensor subsystem — 4 sensors provide feedback: Hall effect (KY-035, ADC) for carousel homing, IR obstacle sensor (GPIO) for pill detection, HTU21D (I2C) for temperature and humidity, HC-SR04P (GPIO) for user proximity detection.
- Actuator subsystem — 3 actuators control dispensing: Stepper motor 1 + ULN2003 (GPIO×4) rotates the carousel, Stepper motor 2 + ULN2003 (GPIO×4) controls the shutter disc, Servo SG90 (PWM) locks/unlocks the lid.
- Interface subsystem — OLED SSD1306 display (I2C), DS3231 RTC (I2C) for scheduling, MicroSD card module (SPI) for event logging, active buzzer (PWM) for alarms, 3 push buttons (GPIO) for menu navigation.
All I2C devices (OLED, DS3231, HTU21D) share the same bus on PB6/PB7 with 4.7kΩ pull-up resistors. Communication protocols used: I2C, SPI, GPIO, PWM, ADC.
Log
Week 5 - 11 May
Week 12 - 18 May
Week 19 - 25 May
Hardware
The mechanical assembly uses a commercially available 7-day round pill box mounted upside-down on the first stepper motor's shaft. The compartments face downward onto a fixed base plate. A second stepper motor drives a shutter disc beneath the base — when the shutter hole aligns with the active compartment, pills fall through by gravity into a collection tray. A neodymium magnet glued to the carousel triggers the Hall sensor once per revolution for position calibration. The IR sensor at the dispensing window confirms that pills actually fell through. The entire assembly sits inside a box enclosure containing the breadboard, STM32 Nucleo, battery holder, and all electronics.
Schematics
Bill of Materials
| Device | Usage | Price |
|---|---|---|
| STM32 Nucleo-U545RE-Q | The microcontroller | Provided by university |
| OLED Display 0.96" SSD1306 I2C | Displays schedule, status, alerts | 16.96 RON |
| DS3231 RTC Module | Real-time clock for dose scheduling | 17.98 RON |
| HTU21D Temp & Humidity Sensor | Monitors medication storage conditions | 17.22 RON |
| Hall Sensor KY-035 | Carousel homing and position verification | 4.15 RON |
| IR Obstacle Sensor | Detects pills falling through dispensing window | 3.12 RON |
| HC-SR04P Ultrasonic Sensor | Detects user proximity for display wake-up | 10.14 RON |
| 28BYJ-48 Stepper Motor ×2 | Rotates carousel and shutter disc | 20.58 RON |
| ULN2003 Driver Module ×2 | Drives the stepper motors | 9.94 RON |
| SG90 Servo Motor 180° | Locks/unlocks the lid | 9.49 RON |
| Active Buzzer 5V ×2 | Audible alarm for dose reminders | 2.22 RON |
| Mini Button 6×6×5mm ×4 | Menu / Up / Down navigation | 1.44 RON |
| MicroSD Card Module SPI | Logs dispensing events in CSV format | ~7.00 RON |
| Breadboard MB102 830pts | Prototyping platform | 16.66 RON |
| Breadboard Power Supply | Regulates power to 3.3V and 5V rails | 6.69 RON |
| 4×AA Battery Holder | Power source (6V) | 6.34 RON |
| Varta AA Batteries ×8 | Powers the system | 13.18 RON |
| TP4056 Charging Module | Optional LiPo charging | 4.72 RON |
| Resistor Kit 600pcs | Pull-up resistors for I2C, button pull-downs | 15.16 RON |
| Dupont Wires MM 30cm ×40 | Breadboard connections | 8.39 RON |
| Dupont Wires FM 10cm ×40 | Module-to-breadboard connections | 7.73 RON |
| Dupont Wires FF 30cm ×40 | Module-to-module connections | 7.59 RON |
| Neodymium Magnets 5×2mm ×20 | Position marker on carousel for Hall sensor | 24.16 RON |
| 7-Day Round Pill Box | Carousel body (7 wedge compartments) | 3.81 RON |
Software
| Library | Description | Usage |
|---|---|---|
| embassy-stm32 | HAL and async runtime for STM32 | GPIO, I2C, SPI, PWM, ADC peripheral access |
| embassy-executor | Async task executor | Runs 6 concurrent tasks: scheduler, motor control, sensors, display, proximity, logging |
| embassy-time | Async timers and delays | Non-blocking delays for stepper stepping, sensor polling, alarm timeouts |
| ssd1306 | OLED display driver | Renders schedule, status pages, and alerts on the 0.96" I2C display |
| ds323x | DS3231 RTC driver | Reads current time, sets alarm times for dose scheduling |
| embedded-sdmmc | SD card FAT32 filesystem driver | Writes timestamped dispensing events to CSV files via SPI |
| embedded-hal | Hardware abstraction layer | Standardized interfaces for GPIO, I2C, SPI, ADC peripherals |
| defmt + defmt-rtt | Logging framework | Structured debug logging via RTT during development |
| panic-probe | Panic handler | Prints panic info and backtraces via RTT probe |