In this tutorial, we'll transform a basic dry food dispenser into an automated treat hub using a Raspberry Pi, a webcam, and some computer vision; no coding necessary! Whether you're working from home and want to track your snacking habits or just love the idea of building your own smart appliances, this project is a perfect entry point into the world of hardware hacking.

What You'll Build

smart snack machine

Prerequisites

What You'll Learn

What You'll Need

Before embedding the Raspberry Pi in the food dispenser, you will prepare it with an operating system.

The Raspberry Pi boots from a USB flash drive (or microSD card). You need to install Raspberry Pi OS on a storage device that you will use with your Pi. For more details about alternative methods of setting up your Raspberry Pi, refer to the Viam docs.

Install Raspberry Pi OS

  1. Connect the USB flash drive (or microSD card) to your computer.
  2. Download the Raspberry Pi Imager and launch it. raspberry pi imager
  3. Click CHOOSE DEVICE. Select your model of Pi, which is Raspberry Pi 4.
  4. Click CHOOSE OS. Select Raspberry Pi OS (64-bit) from the menu.
  5. Click CHOOSE STORAGE. From the list of devices, select the storage device you intend to use in your Raspberry Pi. raspberry pi storage
  6. Configure your Raspberry Pi for remote access. Click Next. When prompted to apply OS customization settings, select EDIT SETTINGS.
  7. Check Set hostname and enter the name you would like to access the Pi by in that field, for example, test.
  8. Select the checkbox for Set username and password and set a username (for example, your first name) that you will use to log into the Pi. If you skip this step, the default username will be pi (not recommended for security reasons). And specify a password.
  9. Connect your Pi to Wi-Fi so that you can run viam-server wirelessly. Check Configure wireless LAN and enter your wireless network credentials. SSID (short for Service Set Identifier) is your Wi-Fi network name, and password is the network password. Change the section Wireless LAN country to where your router is currently being operated. raspberry pi hostname username and password
  10. Select the SERVICES tab, check Enable SSH, and select Use password authentication. raspberry pi enable SSH
  11. Save your updates, and confirm YES to apply OS customization settings. Confirm YES to erase data on your storage device. You may also be prompted by your operating system to enter an administrator password. After granting permissions to the Imager, it will begin writing and then verifying the Linux installation to your storage device.
  12. Remove the storage device from your computer when the installation is complete.

Connect with SSH

  1. Place the storage device into your Raspberry Pi and boot the Pi by plugging it in to an outlet. A red LED will turn on to indicate that the Pi is connected to power.
  2. Once the Pi is started, connect to it with SSH. From a command line terminal window, enter the following command. The text in <> should be replaced (including the < and > symbols themselves) with the user and hostname you configured when you set up your Pi.
    ssh <USERNAME>@<HOSTNAME>.local
    
  3. If you are prompted "Are you sure you want to continue connecting?", type "yes" and hit enter. Then, enter the password for your username. You should be greeted by a login message and a command prompt. raspberry pi SSH login
  4. Update your Raspberry Pi to ensure all the latest packages are installed
    sudo apt update
    sudo apt upgrade
    

Now that you've confirmed the Raspberry Pi is set up with an operating system, you can shut it down with the following command in the SSH session on the Pi:

sudo shutdown -h now

The SSH session will close as the Raspberry Pi begins to turn off. Once the green activity light stops blinking, you can unplug the Raspberry Pi from power to prepare assembling the hardware in the next step.

It's time to get your hands dirty by taking apart the food dispenser and upgrading the internals with the Raspberry Pi and other peripherals!

Disassemble the snack dispenser

  1. Remove the food bowl from the base of the dispenser and set it aside. food dispenser without bowl
  2. Turn the dispenser upside down to access the screws on the bottom of the device. flip the machine
  3. Use the screwdriver to remove the screws in each corner. Set them aside in a safe place for later when reassembling. unscrew base
  4. Carefully, flip the device over again with the food container (hopper) facing up. Slowly, lift the container to separate it from the base of the device and expose the internal wiring. separate container from base

Prepare hardware components

  1. Using the wire strippers, cut the wires connected to the motor and limit switch on the bottom of the food container. Make sure to keep around 6 inches of wire connected to each component. This will make it easier to connect them to the other hardware peripherals later. cut motor, switch wires
  2. Using the wire strippers, cut the power (typically red) and ground (typically black) wires connected to power port at the bottom of the base. Make sure to keep around 6 inches of wire. This will make it easier to connect them to the other hardware peripherals later. power wires
  3. For each of the cut wires, use the wire strippers to expose about half a fingernail's length of metal from the plastic covering. Take your time and pull slowly to remove only the plastic. stripped wire
  4. Set the food container part aside now that it is disconnected from the base. Place the base on its front so the back of the device is facing up. Put on safety glasses and use the drill to slowly bore a hole in the base a couple inches from the bottom. Make sure you have a secure grip to prevent it from spinning while drilling. It should enter the base just above the power port. camera cable hole
  5. This hole will be used by the webcam's USB cable to connect to the Raspberry Pi once it is embedded in the food dispenser. Test it by pushing the USB end through several inches. If there are any issues, use the drill to widen the entry point with a bigger drill bit or by slowly rotating the existing bit around the inside of the hole while the drill is running, applying light pressure to strip away material. test camera entry
  6. Clean out any plastic material from the drilling process from the inside of the base and place the base with the bottom down to prepare for wiring.

Connect hardware components

wiring diagram

  1. Use the above wiring diagram to connect the hardware peripherals and power together. The stripped wires from the snack dispenser can be connected to the jumper wires using the WAGO lever nuts. WAGO connection

    Board to Power:

    Raspberry Pi

    Power Port

    Pin 2 (5V)

    Positive (red)

    Pin 9 (Ground)

    Ground (black)

    Board to Motor Driver:

    Raspberry Pi

    MAX14870 Motor Driver

    Pin 4 (5V)

    VIN

    Pin 6 (Ground)

    GND (below VIN)

    Pin 14 (Ground)

    GND (below EN)

    Pin 16 (GPIO 23)

    DIR

    Pin 18 (GPIO 24)

    PWM

    Board to Switch:

    Raspberry Pi

    Limit Switch

    Pin 2 (5V)

    Positive (red)

    Pin 9 (Ground)

    Ground (black)

    Motor to Motor Driver:

    DC Motor

    MAX14870 Motor Driver

    One wire

    M1

    Other wire

    M2

    The ordering of the motor wires are not important, which is why the naming used in the above table is vague.
  2. Feed the webcam USB cable through the entry hole you drilled earlier and connect it to any of the Raspberry Pi's USB-A ports. fully wired machine
  3. Carefully tuck the Raspberry Pi into the side of the dispenser base, keeping the wires clear of the stand-off pillars for the top container. embedded Raspberry Pi
  4. Place the top container (hopper) on the base, making sure the logo is facing the front of the device so it dispenses correctly.
  5. Sit the webcam on the top of the hopper, facing down towards the bowl (after adding the bowl back to the base). machine assembled

Nice work! 👏

Now you can connect power to the food dispenser to turn on the Raspberry Pi. Lift the top container from the base to peek at the Raspberry Pi, looking for the red power light and blinking green activity light.

In the next section, you'll configure your device in the Viam app and start controlling your hardware!

Configure your machine

  1. In the Viam app under the LOCATIONS tab, create a machine by typing in a name and clicking Add machine. add machine
  2. Click View setup instructions.
  3. To install viam-server on the Raspberry Pi device that you want to use, select the Linux / Aarch64 platform for the Raspberry Pi, and leave your installation method as viam-agent. select platform
  4. Use the viam-agent to download and install viam-server on your Raspberry Pi. Follow the instructions to run the command provided in the setup instructions from the SSH prompt of your Raspberry Pi. installation agent
  5. The setup page will indicate when the machine is successfully connected. successful toast

Configure your Raspberry Pi board

To access the GPIO pins, let's add our Raspberry Pi board to our machine in the Viam app.

  1. In the Viam app, find the CONFIGURE tab.
  2. Click the + icon in the left-hand menu and select Component. add component
  3. Select board, and find the raspberry-pi:rpi4 module. This adds the module for working with the Raspberry Pi 4's GPIO pins. Leave the default name board-1 for now.
  4. Notice adding this module adds the board hardware component called board-1. The collapsible card on the right corresponds to the part listed in the left sidebar. added board
  5. Click Save in the top right to save and apply your configuration changes.

Configure your encoder

  1. In the Viam app, find the CONFIGURE tab.
  2. Click the + icon in the left-hand menu and select Component.
  3. Select encoder, and find the single module. This adds the module for reading the limit switch as an encoder with a board's GPIO pins. Leave the default name encoder-1 for now.
  4. Notice adding this module adds the encoder hardware component called encoder-1. The collapsible card on the right corresponds to the part listed in the left sidebar.
  5. In the new encoder-1 panel, configure your component by updating the following attributes in the CONFIGURE field. This tells your encoder component to use a specific pin on a specific board (called board-1 in the Viam app).
    • Select board-1 from the dropdown under the "board" field.
    • Click the "Add pins" button to reveal the field for setting the encoder's interrupt pin
    • Set the "i" field to "22" configure encoder
  6. Click Save in the top right to save and apply your configuration changes.
  7. At the bottom of the encoder-1 panel, expand the TEST section to ensure you have configured the encoder correctly. You should see the ticks count under "GetPosition" increase when pressing down and releasing the limit switch. test encoder

Configure your motor

  1. In the Viam app, find the CONFIGURE tab.
  2. Click the + icon in the left-hand menu and select Component.
  3. Select motor, and find the gpio module. This adds the module for controlling a motor with a board's GPIO pins. Leave the default name motor-1 for now.
  4. Notice adding this module adds the motor hardware component called motor-1. The collapsible card on the right corresponds to the part listed in the left sidebar.
  5. In the new motor-1 panel, configure your component by updating the following attributes in the CONFIGURE field. This tells your motor component to use specific pins and settings on a specific board (called board-1 in the Viam app).
    • Select board-1 from the dropdown under the "board" field.
    • Click the "Show more" button to reveal additional configuration detail.
    • Select encoder-1 from the "encoder" field.
    • Set the "ticks_per_rotation" field to 100 to indicate how many ticks from the paired encoder will be counted for each full rotation of the motor.
    • Under the "Component pin assignment" section, set the "Type" field to "Direction"
    • Set the "dir" field to 16
    • Set the "pwm" field to 18 configure motor attributesconfigure motor pins
  6. Click Save in the top right to save and apply your configuration changes.
  7. At the bottom of the motor-1 panel, expand the TEST section to ensure you have configured the motor correctly and can move it using the "FORWARD" or "BACKWARD" buttons. test motor

Configure your webcam

  1. In the Viam app under the CONFIGURE tab, click the + icon in the left-hand menu and select Component.
  2. Select camera, and find the webcam module. This adds the module for working with a USB webcam. Leave the default name camera-1 for now.
  3. Notice adding this component adds the webcam hardware component called camera-1. From the Attributes section of the panel, select a video_path. select video path
  4. Click Save in the top right. This may take a moment to apply your configuration changes.
  5. At the bottom of the camera-1 panel, expand the TEST section to ensure you have configured the camera properly and see a video feed. test camera

With your hardware components confirmed working, grab your snacks to start gathering data to train a custom computer vision model in the next step.

To get started with capturing training data for the machine learning model used to automate the smart snack dispenser, fill the top container (hopper) of the dispenser with your snack of choice that you want to model to be able to identify.

Add the data manager

  1. In the Viam app, click the + icon in the left-hand menu and select Service, and then data management.
  2. Create a new Data Management service called data_manager-1. select data management
  3. Notice adding this service adds the data manager service called data_manager-1. It will default to the "Capturing" and "Syncing" toggles as enabled. view default configuration for data manager
  4. Click Save in the top right. This may take a moment to apply your configuration changes.

Capture images from the webcam

  1. In the camera-1 panel, click the "+ Add method" button under the Data capture section.
  2. For the "Method" dropdown, select "ReadImage", enter "0.25" in the "Frequency (hz)" field to capture an image every 4 seconds.
  3. Make sure the data capture toggle is "On". add data capture method
  4. Click Save in the top right. This may take a moment to apply your configuration changes.
  5. At the bottom of the camera-1 panel, expand the TEST section to view the images as they're being captured. test camera
  6. Click the "Toggle picture-in-picture" button to create a separate window of the camera view.
  7. Go to the motor-1 panel and expand the TEST section at the bottom. Use the controls to turn the motor and dispense the snacks into the bowl. dispense snacks with motor
  8. Once the bowl is full, empty the bowl back into the container and dispense more snacks. Go through this cycle a few times to ensure you have enough image data to train an accurate model.
  9. When you feel you have enough images (with a minimum of 20), you can close the picture-in-picture window of the camera and turn off data capture in the camera-1 panel.
  10. Click Save in the top right to save and apply your configuration changes.

Create a dataset of labeled images

  1. In the camera-1 panel, click the ... button in the top-right to reveal an action menu. Select "View captured data" to see all the captured images from the machine in the Data tab of the Viam app. view captured data
  2. From this filtered view of image data, click the first image to reveal a right sidebar of actions. Click the expanding arrows button in the top-left of the sidebar to display the full-screen annotation view. image action sidebarfull screen annotations
  3. In the right sidebar of Actions, click the "+" button under the TAGS section to add a tag that classifies the image as either "full" or "empty". The first time you add one of these labels you will need to select the "+ " option (where is either "empty" or "full" depending on which you're adding at that time) in the tag dropdown to create it before adding it to the image. image tagging
  4. In the right side of the Actions, click the "+" button under the DATASETS section to add the image to a dataset of images called "snacks" used for training a machine learning model. The first time you add an image to a dataset, you will need to select the "+ snacks" option in the datasets dropdown to create it before adding the image. add image to dataset
  5. Use the arrow buttons next to the image, or your computer keyboard arrow keys, to view the next captured image to tag and add to the "snacks" dataset. You can use the "Recent" list of options under the TAGS and DATASETS sections to quickly tag and add each image. recent actions list
  6. Continue tagging and adding images until you have at least 10 of each tag in the "snacks" dataset. The more images in the training data, the more accurate your model will be.
  7. Once you've completed this process, click the "snacks" dataset button in the DATASETS section of one of the images to view the full dataset.

Train a classification model

  1. From the "snacks" dataset view, you can see details about the collection of training data, including total number of images (should be at least 20) and the number of images for each tag (should be at least 10 per tag). Click the "Train model" button to start configuring a training job for your custom model. dataset details
  2. Keep the default options to train a new model using the built-in training scripts for computer vision models. Click the "Next steps" button to continue. training configuration settings
  3. Enter "snack-refill" in the Name text field. Keep the rest of the default options to train a single label classification model with the "full" and "empty" labels (based on the tags on the images in the associated "snacks" dataset). Click the "Train model" button to start the training job. training configuration settings
  4. From the "TRAINING" tab in the Viam app, click on the ID of the training job for your "snack-refill" model based on the "snacks" dataset. training jobs list
  5. From the training job view, you can see details about the active job being run in the Viam cloud infrastructure. Depending on the amount of images in the dataset, this process can take between 10 and 20 minutes. Enjoy some of your snacks as a reward for making it this far while you wait! training job details
  6. When the job completes, you will see a stream of logs and the "Completed" badge on the details page. You will also receive an email notification in case you moved on to something else in the meantime. completed training job details
  7. Click on the "snack-refill" model button under the "Model" section of the job details to view the model details in the Viam Registry. By default, it will be private to your organization. model details

With your model trained, it's time to deploy it to your Raspberry Pi and use it for classifying images from the webcam.

Add an ML model service

  1. In the Viam app, click the + icon in the left-hand menu and select Service, and then ML model.
  2. Search for a module called TFLite CPU. Then click Add module, and Create a new ML Model service called mlmodel-1. This provides the ability to run Tensorflow Lite, a.k.a LiteRT, models on the machine. add ML model
  3. Notice this creates two new items in the left sidebar. The first is your new ML Model service called mlmodel-1, and the second is the tflite_cpu module from the Registry.
  4. In the mlmodel-1 panel, click "Select model" in the Model section, search for "snacks-refill" under the My organization tab, and select your trained model from the list. add model
  5. Click Save in the top right to save and apply your configuration changes.

Add a vision service

  1. In the Viam app, click the + icon in the left-hand menu and select Service, and then vision.
  2. Search for a module called mlmodel. Then click Add module, and Create a new Vision service called vision-1. add module
  3. Notice adding this service adds the vision service called vision-1. From the ML Model section of the panel, select mlmodel-1. select ML model for vision service
  4. Save your changes in the top right and wait a few moments for the configuration changes to take effect.
  5. At the bottom of the vision-1 panel, expand the TEST section to ensure you have configured the service properly and can see the classifications table and its label predictions. test vision service

Add a refill-controller service

  1. In the Viam app, click the + icon in the left-hand menu and select Service, and then generic.
  2. Search for a module called refill-controller:refiller. Then click Add module, and Create a new refill-controller service called refiller. add module
  3. Notice adding this service adds the generic service called refiller.
  4. In the new refiller panel, configure the service by adding the following attributes in the CONFIGURE field. This tells the refill-controller service to use specific motor and camera components as well as a vision service.
    {
      "camera_name": "camera-1",
      "motor_name": "motor-1",
      "vision_name": "vision-1"
    }
    
    configure refiller service
  5. Save your changes in the top right and wait a few moments for the configuration changes to take effect.
  6. Test out the service by emptying the snack bowl and watch the machine automatically dispense more snacks until the bowl is full.

Congratulations! You've just transformed a simple food dispenser into a smart device that makes snacking both fun and mindful. While this project serves up treats, it's also given you a taste of key hardware hacking concepts - from motor control to computer vision to hardware automation.

What You Learned

Building advanced scenarios with Viam and the smart snack dispenser

At this point, you have configured and tested your machine to recognize a snack food and automatically refill the bowl. Consider adding features like:

Real-world applications and projects for smart snack dispensers

This project is fun to run at home, and it has practical uses in other places as well:

Related Viam Resources