Introduction: PEZ Robo Dispenser Using Arduino
I was strolling down the candy aisle at the supermarket and there it was: the PEZ dispenser, conjuring up sweet (literally) childhood memories served up by my favorite cartoon characters in squarish sugary pellets. Then, all of a sudden, an LED went off in my head. PEZ meets Arduino.
In this age of over-automation, where robots do everything short of scratching nostrils, I figured that my sugar-dispensing childhood toy, The PEZ, also deserves automation.
HOW IT WORKS
Basically, I am using an ultrasonic distance sensor to decide if someone is within 20 cm from the sensor which is attached next to PEZ. If so, Arduino will turn the servo 180 degrees back pulling with it the ear of Mickey Mouse. When Mickey's head is pulled back, a PEZ pellet is pushed out of the mouse's mouth. Yummy.
After a few seconds (5 seconds in my Arduino sketch) the servo returns back to 90 degrees, closing Mickey's mouth and waiting for the next person in line to serve.
Step 1: PARTS LIST
- The PEZ Robo Dispenser. I picked a Mickey Mouse PEZ because big ears are better at holding a noose that will be tied to the rotating servo horn.
- Arduino Uno or clone. I used my GOduino III.
- Ultrasonic Distance Sensor HC-SR04. ($2.50 Ebay)
- RC Servo ($2.50 Ebay)
- Breadboard
- Jumper wires
- Wire fasteners or rubber bands
- Battery (Anything over 7 volts with about 500mAh).
Step 2: Wiring Robo PEZ
SERVO
ULTRASONIC SENSOR
- Black ---> Arduino GND pin
- Red ---> Arduino 5V pin
- White/Yellow ---> Arduino 9 pin
ULTRASONIC SENSOR
- GND pin ---> Arduino GND pin
- VCC pin ---> Arduino 5V pin
- TRIG pin ---> Arduino 12 pin
- ECHO pin ---> Arduino 13 pin
Step 3: Linking the Servo to the PEZ
Please take note how I placed the servo against the breadboard and which side is up and which is down. This is important if you wish to use my code as it.
1) Before screwing the horn to the servo, make sure the servo is turned all the way away from the bread board until it can turn no more. You can use the servo horn without a screw to turn the servo. This is BACK position 180 aka BACK/OPEN position (see figure). You can screw the horn now as shown in figure.
2) Now rotate the horn until it's in the 90 position aka UP/CLOSED (see figure). Using a firm wire, bend one end into a hook and insert into the servo's horn. Wrap the other end around Mickey Mouse's ear.
1) Before screwing the horn to the servo, make sure the servo is turned all the way away from the bread board until it can turn no more. You can use the servo horn without a screw to turn the servo. This is BACK position 180 aka BACK/OPEN position (see figure). You can screw the horn now as shown in figure.
2) Now rotate the horn until it's in the 90 position aka UP/CLOSED (see figure). Using a firm wire, bend one end into a hook and insert into the servo's horn. Wrap the other end around Mickey Mouse's ear.
Step 4: The Arduino Robo PEZ Sketch
/* I have attached the Arduion Ultrasonic library to this step. This is the library that I am using in this project. Save it to Ultrasonic.rar then extract it to path: Arduino\libraries\Ultrasonic. There are other libraries and they should work but some code modification maybe required.
I am placing this code in the public domain.
*/
-------------------------------
#include <Servo.h>
#include <Ultrasonic.h>
#define TRIGGER_PIN 12 // Ultrasonic sensor TRIGGER
#define ECHO_PIN 13 // Ultrasonic sensor ECHO
#define DISTANCE 20 // in Centimeters
#define WAITING 5000 // in milliseconds so this is 5 second.
#define SERVO_PIN 9
#define CLOSE_POS 90 // servo in CLOSE/UP position
#define OPEN_POS 180 // servo in OPEN/BACK position
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
Servo myservo; // create servo object to control the PEZ servo
void setup()
{
Serial.begin(9600);
myservo.attach(SERVO_PIN); // attaches the servo to pin
myservo.write(CLOSE_POS); // turn servo to CLOSE/UP position
}
void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
// this is for debugging
Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
if (cmMsec >= DISTANCE) // if no one within distance of PEZ close mouth
{
myservo.write(CLOSE_POS); // tell servo to go to CLOSE/UP
delay(15); // waits 15ms for the servo to reach the position
}
if (cmMsec <= DISTANCE) // if someone is within distance, open mouth
{
myservo.write(OPEN_POS); // tell servo to go to OPEN/BACK
delay(WAITING); // waits sometime to allow grabbing of candy pellet
}
}
I am placing this code in the public domain.
*/
-------------------------------
#include <Servo.h>
#include <Ultrasonic.h>
#define TRIGGER_PIN 12 // Ultrasonic sensor TRIGGER
#define ECHO_PIN 13 // Ultrasonic sensor ECHO
#define DISTANCE 20 // in Centimeters
#define WAITING 5000 // in milliseconds so this is 5 second.
#define SERVO_PIN 9
#define CLOSE_POS 90 // servo in CLOSE/UP position
#define OPEN_POS 180 // servo in OPEN/BACK position
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
Servo myservo; // create servo object to control the PEZ servo
void setup()
{
Serial.begin(9600);
myservo.attach(SERVO_PIN); // attaches the servo to pin
myservo.write(CLOSE_POS); // turn servo to CLOSE/UP position
}
void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
// this is for debugging
Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
if (cmMsec >= DISTANCE) // if no one within distance of PEZ close mouth
{
myservo.write(CLOSE_POS); // tell servo to go to CLOSE/UP
delay(15); // waits 15ms for the servo to reach the position
}
if (cmMsec <= DISTANCE) // if someone is within distance, open mouth
{
myservo.write(OPEN_POS); // tell servo to go to OPEN/BACK
delay(WAITING); // waits sometime to allow grabbing of candy pellet
}
}