Introduction: Automatic Pill or Medication Dispenser

About: I have always been interested in art and creating things. I started with attending a sculpture night class which I did for several years which I really enjoyed. I modelled mainly in clay and either casting or …

I created this pill or medication dispenser because my wife has to take several tablets in the morning and evening and was getting confused with which tablets she had taken and therefore not always taking the right medication at the correct time.

I wanted a simple system that would effectively dispense the correct tablets morning and evening.

It had to have a display to indicate when the tablets were ready and a timer to adjust when they were dispensed.

Supplies

These are the supplies required for creating the dispenser with approximate UK prices:

Arduino Uno Board and USB cable £5

LCD display 1602 12C 16x2 £5

Clock DS3231 £4

Motor 28BYJ Stepper motor and ULN2003 driver £7

5mm LED

Push button switch and Cap

Wire for connections

Connector block

M6 50mm bolt

Glue gun

Superglue

Soldering iron and solder

3D printer and PLA

Step 1: Design Concept

The criteria for the dispenser was:

  1. Needed to be compact so that it could sit without taking up too much space on the kitchen worktop
  2. Be able to hold and dispense medication in the morning and evening for a week
  3. The time for dispensing could be adjusted
  4. Provide a visual display of time and when tablets were ready


The simplest way to store the tablets was a circle with separate containers for each set of tablets around the edge. Each container needed to be large enough to contain sufficient tablets. I decided to have 14 containers (7days AM and PM) plus one spare so that this could be positioned in the opening when the dispenser was refilled so a total of 15. This then determined how big it needed to be.

The next thing was how to rotate the dispenser, I had a 28BYJ stepper motor which are cheap and readily available. I decided to use an internal gear ring fixed to the underside of the dispenser which would be driven by a small gear on the stepper motor. I used a gear ration of 7.5 to 1 which is divisible into 15 so I can easily set the the stepper motor to turn half a revolution and move each container exactly.

The base of the unit had to hold all the electronics and motor, and ideally have locators so the the various parts could be easily mounted and glued in place.

Step 2: 3D Model

I designed this using OnShape, I am using the free licence for hobbyists so the model is freely available for others to copy and use here.

Based on the concept as set out above I designed each part so that as many as possible could be printed without any support.

There are 7 main parts:

  1. The base containing all the electronics
  2. Mounting pins are on the underside so components can be glued in place.
  3. Holes for the stepper motor, Arduino Power supply, LCD display and push button. The LED bulb was an afterthought and a hole had to be drilled in the base.
  4. Three small slots around the edge help to align the lid.
  5. I managed to find 3D models of the components which I could then use to make sure they all fitted into the base and finalise their location and mounting pins, fixing holes and access holes.
  6. Centre spindle over which the container sits.
  7. Internal gear which is glued to the base of the container. Location pins are on top of the gear which align with corresponding holes in the container.
  8. Small gear which fits to the stepper motor
  9. The container.
  10. This has vertical walls to divide each compartment.
  11. Each compartment is labelled with the day Initial and Am or Pm
  12. The Lid to cover the container and stop pills falling out
  13. This has an opening corresponding to one of the container slots
  14. Three lugs are included on the bottom edge to align the lid and base
  15. Dish to receive the tablets
  16. Separate mounting pins in case those designed on the base don't align with your particular part.

STL files for each part is available to download below

The gears were designed using Involute Spur Gear Builder v2.0

http://hessmer.org/gears/InvoluteSpurGearBuilder.html

The SVG downloaded and converted to DXF then imported into OnShape and extruded to create the gears. Flats were added to the Small gear hole to correspond with the stepper spindle.


Step 3: Circuit

I have included a Fritzing wiring diagram showing how all the components are connected.

Step 4: Printing and Building

All the components were exported as STL models at the highest resolution. I used Ultimaker Cura to slice the models and printed them with a layer height of 0.2mm.

My printer is an Ender 3

Specifics for each component are:

  1. Base: Needs printing with support for holes in the side and alignment slots. Print with top on the printer bed.
  2. Gears: Print with 50% infill, no support
  3. Spindle: Print with 20% infill, no support
  4. Container: Print with 100% infill, no support
  5. Lid: Print with top face on the print bed, 100% infill, no support


Building

Once all the parts have been printed they will need to be cleaned up, remove any supports from the holes. There is usually a little edge where the piece adheres to the print bed this can be removed with a file or sand paper. The gears will need to be smoothed and cleaned up so that they run smoothly.

Hopefully the pins in the base correspond with the mounting holes on your components however depending on manufacturer the holes don't always appear in the same place. Test all components before trying to glue in, if the don't fit either remove the pips on the post and glue on the post or if they are very different remove the post, i have included an STL model to print new posts which can be superglued to the bas in the right place.

It is easier to solder some of the wires to the components before fixing them into the base.

The Stepper motor is fixed to the base with 2 screws.

All the other components are glued in place with a glue gun.

I also applied some glue to hold some of the wires in place.

The inner gear is superglued to the container, the pins on the gear align with the holes on the container. The centre spindle is pushed through the container from the underside and superglued in place.

The small gear can now be pushed onto the motor spindle.

Insert the M6 bolt through the bottom of the base this acts as the main spindle, it can be superglued in place where the hex head is inserted in the base.

The container can now be placed onto the main spindle and gears aligned the lid can be clipped over the top.

Now position the dish in front of the opening in the lid and superglue in place, being careful not to glue it to the lid or container.

We can now move on to the programming.

Step 5: Arduino Program

I am not an expert programmer so I have utilised code written by others who have been credited in the code. I have then using the basic skills I have learned adapted the code for my use. I am sure there are improvements that could be made, but it appears to work very well.

I have used Arduino IDE to edit and upload the code to the Arduino UNO. I am not going into specifics of uploading the code to the board. Other than you need to set IDE to the correct board before uploading.

The code is attached and is ready to use.

I have described the various key sections below:


  • Setting dispense time

The time that the pills are dispensed is in the code as shown below it is currently set for 08:00 and 21:00 change these figures in the code if you want a different time.

//time settings for pill dispensing

//AM

int amHr = 8;

int amMin = 0;

int amSec = 0;

//PM

int pmHr = 21;

int pmMin = 00;

int pmSec = 0;


  • File display

One thing I have found useful while testing code is to include the following line in the void setup () section, when you run the serial monitor it displays the filename of the code that is running, this is particularly useful when testing various versions and you have forgotten which one is currently running.

Serial.print(__FILE__);


  • Moving the stepper motor

There are 2 sections for rotating the motor one for AM and one PM.

While the dispenser is waiting to move it is important that there is now power to the motor otherwise it will get hot. Line stepper.enableOutputs(); turns power on and once the motor has moved stepper.disableOutputs(); turns it off.

The notes in the code explain the gearing and the number of turns required for the stepper motor to rotate the container one section.

The LED is turned on digitalWrite(led, HIGH); to indicate the pills are ready and the LCD is updated starting at the beging of line 1 lcd.setCursor(0, 1); with the text lcd.print("AM Pills Ready"); This text can be changed by editing the text in quotes.

The PM code is the same.

void rotateAM() {

 // Set the current position to 0:

 stepper.enableOutputs();

 stepper.setCurrentPosition(0);


 // Run the motor forward at 500 steps/second until the motor reaches 4096 steps (1 revolution):

 // Gear ratio is 60 to 8 teeth ie 7.5:1 therefore 1/2 revolution will be 1/15th of 360 degrees = 2048 steps

 while (stepper.currentPosition() != 2048) {

  stepper.setSpeed(500);

  stepper.runSpeed();

 }

 stepper.disableOutputs();

 digitalWrite(led, HIGH);

 lcd.setCursor(0, 1);

 lcd.print("AM Pills Ready");


}

  • Updating after pills taken

When the button is pressed the light goes out and and the display is updated with the text "Pills Taken" This is the code for that:

// Press button when pills taken. Check if the pushbutton is pressed. If it is, the buttonState is HIGH:

 if (buttonState == HIGH) {


  //lcd.backlight();  // Switch-on lcd backlight

  lcd.clear();

  digitalWrite(led, LOW);

  lcd.setCursor(0, 1);

  lcd.print("Pills Taken");


  • Setting the current time

Once uploaded the time needs to be set.

The Arduino Clock code is courtesy of Norman Dunbar

Open the serial monitor, or similar, and type u (and enter). The LCD will display "Edit mode" and you will be prompted to enter the years, months etc to set the clock.

While entering details, if you need to abort, type -1 for the value and all changes so far will be aborted. The LCD will return to showing the date and time as before.

All inputs are validated for a certain range of values. Months are 1 to 12, but what about leap years? Those are not validated (yet) and neither are "30 days hath September ...".

After all inputs are done, a temporary DateTime is created with the entered values. If this is a valid date time - leap year ok, days in month ok etc - then the RTC will be set. If the DateTime is invalid, then we go around all the inputs again.


You should now be ready to connect the unit to a USB charger to power up. If all is well it will display the current time.

Step 6: Operation

It is best to fill the dispenser on a Monday morning before the first dispense time occurs. Remove the lid and manually rotate the container until the slot without day markings is in line with the dish.

Place all the tablets for the week in the correct slots and replace the lid.

Plug in the USB power cord and the LCD will display the correct time.

The RTC has a battery backup so the time should always be correct, the only time it will need to be changed is if the battery runs out or when the clocks change for daylight saving.

To change the time plug into the computer and run Arduino IDE serial mode, type U and enter to go back to the time setting.

The container will rotate at the next dispense time.

Step 7: Conclusion

This unit works really well and we have been using if for more than a year, it is simple and therefore has limitations.

Power cuts could be an issue if it happens at the time tablets should be dispensed, meaning the wrong tablets are dispensed at the next time. This just requires the container to be manually rotated so the correct tablets will be dispensed next time.

Ideally I would have liked to have the ability to send a text or email when the tablets are ready, however I struggled to get the stepper to work with the likes of a NodeMCU.

If you wanted to have tablets dispensed more than twice a day the container would need to be redesigned and gears adjusted.

I am sure there are other improvements that could be made and I'd be pleased to see what others can do.


I hope you find this useful

Microcontroller Contest

Participated in the
Microcontroller Contest