Foam Dart Launcher Robot Tutorial

Use Viam to configure a rover and launch a foam dart activated by GPIO pins on a Raspberry Pi.

Introduction

This tutorial will show you how to build your very own foam dart launcher robot using Viam, a Raspberry Pi, a generic foam dart launcher with foam darts, a USB camera, a solenoid, a relay, and a motor controller. This robot will be able to move around and launch foam darts.

This project is a great place to start if you are new to building robots, have a love for Nerf toys, an occasion to target something and launch a dart at it, or if you just want to troll your friends. Don’t forget to be careful and have fun!

Figure: Robot launching a dart from a foam dart launcher taped to the robot.
Robot launching a dart from a foam dart launcher taped to the robot.

What You’ll Need for This Tutorial

You will need the following hardware, software, tools, and consumables to complete this project:

Hardware and software requirements

Tools and Consumables

  • Solder (optional)
  • Small flathead screwdriver
  • Cutting pliers (flush-cutting pliers preferred)
  • Electrical tape
  • Elastic/rubber bands

How to Assemble Your Hardware

Motor Controller Setup

A motor controller is a piece of hardware that takes digital signals from the Raspberry Pi and sends power to the motors accordingly. For this setup we have one dual motor controller for the two motors.

Figure: Dual H-bridge motor controller.
Dual H-bridge motor controller with four signal wires going in and two sets of motor power wires coming out.

We need only worry about OUT1 through OUT4, IN1 through IN4, 12V, and ground.

  1. Attach left to the motor controller.
    1. Use a small flathead screwdriver to loosen the Out1 and OUT2 screw terminals.
    2. Place the red motor wire into OUT1
    3. Place the black motor wire into OUT2
    4. Use the flathead screwdriver to tighten the terminals to firmly hold the wires in place. Note: Tightly twisting each stripped wire end and then tinning it makes it easier to insert and secure the jumper in screw terminal connectors.
  2. Connect the control wires IN1 and IN2 to your Raspberry Pi.
    1. The example robot has IN1 -> pin 11, IN2 -> pin 13
  3. Repeat steps 1-2 for the right motor using OUT3, OUT4, IN3, IN4
    1. The example robot has IN3 -> pin 16, IN4 -> pin 18
  4. Connect the external power that will supply power to the motors.
    1. In the example we have 4 AA batteries connected to the motor driver giving us 6V of power for the motors.

Camera Setup

This is as easy as plugging the camera into a USB slot on your Pi. We’ll configure the camera in the Viam app in later steps.

Relay/Solenoid Setup

The solenoid component actuates the foam dart launcher trigger. The relay works as a switch to turn on and off the solenoid. This allows us to activate the foam dart launcher via a GPIO pin on the board.

Figure: Power relay.
Power relay.
  1. Connect the solenoid to the relay.
    1. Connect a wire to the Normally Open (NO) terminal connector.
    2. Connect the other end to a ground pin on the Raspberry Pi.
  2. Connect the Relay COMmon pin.
    1. Connect the COM pin to the 3.3V power of the Raspberry Pi.
  3. Connect VCC (DC+) and ground (DC-) .
    1. Connect DC+ to the 5V of the Raspberry Pi.
    2. Connect DC- to ground on the Raspberry Pi.
  4. Connect IN.
    1. Connect IN to a GPIO pin on the Raspberry Pi.
    2. For this example, we've connected to pin 37.

Assemble Solenoid/Foam Dart Launcher

Figure: Foam dart launcher/Solenoid Assembly.
Solenoid attached to the orange foam dart launcher using rubber bands and electrical tape.
  1. Modify the foam dart launcher to make room for the solenoid. Using cutting pliers, we cut the trigger guard off of the front as seen in the picture above.
  2. Test that the solenoid has enough power to press the trigger when the foam dart launcher is loaded.
  3. If the solenoid is not strong enough we can:
    • Wrap the trigger with rubber bands to make the trigger easier to activate1.
    • Increase the voltage to the solenoid. Right now it receives 5 volts, but some solenoids can support up to 12 volts. If necessary, you can connect the solenoid to another power supply such as a 9 volt battery. Check the details of your solenoid2.
  4. Tape the solenoid in such a manner that it makes good contact with the trigger when activated via the relay.
  5. Attach all of your components to the base.
Figure: Foam dart launcher Taped to the Rover
Foam dart launcher taped to a rover base using electrical tape.

Configure Your Foam Dart Launcher Robot with the Viam App

Create a new robot in the Viam app and give it a name.

Pi Configuration (board)

Add your board with the name board, type board, and model pi. Click Create Component.

Figure: Board Component Attribute Pane
Viam App Board component attribute pane.

You can name your board whatever you want, we picked board for simplicity. Just remember to use that name consistently in the following steps.

Motors Configuration (Left and Right)

Add the left motor with the name left, type motor, and model gpio, then add the right motor with the name right, type motor and model gpio. After clicking Create Component you’ll see the Component Pin Assignment toggle. Select In1/In2 since that is compatible with the input type our motor controller expects. In the A/In1 and B/In2 drop-downs, choose 11 GPIO 17 and 13 GPIO 27, respectively, for the left motor, and 16 GPIO 23 and 18 GPIO 24, respectively, for the right motor.

Select the name of the board the motor controller is wired to (“board”) from the Board drop-down.

Figure: Left Motor Component Configuration Pane
Motor Component Attribute Pane (left motor).
Figure: Right Motor Component Configuration Pane
Motor Component Attribute Pane (right motor).

Click SAVE CONFIG at the bottom of the screen. Then go to the CONTROL tab where you will now see the buttons you can use to control the motors:

Figure: Left and Right Motor Configuration Panes
Left and right motor configuration panes.

Now, you can move around your wheels separately. Let’s add a base to be able to control them together.

Base Configuration

Add your motors to the base so you can move them around with your keyboard.

Here we must specify the wheel circumference and the width between the wheels (measured centerline to centerline). Depending on your rover, you can change these numbers. Don’t forget to make the base depend on the left, right, and the board components.

Figure: Base Component Attribute Pane
Base Component Attribute Pane.

Now let’s add a camera to watch the video stream on the control panel as you move your rover.

Camera configuration

Add your USB camera as a webcam. Please refer to How to Configure a Camera > Connect and Configure a Camera for complete instructions.

Figure: Camera Component Attribute Pane
Camera Component Attribute Pane.

If you go back to the CONTROL tab, you will see your base and camera.

Figure: Camera View from the Base Component's Keyboard Tab.
Camera view from the Base component's keyboard tab.

Toggle the switch under Keyboard Disabled to enable keyboard control, then use the W, A, S, and D keys on your keyboard to move your rover around. You can view the camera stream at the same time. If you scroll down, you will see your camera as a component. From the camera component pane, you can change the camera’s refresh frequency and also export screenshots from your camera.

Figure: Camera View from the Camera Component Pane.
Camera view from the Camera component pane.

Toggling GPIO Pin States

The board card in the CONTROL tab provides a way to change the state of the pin connected to the solenoid. If you click the board component, you will be able to see your GPIO pin and its current state. In the Set part of the GPIO section, select pin 37 (the pin we wired to control the solenoid). If you set the pin state to high, the solenoid will actuate. If you set it to low, it should deactivate the solenoid. Since we already assembled the robot, setting pin 37 to high launches your foam dart launcher if its loaded.

Figure: Board component, GPIO section showing Low selected on the Pin State drop-down
Board component, G P I O section showing Low selected on the Pin State drop-down.
Figure: Board component, GPIO section with pin 37 set to High.
Board component, G P I O section with pin 37 set to High.
Figure: Board component, GPIO section showing High selected on the Pin State drop-down
Board component, G P I O section showing High selected on the Pin State drop-down.

Debug Information

If any component fails to appear when connecting to the robot in the Viam app, check the LOGS tab for potential errors.

Final Code

The raw JSON for the entire configuration can be found here if you wish to copy it into your config: https://gist.github.com/mestcihazal/7683fbdd0975579b44295b3208757b5a

Control via the Viam App

Controlling the Base

  1. Navigate to Viam app -> base component
  2. Enable keyboard controls
  3. Drive your robot around!

Activating the Foam Dart Launcher

  1. Make sure your foam dart launcher is loaded and ready to go.
  2. When you are ready, navigate to the Viam app -> CONTROL tab -> board component and set the IN pin (pin 37 in our example) to high to activate.

You can see the demo video of it in action here:

Summary

In this tutorial, you learned how to create a remotely-controlled foam dart launching robot activated by the GPIO pins on a Raspberry Pi using Viam. You could use this same concept as the basis for a security robot that launches darts at people if they enter your room, a Nerf ball blaster dog toy, a kitten treat shooter for cats to fetch in excitement, you name it! You could even add object detection and machine learning and activate the launcher only when the camera sees a specific object or person.

If you are looking for projects that would be a great next step in your journey of robots creation, we recommend that you check out the following tutorials:

If you have any issues getting Viam set up or get stuck, let us know on the Viam Community Slack and we’ll be happy to help you get up and running. The Viam Community Slack is also a great place to connect with other developers learning how to build robots.


  1. If you use the rubber band method, you may need to pull the rubber bands away from the trigger when reloading the foam dart launcher so it can reset and load properly. Try activating the solenoid manually to ensure that it hits the foam dart launcher trigger in the right spot. ↩︎

  2. If you choose to increase the voltage, you must connect VCC and ground (DC+ and DC-) to the new voltage source rather than connecting them to the Raspberry Pi as described in Step 3 of Assemble Solenoid/Foam Dart Launcher↩︎