Introduction: Autonomous Pumpkin Candy Dispenser: Socially Distanced Pumpkin

Halloween will be different this year! This project allows for automatic hands-free candy distribution using a lego-pumpkin as the candy container and an Arduino/Lego Power Functions arm.



Velcro (scissors for cutting)


1 - USB Cable to connect Arduino to Computer

1 - 9 Volt battery with wire connector

1 - 330 ohm resistor

1 - Arduino Nano

1 - Breadboard

1 - Infrared Transmitter

4 - wires (male one end and female other end)

1 - Ultrasonic sensor

Lego Electronics:

2 - Medium Motors

1 - Extra Large Motor

2 - Infrared Receivers

1 - Lego Battery


See list of lego parts

Note: other pumpkin-related colors (e.g., tan, dark tan) may be used in lieu or Orange

Step 1: Build the Pumpkin

Follow the steps (see photos 1-15) to create the body (see photos 1-7) of the pumpkin and the top (see photos 8-15) of the pumpkin.

Step 2: Build the Arm

Follow the steps (see photos 1-44) to create the moving arm (see photos 45-51 completed arm from back, front, left, and right sides) for the project.

Step 3: Add the Arduino and Electronics

Now that you have the legos portion completed, it is time to add the Arduino and other electronics parts.

1) Attach wire cap to 9-volt battery and Arduino to the breadboard (see photo 1).

2) Attach the ultrasonic transmitter to the breadboard (short negative leg to ground row and long positive leg to empty row) and the 330 ohm resistor to the bread (one leg to empty row and one leg to row D11. Photo 2 shows a close up view.

3) Connect female ends of four wires to ultrasonic sensor (yellow to VCC pin, maroon to ground pin, green to trig pin, and blue to echo pin). Photo 3 shows a close up view.

4) Connect male ends of four wires to breadboard (VCC pin to 5V row, ground pin to ground row, trig pin to D2 row, and echo pin to D3 row). Photos 4 and 5 show close up views.

5) When connecting 9 volt battery to Arduino, connect negative to ground and positive to Vin (see photo 6).

6) Apply felt side of velcro to each wide side of the 9 volt battery. Apply hook side of velcro to bottom of breadboard and the top of the 2x4 plate. Attach breadboard to battery and battery to 2x4 plate (see photo 7).

7) Attach 5-2x4 bricks together. Attach 2x4 plate (the one attached to battery) to top of 2x4 bricks (see photo 8).

8) Attach 3-2x4 bricks together. Attach 3 modified tiles with clip on top at shown in photo 9.

9) Slip ultrasonic sensor into the clips (see photo 10).

Step 4: Code the Arduino


If you are not familiar with writing Arduino Code or using the Lego Power Functions Library to control Lego Motors, please read the "How to Move Lego Motors Autonomously Using Arduino" Instructable (steps 1-5):

Arduino Software Set Up

Power Function Library Set Up

Infrared Transmitter Set Up

Write Arduino Code

Writing Power Function Motor Code

Writing Project Code for Arduino

Below is the code for the Autonomous Pumpkin Candy Dispenser (Social Distancing) Project. It is also available for download (see file 1). Note that the delay command on the line below each lego.singleOutput() command determines how long the motor runs. So for the extra large turntable motor, it determines how far the turntable turns. The goal is 90 degree turns; however, your exact time may defer from mine. It is important to experiment and tweak the code accordingly. For the medium motors; one motor bends/extends the elbow (the delay determines how much it bends/extends) and the second motor closes/opens the grip feature (the delay determines how much it opens and closes).


long duration; //Time from Sonar sensor - rebound - return to to Sonar sensor

long distance; // duration converted to distance

const int trigPin = 2; //sending sonar sensor connection

const int echoPin = 3; // receiving sonar sensor connection

LEGOPowerFunctions lego(12); //transmitter connection

void setup() {

pinMode(trigPin, OUTPUT); //pinMode for sonar sensor

pinMode(echoPin, INPUT); //pinMode for sonar sensor


void loop(){

digitalWrite(trigPin, LOW); //Sonar Set up

delayMicroseconds(5); //Sonar Set up

digitalWrite(trigPin, HIGH); //Sonar Set up

delayMicroseconds(10); //Sonar Set up

digitalWrite(trigPin, LOW); //Sonar Set up

duration = pulseIn(echoPin, HIGH); //duration of send/receive sonar

distance = (duration/2) * 0.0135; //divide duration by two since time to send and retrieve

if(distance < 36) {

lego.SingleOutput(0, PWM_REV7, RED, CH1); //Elbow bend

delay(5000); //Pause program

lego.SingleOutput(0, PWM_BRK, RED, CH1); //Stop

delay(1000); //pause program

lego.SingleOutput(0, PWM_FWD3, BLUE, CH1); // Grip close

delay(1500); //Pause program

lego.SingleOutput(0, PWM_BRK, RED, CH1); //Stop

delay(1000); //Pause program

lego.SingleOutput(0, PWM_FWD7, RED, CH1); //Elbow extend

delay(5000); //Pause program

lego.SingleOutput(0, PWM_BRK, RED, CH1); //Stop

delay(1000); //pause program

lego.SingleOutput(0, PWM_REV2, RED, CH2); //turntable left

delay(800); //Pause program

lego.SingleOutput(0, PWM_BRK, RED, CH2); //Stop

delay(1000); //pause program

lego.SingleOutput(0, PWM_REV3, BLUE, CH1); //Grip open

delay(2200); //Pause program

lego.SingleOutput(0, PWM_BRK, BLUE, CH1); //Stop

delay (1000);//Pause program

lego.SingleOutput(0, PWM_FWD2, RED, CH2); //turntable right

delay(800); //Pause program

lego.SingleOutput(0, PWM_BRK, RED, CH2); //Stop

delay(3000);//Pause program



Step 5: Watch How It Works

As the user approaches the Automatic Pumpkin Candy Dispenser (Social Distancing) - the arm mechanism lowers the arm and grabs for candy (see video 1).

The arm mechanism secures the candy, raises the arm, turns the arm towards the user, opens its grip to release the candy, and then returns to its wait position for the next user (see video 2).

The two photographs show the complete set from the front and from the back (see photos).

Pumpkin Challenge

Participated in the
Pumpkin Challenge