Previous
Part 1: Setup
Part 2 of 5 | ⏱️ 15 minutes
A module wraps your hardware driver in a standardized Viam API. This means:
You can think of a module as a packaged wrapper around a script. The module takes the functionality of the script and maps it to a standardized API for use within the Viam ecosystem.
Review the available component APIs and choose the one whose methods map most closely to the functionality you need.
If you need a method that is not in your chosen API, you can use the flexible DoCommand (which is built into all component APIs) to create custom commands. See Run control logic for more information.
| Hardware Type | Viam API | When to Use | Example Devices | Key Methods |
|---|---|---|---|---|
| Image capture | Camera | Captures still images or video streams | Webcam, Raspberry Pi Camera, RTSP stream, CV pipeline | GetImages, GetPointCloud |
| Measurement sensor | Sensor | Reads numeric values or data | Temperature, IMU, distance, air quality, GPS | GetReadings |
| Single motor | Motor | Controls one motor independently | DC motor, stepper motor, servo | SetPower, GoTo, GetPosition |
| Multi-motor platform | Base | Coordinated control of multiple motors | Rover, mobile robot, wheeled platform | MoveStraight, Spin, SetVelocity |
| Robotic arm | Arm | Multi-joint arm with kinematics | Robot arm, manipulator | MoveToPosition, MoveToJointPositions |
| Other hardware | Generic | Doesn’t fit standard categories | Custom actuators, unique hardware | DoCommand only |
Step 1: Identify your hardware’s primary function
Step 2: Check if an existing API provides the methods you need
Step 3: For multiple functions, create multiple models
Hardware capabilities:
API mapping:
GetImages method)GetReadings method)Result: Two models in one module
Start with the Generic API and migrate later if needed. You’re not locked into your initial choice.
Use the Viam CLI to generate template files for your module. You can work on your module either on the device running viam-server or on another computer.
Run the module generate command in your terminal:
viam module generate
The CLI will prompt you for several configuration options:
For the hello-world module, you can skip the interactive prompts by providing all options on the command line:
viam module generate --language python --model-name hello-camera \
--name hello-world --resource-subtype=camera --public false \
--enable-cloud true
viam module generate --language go --model-name hello-camera \
--name hello-world --resource-subtype=camera --public false \
--enable-cloud true
The CLI only supports generating code for one model at a time. You’ll add the sensor model in Part 5.
The generator creates a directory containing stub files for your modular component. Here’s what you need to know about each file:
hello-world/
└── src/
| ├── models/
| | └── hello_camera.py
| └── main.py
└── README.md
└── <org-id>_hello-world_hello-camera.md
└── build.sh
└── meta.json
└── requirements.txt
└── run.sh
└── setup.sh
These are where your main work happens:
src/models/hello_camera.py
GetImages, etc.)requirements.txt
setup.shPillow for image processingREADME.md and <org-id>_hello-world_hello-camera.md
meta.json
src/main.py
setup.sh, build.sh, run.sh
.github/workflows/build.yml
hello-world/
└── cmd/
| ├── cli/
| | └── main.go
| └── module/
| └── main.go
└── Makefile
└── README.md
└── <org-id>_hello-world_hello-camera.md
└── go.mod
└── module.go
└── meta.json
module.go
Images, etc.)README.md and <org-id>_hello-world_hello-camera.md
meta.json
cmd/module/main.go
cmd/cli/main.go
go run ./cmd/cliMakefile
go.mod
.github/workflows/build.yml
💡 Getting started tip: Focus on the main model file (hello_camera.py or module.go). Everything else can wait until you need it.
✅ API selected:
✅ Module generated:
✅ Ready to code:
Now that you have your module structure, continue to Part 3: Implement your module to write the code that makes your hardware work.
Tutorial navigation:
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!