Introduction: Automated Candy Dispenser

This instructable was created in fulfillment of the project requirement of the Makecourse at the University of South Florida (

The following are step by step instructions for creating your own Automated Candy Dispenser. All parts can be 3-D printed and the control system is Arduino based. All 3-D part, Arduino sketches, and libraries are included.

Step 1: 3-D Printed Parts

The following are all the part that will need to be 3-D printed.

The .ipt files can be downloaded below.

Step 2: Electronic Components

The following are all the electronic components you will need for this project.

1) Arduino Uno

2) 9V battery and Arduino connector

3) IR Sensor and Remote Control

4) Stepper Motor and Driver Board

5) Adafruit NeoPixel NeoMatrix 8x8

7) Wide-Mouth Mason Jar with Threaded Attachment

6) 10kohm Resistor, 300-500 ohm resistor, and 1mF 6.3V+ Capacitor

Step 3: 3-D Parts and Mason Jar Assembly

Assemble the 3-D printed parts and the mason jar as shown.

Gluing the part together with Liquid Nail Perfect glue and allowing to cure overnight created a very strong bond between the parts.

Step 4: Control Circuit Implementation

Place the 10kohm resistor between the Vcc and Output pins on the IR Sensor.

Place the 1mF 6.3V+ capacitor between the positive and ground terminal on the LED Matrix. Also, place the 300-500ohm resistor between the Arduino output pin and the DIN pin on the LED Matrix.

Plug the Motor into the Drive Board.

Next connect all 5V and GND pins for the IR Sensor, LED Matrix, and Motor, into the 5V and GND pin on the Arduino Power Rail.

Then, connect all the device input pins into the digital pins on the Arduino. The IR Sensor and LED Matrix each require 1 pin. The Motor Driver Board requires 4.

The video attached provides a description of the breadboard setup of the electronic components.

Step 5: The Code and Libraries Required

The following are the Arduino sketch developed for this project and all the libraries required to run it.

Ensure that you unzip all the libraries and place them in the Arduino library folder which can be found in at Document/Arduino/library, on your hard drive.

Change the pin numbers for each item to the pin number you have each component plugged into on the Arduino.

The attached video provides a description of the Arduino sketch developed for this project.

The following is a description of the Arduino sketch.

This part of the code calls all the libraries used:

#include "stepMotor28BYJ.h" //Includes Motor library
#include "IRremote.h" //Includes IR Senor library #include "Adafruit_GFX.h" //Includes Library for LED Matrix #include "Adafruit_NeoMatrix.h" //Includes Library for LED Matrix #include "Adafruit_NeoPixel.h" //Includes Library for LED Matrix

This part initializes the Motor, IR Sensor, and LED Matrix, setting all the pins for each device and constructors:

#define pin1 8 //Motor Pin 1
#define pin2 9 //Motor Pin 2 #define pin3 10 //Motor Pin 3 #define pin4 11 //Motor Pin 4 #define delaytime 8 //Motor delay time

stepMotor28BYJ stepper(pin1, pin2, pin3, pin4, delaytime); //Initializes Motor Library

#define irPin 2 //IR Sensor Pin

IRrecv irrecv(irPin); //Intitializes Motor Library decode_results results; //Results of IR reading are stored in results

#define PIN 5 //LED Matrix Data Input Pin #define BOARDCOUNT 1 //Number of LED Matrices Adafruit_NeoPixel strip = Adafruit_NeoPixel(64 * BOARDCOUNT, PIN, NEO_GRB + NEO_KHZ800); //Initializes Number of LEDs

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN, //Initializes LED Matrix NEO_MATRIX_BOTTOM + NEO_MATRIX_LEFT + //Sets LED Matrix orientation NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE, //Sets how input data is coded NEO_GRB + NEO_KHZ800); //Sets LED Matrix color settings

This matrix initializes all the colors used in the LED Matrix, the specific color is commented in the code:

const uint16_t colors[] = { //Initializes colors displayed on LED Matrix
matrix.Color(85, 85, 85),matrix.Color(128, 0, 0), matrix.Color(100, 40, 0), //White, Red, Orange matrix.Color(85, 85, 0),matrix.Color(0, 128, 0),matrix.Color(0, 0, 128), //Yellow, Green, Blue matrix.Color(85, 0, 85), //Purple };

These integers are used for turning the LED Matrix on and off, and setting the standby display mode:

int mode=0; //Variable used to track Remote Mode Button, Changes Standby display from Skittle to M&M based on value
int power=0; //Variable used to track Remote Power Button, Turns LED Matrix on and off based on value

This part sets up the devices for the main loop:

void setup() { //Setup Initialization
irrecv.enableIRIn(); //Enables IR Sensor matrix.begin(); //Allows Data Output to Matrix matrix.setTextWrap(0); // matrix.setBrightness(30); //Sets Matrix Brightness matrix.setTextColor(colors[0]); //Defines Matrix color as white, unless otherwise defined Serial.begin(9600); //Begins Serial Communication }

This is the main loop. First the IR Sensor is checked if it is receiving a signal. If the signal is from the 1-9 buttons on the remote, i is set to the corresponding number. If the power button is pressed the LED matrix is reset and the power integer is iterated. If the mode button is pressed the mode integer is iterated. If the button that is pressed is not one of the designated buttons the i integer is set to 0. Next, the IR Sensor is set to resume normal operations. Then, i is passed to the stepper motor rotate function, and will rotate the motor 1/6 of a revolution for each value of i. if the motor has rotated, the enjoy function displays the word "ENJOY" scrolling across the LED Matrix. If the IR Sensor is not receiving a signal the candy function is executed and displays either a Skittle or M&M dependent on the value of mode.

void loop() { //Main Loop
int i=0; //Sets number of candies to be dispensed to 0 if(irrecv.decode(&results)) { //Executes if IR Sensor recieves signal if(results.value == 16724175) i=1; //Remote Button 1 else if(results.value == 16718055) i=2; //Remote Button 2 else if(results.value == 16743045) i=3; //Remote Button 3 else if(results.value == 16716015) i=4; //Remote Button 4 else if(results.value == 16726215) i=5; //Remote Button 5 else if(results.value == 16734885) i=6; //Remote Button 6 else if(results.value == 16728765) i=7; //Remote Button 7 else if(results.value == 16730805) i=8; //Remote Button 8 else if(results.value == 16732845) i=9; //Remote Button 9 else if(results.value == 16753245) { //Remote Power Button matrix.fillScreen(0); //Sets Input to Matrix to 0; //Clears Matrix power++; //Adds on to power if Power Button pressed //Used in Enjoy and Candy Functions, below } else if(results.value == 16736925) mode++; //Remote Mode Button //Used in Candy Function, below else i=0; //If no other button is pressed, sets number of candies to 0 irrecv.resume(); //IR Sensor turned back on stepper.rotate(i); //Passes number of candies to be dispensed to Stepper Library, Rotates Motor to dispense specified number of candies if(i!=0) enjoy(power); //If candies are dispensed, Display Enjoy on LED Matrix, takes power variable as input } else candy(power,mode); //If not candy is dispense, Display a pieces of cand on LED Matix, takes mode and power as inputs }

This is the enjoy function which displays the word "ENJOY". The for loop is used to scroll the word across the matrix. If at anytime an IR signal is received the loop function is exited and returns to the main loop. Also, if the power integer is odd the word is not displayed.

void enjoy(int power) { //Displays word ENJOY, dependent on power variable
for(int i=8; i > -30; --i) { //For loop to scroll ENJOY across LED Matrix if(power%2 == 1) break; //If the power variable is odd, Breaks ENJOY display because the LED Matrix is set to off matrix.fillScreen(0); //Clears LED Matrix Input matrix.setCursor(i, 0); //Sets LED Matrix Cursor to postion i, this allows ENJOY to scroll as for loop executes matrix.print(F("ENJOY")); //Prints ENJOY on LED Matrix at defined cursor position; //Shows latest update to LED Matrix input delay(125); //Delay .125 seconds before shifting cursor } }

This is the candy function. If the mode integer is even a Skittle is displayed, if odd an M&M is displayed. If the power integer is odd nothing is displayed. The for loop cycles the colors of the piece of candy.

void candy(int power, int mode) { //Display piece of candy flashing different colors on LED Matrix, dependent on power variable, candy type dependent on mode variable
matrix.fillScreen(0); //Clear LED Matrix Input for(int i=1; i<7; i++) { //For loop to change the color of candy, RED, ORANGE, YELLOW, GREEN, BLUE, and PURPLE if(irrecv.decode(&results)) break; //If IR Sensor receives input, breaks display of candy if(power%2 == 1) break; //If power variable is odd, Breaks candy display because the LED Matrix is set to off matrix.fillCircle(3,3,3,colors[i]); //Creates circle of specified color from colors matrix previously defined matrix.setCursor(1, -1); //Sets position of cursor so either 's' or 'm' will be displayed in the middle of circle if(mode%2 == 0) matrix.print(F("s")); //If mode variable is even, an 's' is printed to LED Matrix, effectively displaying a Skittle else matrix.print(F("m")); //If mode variable isn't even, an 'm' is printed to LED Matrix, effectively displaying an M&M; //Shows latest update to LED Matrix input delay(750); //Delays .75 seconds before changing candy color } }

I wrote a motor library for this code because the motor could not be made to rotate only 1/6 of a revolution at a time. It is a standard stepper motor library with high torque settings. The correction to allow the stepper to rotate 1/6 a revolution without coming of center is in the following code.

void stepMotor28BYJ::rotate(int pieces) { //Rotates motor to deliver the user defined number of pieces
for( _i=0; _i 0; _steps--) _forward(); //Counts number of steps taken _stepOff(); //Turns coils off _j++; //Add 1 to _j, counts numbe of candies dispensed until next correction } }

The code is fully commented and should be fairly easy to follow if you have experience programming.

Step 6: Putting It All Together and ENJOY

Once you have all the parts assembled and uploaded the libraries and code you should be able to fill the machine with candy and begin to ENJOY it.

I hope you have enjoyed building your own Automated Candy Dispenser.