Skip to main content
Partnered with EDGE AI FOUNDATION · Part of the Internet of Communities™
MCU
SENSOR
ML MODEL
Intermediate
ExploreProjectsAutomatic Medication Dispenser

Automatic Medication Dispenser

A 7-slot medication dispenser with motor control and camera detection of pill intake. Notifies users on an app when to take their medication and whether they missed a dose.

TA
tallgiraffe102
Published Aug 6, 2026
Intermediateesp32
Automatic Medication Dispenser

Introduction

In this guide, we'll build a smart automatic pill dispenser that schedules daily medication, dispenses doses with a stepper motor-driven carousel, and uses camera verification to verify whether the pills were taken while alerting users or caregivers using an iOS app.

Prerequisites

  • ESP32 development setup with PlatformIO installed

  • Soldering tools and jumper wires

  • Access to 3D-printing services

  • Xcode (for building the iOS app)

  • Free Supabase Account for cloud storage and backend database setup

Parts List7 items
ComponentQtyNotes
Freenove ESP32-S3-WROOM (with built-in OV2640 camera)1-
28BYJ-48 Stepper Motor with ULN2003 Driver Board1-
SSD1306 0.96" I2C OLED Display1-
5V USB-C Power Supply1-
M3 Screws (6-10 mm) & Nuts64 for backing, 2 for microcontroller mount
M1.2 Screws (6-10 mm) & Nuts44 for attaching OLED display
Perfboard1-
1

Set Up

Set up the Supabase backend and flash the firmware to your ESP32-S3 board.

  1. Create a public bucket named pill-photos in Supabase Storage.

  2. Run supabase_setup.sql in the Supabase SQL Editor to initialize the database tables.

  3. Configure include/secrets.h with your Wi-Fi and Supabase credentials:

c
#pragma once
#define WIFI_SSID "your_network"
#define WIFI_PASS "your_password"
#define SUPABASE_URL "https://your-project.supabase.co"
#define SUPABASE_KEY "your_service_role_key"
#define SUPABASE_BUCKET "pill-photos"
  1. Upload firmware using PlatformIO:

bash
pio run --target upload
2

Build

Assemble the hardware components and pair the device with the companion iOS app.

  1. Wire and solder the stepper driver and OLED display to the ESP32 according to the GPIO pin mapping onto a perfboard:

    • Stepper (IN1–IN4): GPIO 1, 2, 42, 41

    • OLED (SDA / SCL): GPIO 39, 40

  2. Attach the ESP32 to the mount and subsequently screw it into the base, making sure to

  3. Mount the 3D-printed 8-slot carousel onto the stepper motor shaft (Slots 0–6 hold Mon–Sun doses; Slot 7 acts as the blocked loading slot).

  4. Open ios/MedSyncApp/MedSyncApp.swift in Xcode and deploy the app to your iOS device over Bluetooth.

3

Verify

Calibrate the physical carousel alignment and confirm end-to-end functionality.

  1. Connect to the device via BLE using the Controls tab in the iOS app.

  2. Hand-rotate the carousel until Slot 0 (Monday) is centered over the dispense hole, then tap Calibrate Home.

  3. Trigger a manual DISPENSE command from the app to verify 45° rotation and immediate photo upload to Supabase (should be visible on app).

Testing & Verification

  • Trigger a dispense cycle and verify the built-in camera captures an image and performs RGB565 saturation analysis to detect pills in the cup.

  • Optional: Run python scripts/cv_processor.py on a host machine to test higher-accuracy OpenCV-based pill verification and automated 14-day record cleanup.

Improvements & Enhancements

  • Enhanced Pill Detection: The current on-device algorithm relies on RGB565 color saturation analysis, which excels with brightly colored pills against a light cup but can struggle with all-white pills. Integrating the optional OpenCV Python backend enables morphological filtering and Hough circle detection for higher accuracy across all pill types.

  • Physical Sensors: Add an ambient light sensor or an infrared proximity sensor inside the collection cup to detect when the patient physically removes the dispensed pills, providing a second layer of intake verification beyond camera analysis.

  • Battery Backup: Implement an 18650 Li-ion battery shield with UPS functionality so the device maintains real-time clock tracking, scheduled dispensing, and Bluetooth connectivity during power outages rather than solely rely on USB-C power.