Tutorials
May 29, 2023

Build a confetti bot with a Raspberry Pi

Written by
Hazal Mestci
Developer Advocate

Are you tired of boring celebrations with the same old confetti popping techniques? You can now design your very own confetti-popping robot with a big red button that will make your parties a colorful blast, literally! With this robot, you’ll become the life of the party with just one press of a button. Whether it’s a birthday party, a wedding, a company launch, or just a random Wednesday night, this robot will add an extra dimension of fun and excitement to any event.

In this tutorial, you’ll learn how to build your very own confetti bot using Viam. This robot turns a motor when you press a button, which then sets off a confetti cannon and makes it rain confetti.

You can expand on this project to turn a motor based on other types of inputs, such as when a specific sensor goes high.

Requirements

Hardware

Software

Set up your hardware

3D Print the enclosure

3D print the enclosure using these STL files. If you are using a different confetti cannon, you may need to adjust the size of the 3D prints to your confetti cannon size, as the wall thickness of the holder changes between brands. The same applies for the motor, if you are using a different model, you can adjust the size for the motor so the holder fits your motor head.

You can either laser cut or 3D print the side panels depending on your liking, and which machine you have access to.

Wire your motor, motor driver, board, and the battery

First, attach your motor with screws to the middle of the enclosure. Attaching the motor first makes wiring the rest easier.

Because of the model we have, we only used two screws for the top, but some motors require four which is why the enclosure has four holes.

The motor shown next to the enclosure before being installed.

The motor being installed inside the enclosure with screws on top.

Now wire all of the components according to the wiring diagram:

Wiring Diagram for confetti bot

  1. Connect your motor to the motor driver. There are several wires coming out of the motor, connect the black one (ground) into Out1 terminal and the red one (power) to Out2 terminal and screw them in tightly (a 2.4mm screwdriver works well for this). After you have finished screwing the block down, make sure it is secure by gently tugging on the wire. If it is not secure, the wire will come out. You can leave the other wires disconnected.
  2. Connect your Raspberry Pi to the motor driver. Since you are using Out1 and Out2 on the driver, you need to use the corresponding inputs (In1 and In2), as well as a PWM pin. Select three available GPIO pins on the Raspberry Pi and connect them to the ENA (green wire in the wiring diagram), In1 (yellow wire in the wiring diagram), and In2 (orange wire in the wiring diagram) pins on the motor controller. Take the black safety cap out from the motor driver so you can use the ENA pin. Make sure to remember which GPIO pin on the Raspberry Pi you connected to each of these inputs. In our case, we connected Pin 11, 13, and 15 respectively.
  3. Connect your button to the Raspberry Pi. One of the connections goes to a 3.3V pin on the Raspberry Pi, and the other needs to be connected to a GPIO pin. We used Pin 1 for 3.3V (blue wire in wiring diagram). Make sure to record which GPIO pin you connect to since you will be using this in your machine configuration. We used Pin 37 (white wire in wiring diagram).
  4. Now you can connect the button to your 12V battery, use a black alligator clip to connect the ground of the button switch to the ground of the battery. Then from the same ground of the battery, use a black wire to connect to the ground pin on the motor driver. The ground of the motor driver will be shared with the Raspberry Pi, and your battery. Find a ground pin on your Raspberry Pi and connect it to the ground pin on the motor driver, allowing two pins to share the ground. Screw them tight with a screwdriver. We used Pin 6 on the Raspberry Pi as our ground pin, but you can use any Raspberry Pi ground pin.
  5. Now that you have grounded your robot, you can connect the motors to power. Use a red alligator clip to connect the battery to the button switch, and another red alligator clip to connect the battery to the 12V input on the motor driver.
  6. Your robot wiring is now complete! Put the microSD card in the Raspberry Pi and turn it on by plugging it to the wall.

Closeup photo of the motor driver after all the wiring to the motor, the board and the battery.
Motor, motor driver, board and battery all wired to each other.

Configure your confetti bot with the Viam app

Now that your robot is wired, you need to configure it on the Viam app before you can program it.

In the Viam app, create a new machine and give it a name. We named ours ConfettiBot.

A machine page header in the Viam app, its under the location work, and named ConfettiBot.

Navigate to the CONFIGURE tab of your machine’s page in the Viam app.

Configure the Pi as a board

Click on the Components subtab and click Create component in the lower-left corner of the page.

Click the + icon next to your machine part in the left-hand menu and select Component. Select the board type, then select the pi model. Enter party as the name and click Create. You can name your board whatever you want as long as you refer to it the same way in your code; we picked for fun. Your board configuration should now look like this:

Board component configured in the Viam app, the component tab is named party, with a type attribute board and model attribute pi.

Configure the motor

Click the + icon next to your machine part in the left-hand menu and select Component. Select the motor type, then select the gpio model. Enter start as the name for your  motor component and click Create. We named it “start” to refer to the button being pressed, but this name is up to you as long as you remember the name and use the same name in the code later.

After clicking Create, there is a pin assignment type toggle. Select In1/In2 since that is compatible with the type of input our motor controller expects. In the dropdowns for A/In1 and B/In2, choose 13 GPIO 27 and 15 GPIO 22 and for PWM choose 11 GPIO 17 corresponding to our wiring.

In the Board dropdown within attributes, choose the name of the board the motor controller is wired to (“party”). This will ensure that the board initializes before the motor driver when the robot boots up.

Motor component configured in the Viam app, the component tab is named start, with a type attribute motor and model attribute gpio. It has the attributes as of the board as party, encoder as non-encoded, max rpm as 1000, component pin assignment type as In1/In2, enable pins as neither, a/In1 as 13 GPIO 27, b/In2 as 15 GPIO 22, pwm as 11 GPIO 17.

Click Save Config at the bottom of the screen.

Let’s test our configuration from the Control tab. Go to the board panel, set the pin connected to your motor (in our case pin 37) to high, and your motor should turn.

Attach components to the enclosure

Now that you have tested your motor in the app and confirmed that everything works, you can finish assembling your confetti bot.

  1. Attach the motor driver to the enclosure wall with four M2.5 x 16mm screws and M2.5 nuts.
Back view of the motor driver being installed inside the 3D printed enclosure with screws.
Front view of the motor driver being installed inside the 3D printed enclosure with screws.
  1. Slip the Raspberry Pi into the designated slot in the enclosure. Be careful of the wires during this so they don’t pop off.
Back view of the Raspberry Pi being installed inside the 3D printed enclosure within the designated slot.
Front view of the Raspberry Pi being installed inside the 3D printed enclosure within the designated slot.
  1. Add the confetti cannon to the enclosure by tightening the 3D printed holder around the confetti socket using one M2.5 x 16mm screw and a M2.5 nut. Then connect the enclosure to the front panel.
Front photo of the confetti cannon attached to the 3D printed parts.
Side photo of the confetti cannon attached to the 3D printed parts.
  1. Next, attach the 3d-printed circular holder to the base of the confetti cannon to hold its place, then to the motor. Secure the two together with a screw from the side. Depending on your motor size, you may need a different screw size.
The 3D printed black piece attached to motor with a screw securing it in place over the motor head.
Motor attachment with a 3D printed black piece wrapped around the confetti cannon secured over the motor head.
Motor attachment with a 3D printed black piece wrapped around the confetti cannon secured over the motor head attached to the enclosure.
  1. Add the front section you just built to the rest of the enclosure using M2.5 x 16mm screws and M2.5 nuts. Make sure to do this step before closing the side walls to be able to access the slots for screws.
Side view photo of the front section attached to the rest of the enclosure.

Optional: Laser cut or print the sides

Laser cut the sides of the enclosure and attach with screws. You can find the designs on Viam Labs’ GitHub.

If you don’t have a laser cutter, you can 3D print the sides instead, or leave them empty.

Lasercut side attached to the enclosure, with few cables going out of the enclosure.

Final design

The final design, fully wired and put together looks like this:

Top view of the final assembly of the confetti cannon and 3D printed/laser cutted pieces, with a battery and a red button on a white table.
Side view of the final assembly of the confetti cannon and 3D printed/laser cutted pieces, with a battery and a red button on a white table.

Write Python code to control the confetti bot

The following section explains the code for the confetti bot. The completed code for this project is available on GitHub. If you copy the code from this link, don’t forget to change your machine address and secret so it connects to your own confetti robot.

Navigate to the CONNECT tab on the Viam app, select Python as the language, and copy the sample code. Paste this into a new Python file in your favorite code editor to connect to your machine.

API KEY AND API KEY ID: By default, the sample code does not include your machine API key and API key ID. We strongly recommend that you add your API key and API key ID as an environment variable and import this variable into your development environment as needed. To show your machine’s API key and API key ID in the sample code, toggle Include secret on the Code sample tab. You can also see your API key and API key ID on your machine’s Security tab.

CAUTION: Do not share your API key or machine address publicly. Sharing this information could compromise your system security by allowing unauthorized access to your machine, or to the computer running your machine.

At the top of the code, your board and motor components are imported:

from viam.components.board import Board
from viam.components.motor import Motor

In your main function add the following code, which instantiates a variable party as the board and start as the motor:

party = Board.from_robot(robot, "party")
# Note that the pin supplied is the pin we use. Please change this to the pin
# you are using.
party_return_value = await party.gpio_pin_by_name("37")
print(f"party gpio_pin_by_name return value: {party_return_value.get()}")

start = Motor.from_robot(robot, "start")
start_return_value = await start.is_moving()
print(f"start is_moving return value: {start_return_value}")

The only other code you need to add to your main function is a while loop to check if the button is being pressed. Copy this code and add it to your own code within the main function block:

while True:
    print(party_return_value.get())
    while (await party_return_value.get()):
        await start.set_power(.8)
        await asyncio.sleep(0.1)
        if not (await GPIO.get()):
            break
    await start.set_power(0)

This is all the code you need to activate the gpio pin and turn the confetti cannon that is attached to your motor!

Next steps

In this tutorial, you learned how to turn a motor when a button is pressed and set off a confetti cannon. You could use this same concept for any simple robot that involves turning a motor when a pin on the Raspberry Pi goes “high.” One example of this logic would be connecting a PIR sensor to a Raspberry Pi. You could then make a motor turn whenever you sense a person walking by.

For more robotics projects, check out our other tutorials.

Have questions, or want to meet other people working on robots? Join our Community Discord.

On this page

Get started with Viam today!