Part 1: Prerequisites and setup

Part 1 of 5 | ⏱️ 15-20 minutes

In this tutorial series, you’ll learn how to create a custom module to add support for hardware that isn’t already supported by existing registry modules.

What you’ll do in this part

  • Install and authenticate the Viam CLI
  • Set up a test machine (optional but recommended)
  • Verify your development environment (Python 3.11+ or Go 1.20+)
  • Write a test script to verify hardware connectivity
  • Understand the module architecture

What you’ll build

Throughout this tutorial series, you’ll create a hello-world module that demonstrates two common module capabilities:

  1. Camera model: Returns an image from a configured file path
  2. Sensor model: Returns a random number reading

This example shows how to implement multiple resources within a single module.

Prerequisites

Before you begin, make sure you have the following:

1. Install and authenticate the Viam CLI

The Viam CLI is required to generate module code and upload modules to the registry.

Install the Viam CLI on your development machine:

To download the Viam CLI on a macOS computer, install brew and run the following commands:

brew tap viamrobotics/brews
brew install viam

To download the Viam CLI on a Linux computer with the aarch64 architecture, run the following commands:

sudo curl -o /usr/local/bin/viam https://storage.googleapis.com/packages.viam.com/apps/viam-cli/viam-cli-stable-linux-arm64
sudo chmod a+rx /usr/local/bin/viam

To download the Viam CLI on a Linux computer with the amd64 (Intel x86_64) architecture, run the following commands:

sudo curl -o /usr/local/bin/viam https://storage.googleapis.com/packages.viam.com/apps/viam-cli/viam-cli-stable-linux-amd64
sudo chmod a+rx /usr/local/bin/viam

You can also install the Viam CLI using brew on Linux amd64 (Intel x86_64):

brew tap viamrobotics/brews
brew install viam

Download the binary and run it directly to use the Viam CLI on a Windows computer.

If you have Go installed, you can build the Viam CLI directly from source using the go install command:

go install go.viam.com/rdk/cli/viam@latest

To confirm viam is installed and ready to use, issue the viam command from your terminal. If you see help instructions, everything is correctly installed. If you do not see help instructions, add your local go/bin/* directory to your PATH variable. If you use bash as your shell, you can use the following command:

echo 'export PATH="$HOME/go/bin:$PATH"' >> ~/.bashrc

For more information see install the Viam CLI.

Verify installation:

viam version

You should see output like: viam version 0.x.x

Authenticate to Viam:

viam login

This will open a new browser window with a prompt to start the authentication process. If a browser window does not open, the CLI will present a URL for you to manually open in your browser. Follow the instructions to complete the authentication process.

Use your organization, location, or machine part API key and corresponding API key ID in the following command:

viam login api-key --key-id <api-key-id> --key <organization-api-key-secret>

2. Set up a machine for testing

While you can develop a module without a machine, you’ll need one to test your module.

Add a new machine on Viam. On the machine’s page, follow the setup instructions to install viam-server on the computer you’re using for your project. Wait until your machine has successfully connected to Viam.

Already have a machine? Great! You’ll configure it in Part 4.

3. Development environment

Your module can be written in Python or Go. Choose your preferred language and verify your environment:

Required: Python 3.11 or newer

Check your version:

python3 --version

You should see: Python 3.11.x or higher

Need to install Python 3.11+?

  • macOS: brew install python@3.11
  • Ubuntu/Debian: sudo apt install python3.11
  • Windows: Download from python.org

Required: Go 1.20 or newer

Check your version:

go version

You should see: go version go1.20 or higher

Need to install Go? Download from go.dev

Write a test script

Before writing a module, it’s helpful to write a simple test script to verify you can control your hardware.

For the example module, this script opens an image file and prints a random number.

Create a file named test.py:

import random
from PIL import Image

# Open an image
img = Image.open("example.png")
img.show()

# Return a random number
random_number = random.random()
print(random_number)

Test it:

python3 test.py

Create a file named test.go:

package main

import (
  "fmt"
  "math/rand"
  "os"
)

func main() {
  // Open an image
  imgFile, err := os.Open("example.png")
  if err != nil {
    fmt.Printf("Error opening image file: %v\n", err)
    return
  }
  defer imgFile.Close()
  imgByte, err := os.ReadFile("example.png")
  fmt.Printf("Image file type: %T\n", imgByte)
  if err != nil {
    fmt.Printf("Error reading image file: %v\n", err)
    return
  }

  // Return a random number
  number := rand.Float64()
  fmt.Printf("Random number: %f\n", number)
}

Test it:

go run test.go

Understanding module architecture

A module is essentially a packaged wrapper around your hardware control code. The module:

  1. Registers with viam-server when it starts
  2. Validates user configuration when they add your component
  3. Implements standard Viam API methods (like GetImages for cameras)
  4. Communicates with your hardware using your control code

In Part 2, you’ll choose which Viam API best matches your hardware’s capabilities.

What you’ve accomplished

Environment ready:

  • Viam CLI installed and authenticated
  • Machine set up for testing (optional)
  • Development environment verified (Python 3.11+ or Go 1.20+)
  • Test script working

Understanding:

  • What modules are and why you’d create one
  • Basic module architecture

Next steps

Now that your environment is ready, continue to Part 2: Choose an API and generate code to select the right API for your hardware and generate your module structure.


Tutorial navigation: