Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Based Automated Pill Dispenser with GSM Notifications and RTC Scheduling

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Automated Pill Dispenser with GSM Notifications and RTC Scheduling

Circuit Documentation

Summary

This circuit is designed to operate as a medical pill dispenser with a real-time clock (RTC) for timekeeping, an LCD for user interface, servos for dispensing pills, a GSM module for sending SMS alerts, and a buzzer for audible notifications. The Arduino UNO serves as the central microcontroller, interfacing with the various components to control the dispensing mechanism and communicate with the user.

Component List

Arduino UNO

DS3231 RTC

Tower Pro SG90 Servo (x3)

I2C LCD 16x2 Screen

Sim800l

Piezo Buzzer

Pushbutton (x2)

Wiring Details

Arduino UNO

DS3231 RTC

Tower Pro SG90 Servo

I2C LCD 16x2 Screen

Sim800l

Piezo Buzzer

Pushbutton

Documented Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DS3231.h>
#include <SoftwareSerial.h>

// Pin definitions
#define BUZZER_PIN 8
#define BUTTON_PIN 7
#define SERVO_PIN_1 9
#define SERVO_PIN_2 10
#define SERVO_PIN_3 11

// GSM Module pins
#define GSM_TX 12
#define GSM_RX 13

// Servo and RTC objects
Servo pillServo1;
Servo pillServo2;
Servo pillServo3;
DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Adjust the address if needed

// GSM module
SoftwareSerial gsm(GSM_TX, GSM_RX);

// Variables
bool pillTaken = false;
int dispensingHour[3] = {8, 14, 20}; // Hardcoded times: 8 AM, 2 PM, 8 PM

void setup() {
  // Setup the servos
  pillServo1.attach(SERVO_PIN_1);
  pillServo2.attach(SERVO_PIN_2);
  pillServo3.attach(SERVO_PIN_3);
  
  // Setup the LCD
  lcd.begin();
  lcd.backlight();
  
  // Setup the RTC
  rtc.begin();
  
  // Setup the GSM module
  gsm.begin(9600);
  delay(1000); // Allow time for GSM module to initialize
  sendSMS("Medical dispenser started.");
  
  // Setup the buzzer and button
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  DateTime now = rtc.now();
  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  lcd.print(now.hour());
  lcd.print(":");
  lcd.print(now.minute());
  
  for (int i = 0; i < 3; i++) {
    if (now.hour() == dispensingHour[i] && now.minute() == 0 && !pillTaken) {
      dispensePill(i);
      pillTaken = true;
      sendSMS("Pills dispensed. Please take them.");
      startReminderTimer();
    }
  }
  
  // Reset pillTaken at the end of the hour
  if (now.minute() > 0) {
    pillTaken = false;
  }
  
  // Manual pill-taking confirmation
  if (digitalRead(BUTTON_PIN) == LOW) {
    pillTaken = true;
    lcd.setCursor(0, 1);
    lcd.print("Pill Taken       ");
    sendSMS("Pill taken confirmation received.");
    delay(1000);
  }
  
  delay(1000); // Loop every second
}

void dispensePill(int timeSlot) {
  if (timeSlot == 0) {
    pillServo1.write(90); // Adjust this value according to the dispenser mechanism
    delay(1000); // Wait for the pill to be dispensed
    pillServo1.write(0); // Return to original position
  } else if (timeSlot == 1) {
    pillServo2.write(90);
    delay(1000);
    pillServo2.write(0);
  } else if (timeSlot == 2) {
    pillServo3.write(90);
    delay(1000);
    pillServo3.write(0);
  }
  
  lcd.setCursor(0, 1);
  lcd.print("Pill Dispensed   ");
}

void sendSMS(String message) {
  gsm.print("AT+CMGF=1\r"); // Set SMS to text mode
  delay(100);
  gsm.print("AT+CMGS=\"+1234567890\"\r"); // Replace with your phone number
  delay(100);
  gsm.print(message); 
  delay(100);
  gsm.write(26); // Send SMS (CTRL+Z)
  delay(1000); 
}

void startReminderTimer() {
  long reminderTime = millis() + 600000; // 10 minutes reminder timer
  while (millis() < reminderTime) {
    if (pillTaken) return; // Exit if pill taken
    if (millis() >= reminderTime) {
      tone(BUZZER_PIN, 1000, 2000); // Buzzer alert
      sendSMS("Reminder: Please take your pills.");
    }
  }
}

This code is responsible for controlling the medical pill dispenser. It initializes the servos, LCD, RTC, and GSM module in the setup() function. The loop() function checks the current time against predefined dispensing hours and actuates the corresponding servo to dispense pills. It also listens for button presses to confirm pill intake and sends SMS alerts for various events. The sendSMS() function is used to send text messages, and startReminderTimer() sets up a reminder for the user to take their medication.