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.

For reference, the circuit you are building for this tutorial looks like this:

Circuit diagram showing a Raspberry Pi with a red connector running out of GPIO pin 8 to a 100-ohm* resistor*. The resistor is connected to the long lead of a red LED bulb. Finally, a blue connector connects the short lead of the LED to the ground connection on pin 6 of the Raspberry Pi GPIO pins.

What you’ll need for this guide

You will need the following hardware, tools, and software to complete this project:

Hardware

  1. Raspberry Pi 3 or 4
    1. Refer to the Viam Raspberry Pi Setup Guide to setup your Pi.
    2. You must also enable SSH on your Pi.
  2. Solderless breadboard
  3. Jumper wires for easy hookup
  4. Resistor pack You will be using a 100 Ohm resistor, which is the resistor with brown-black-brown bands
  5. 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.

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 blink.py or blink.go file in your code editor, and save your file.

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.

A screenshot from the Visual Studio Code command line that prints the output of print(robot.resource_names) when your Raspberry Pi has correctly connected and initialized with the Viam app. The output is an array of resources that have been pulled from the Viam app. Some of these are the Vision Service, Data Manager, and Board.

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. 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:

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

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.

Components URL List



Have questions, or want to meet other people working on robots? Join our Community Discord.