How to Make an LED Blink with a Raspberry Pi and the Viam SDK
In this post, you will be introduced to the basics of programming hardware by using either the Viam Python SDK or the Viam Go SDK to make an LED blink. This will allow you to write code to make an LED connected to the GPIO of a Raspberry Pi blink on and off. This tutorial is a good introduction to Python or Go programming languages, and developing custom software for robots.
Note
This is part 2 of Viam’s Intro to Robotics series.
If you haven’t completed Part 1, be sure to go back and complete that before starting on this tutorial.
You should have already set up your Raspberry Pi, connected to the Viam app and set up viam-server
, and built your circuit before proceeding.
For reference, the circuit you are building for this tutorial looks like this:

What you’ll need for this guide
You will need the following hardware, tools, and software to complete this project:
Hardware
- Raspberry Pi 3 or 4
- Refer to the Viam Raspberry Pi Setup Guide to setup your Pi.
- You must also enable SSH on your Pi.
- Solderless breadboard
- Jumper wires for easy hookup
- Resistor pack You will be using a 100 Ohm resistor, which is the resistor with brown-black-brown bands
- LED
Click to view the Component URL Listing
Software
How to install a Viam SDK
In this step, you are going to install either the Viam Python SDK (Software Development Kit) or the Viam Go SDK on your local computer. Use which ever programming language you are most comfortable with.
Note
Refer to the appropriate SDK documentation for SDK installation instructions.
How to connect your robot to the Viam SDK
The easiest way to get started writing an application with Viam is to navigate to the robot page on the Viam App, select the Code Sample tab, and copy the boilerplate code from the section labeled Python SDK or Go SDK.
These code snippets import all the necessary libraries and set up a connection with the Viam app in the cloud.
Next, paste that boilerplate code from the Code Sample tab of the Viam app into a file named
You can now run the code.
Doing so will ensure that the Viam SDK is properly installed and that the viam-server
instance on your robot is live.
You can run your code by typing the following into the terminal:
python3 blink.py
go run blink.go
If you successfully configured your robot and it is able to connect to the Viam app, you should see something like this printed to the terminal after running your program. What you see here is a list of the various resources, components, and services that have been configured to your robot in the Viam app.

Tip
You can also ask questions in the Community Discord and we will be happy to help.
There, you will find a friendly developer community of people learning how to make robots using Viam.
How to make an LED Blink with the Viam SDKs
The first thing you need to do is import the Board component from the Viam SDK. This component represents a physical general-purpose board that contains GPIO pins. We will need this component in order to interact with the GPIO pins on our Raspberry Pi.
At the top of your
from viam.components.board import Board
In import
block at the top of the file, add the following to the import statements without removing any of the other imports:
import (
"fmt"
"time"
"go.viam.com/rdk/components/board"
)
Next, you will need to initialize the Raspberry Pi board, and you will need to tell Viam which GPIO pin your LED is on.
At the bottom of the main()
function, paste the following:
# Initialize the board and the LED on pin 8
local = Board.from_robot(robot, 'local')
led = await local.gpio_pin_by_name('8')
// Initialize the board
myBoard, err := board.FromRobot(robot, "myBoard")
if err != nil {
logger.Fatalf("could not get board: %v", err)
}
// Initialize the board's GPIO pin and name it "led"
led, err := myBoard.GPIOPinByName("8")
if err != nil {
logger.Fatalf("could not get led: %v", err)
}
Now that we have our board, and LED initialized, let’s create an infinite loop that will blink the LED on and off.
Within the main
function, you can add the code to create an infinite loop, you can remove the line to close out the connection to your robot, since the infinite loop will never hit that line.
Your completed main
function should look like this:
async def main():
robot = await connect()
print('Resources:')
print(robot.resource_names)
# Initialize the board and the LED on pin 8
local = Board.from_robot(robot, 'local')
led = await local.gpio_pin_by_name('8')
# Create an infinite loop that will blink the LED on and off
while (True):
# When True, sets the LED pin to high/on.
await led.set(True)
print('LED is on')
await asyncio.sleep(1)
# When False, sets the pin to low/off.
await led.set(False)
print('LED is off')
await asyncio.sleep(1)
func main() {
logger := golog.NewDevelopmentLogger("client")
robot, err := client.New(
context.Background(),
"[ADD YOUR ROBOT ADDRESS HERE. YOU CAN FIND THIS ON THE CONNECT TAB OF THE VIAM APP]",
logger,
client.WithDialOptions(rpc.WithCredentials(rpc.Credentials{
Type: utils.CredentialsTypeRobotLocationSecret,
Payload: "[PLEASE ADD YOUR SECRET HERE. YOU CAN FIND THIS ON THE CONNECT TAB OF THE VIAM APP]",
})),
)
if err != nil {
logger.Fatal(err)
}
defer robot.Close(context.Background())
logger.Info("Resources:")
logger.Info(robot.ResourceNames())
// Initialize the board
myBoard, err := board.FromRobot(robot, "myBoard")
if err != nil {
logger.Fatalf("could not get board: %v", err)
}
// Initialize the board's GPIO pin and name it "led"
led, err := myBoard.GPIOPinByName("8")
if err != nil {
logger.Fatalf("could not get led: %v", err)
}
// Infinite loop that will blink the LED on and off.
for {
// When True, sets the LED pin to high/on.
err = led.Set(context.Background(), true, nil)
if err != nil {
logger.Fatalf("could not set led to on: %v", err)
}
fmt.Println("LED is on")
time.Sleep(1 * time.Second)
// When False, sets the pin to low/off.
err = led.Set(context.Background(), false, nil)
if err != nil {
logger.Fatalf("could not set led to off: %v", err)
}
fmt.Println("LED is off")
time.Sleep(1 * time.Second)
}
}
You can run your code again by typing the following into the terminal:
python3 blink.py
go run blink.go
And, if all goes well, you should see your LED blinking on and off again every second!
You can exit this program by pressing CTRL + C in your terminal window.
If you get an error, you can check your code against my complete code here:
Completed code: https://github.com/viam-labs/LED-Blink
If you have any issues whatsoever getting the Viam SDK set up or getting your code to run on your computer, the best way to get help is over on the Community Discord.
Summary
In this tutorial, you learned the basics of controlling your robot using the Viam SDK by writing a short program in either Go or Python that makes an LED on your Raspberry Pi blink on and off.
If you are looking for some projects that would be a great next step in your journey of learning about how to build robots, check out one of following Tutorial List.
Tip
If you have any issues whatsoever getting the Viam SDK set up or getting your code to run on your Raspberry Pi, the best way to get help is over on the Community Discord. There, you will find a friendly developer community of people learning how to make robots using Viam.
Components URL List
- Raspberry Pi 3 or 4: https://a.co/d/5Tn67G3
- Solderless breadboard: https://amzn.to/2Q4Z5Ta
- Jumper wires for easy hookup: http://amzn.to/2qVhd4y
- Resistor pack: http://amzn.to/2Dmainw
- Red LED: http://amzn.to/2Ex2v5q
Have questions, or want to meet other people working on robots? Join our Community Discord.
Was this page helpful?
Glad to hear it! If there is anything we could be doing better, please create an issue.
We're sorry about that. If you'd like to talk to us for help, please join the Community Discord. To ensure we know what's wrong with this page, you can also open an issue.