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 Golang 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.
NoteThis 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, set up viam-server, built your circuit, and connected your robot to the Viam app 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:
- 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
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 Golang SDK on your local computer. Use which ever programming language you are most comfortable with.
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 Golang 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:
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.
TipIf 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 Viam Community Slack. 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.
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:
Now that we have our board, and LED initialized, let’s create an infinite loop that will blink the LED on and off. Directly after the code you pasted above, paste the following snippet:
You can run your code again by typing the following into the terminal:
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
TipIf 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 Viam Community Slack. There, you will find a friendly developer community of people learning how to make robots using Viam.
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.
If you want to connect with other developers learning how to build robots, or if you have any issues whatsoever getting Viam set up, let us know on the Viam Community Slack3, and we will be happy to help you get up and running.