Control an LED with a Raspberry Pi Using the Viam App

In this post, we will show you how to use Viam to make an LED blink with a Raspberry Pi. This tutorial is a great place to start if you have never built a robot or a circuit before.

What you’ll need for this guide

You will need the following tools to complete the project:

Hardware

  1. Raspberry Pi 3 or 4
    1. Refer to the Viam Raspberry Pi Setup Guide to setup 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 hardware URL listing

Software

Project setup

Before you proceed with building your circuit, you are going to need to set up the operating system on your Raspberry Pi and install viam-server on the Pi. We recommend that you follow along with the Installing viam-server on Raspberry Pi guide in the Viam documentation. Be sure to follow all the steps including adding your Pi on the Viam app..

You can also ask questions in the Community Discord and we will be happy to help.

Building the circuit

The first step in this project is to design a simple LED circuit. Then you will make the LED circuit controllable from the Raspberry Pi by connecting the circuit to the general purpose input/output (GPIO) pins on the Raspberry Pi.

A simple LED circuit consists of a LED and resistor. The resistor is used to limit the current that is being drawn and is called a current-limiting resistor.

GPIO Pinout

General-purpose input/output (GPIO) is a digital signal pin on a circuit board, like a Raspberry Pi, which may be used as an input or output, or both, and is controllable by software.

Photo showing a Raspberry Pi 4 with a white box around the GPIO pins on the Pi and big red letters that say, 'GPIO Pins.'

As you may have guessed, each pin has a specific role, and you can use it only for that role. Some of them are input/output, power (3.3V or 5V), or ground. As you can see in the diagram below, there are 40 output pins on the Pi. You can program 26 of the GPIO pins.

Diagram showing all of the GPIO pins on a Raspberry Pi 4 and their corresponding pin number and function.

One thing to note that might be confusing with the pin numbering on Raspberry Pis: There are 40 physical pins numbered from 1 to 40. That is board pin numbering, corresponding to the pin’s physical location on the board. When working with the GPIO pins with Viam, you will use the board numbers. For example, Pin 1 can be located pretty easily, since Pin 1 is always the pin whose corner is rounded.

Then there’s numbering them by function or GPIO connection. These are the big numbers, like “GPIO 22”. These numbers are helpful for understanding the function of each pin.

When in doubt, the website pinout.xyz is useful for identifying pins. It gives you the exact layout and role of each pin.

Circuit explanation

Here’s the circuit diagram used in this tutorial for making the LED blink.

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.

You can now hook the LED and resistor up to GPIO 14 (or pin 8) on your Raspberry Pi. The resistor and LED need to be in series as in the diagram above. To find the right resistor use the resistor color code – for a 100 ohm resistor, it needs to be brown-black-brown. You can use your multimeter to double-check the resistor value or check yours using the photo below.

Photo of a 100-ohm resistor with text overlaid that says, in order, brown-black-brown-gold.

When hooking up the circuit, note the polarity of the LED. You will notice that the LED has long and short leads. The long lead is the positive side, which is known as the anode, the short lead is the negative side, which is known as the cathode. The long anode should be connected to the resistor and the short cathode should be connected to the ground using the blue jumper wire and pin 6 on the Raspberry Pi as shown on the diagram.

Configuring your bot using the Viam app

Before proceeding, be sure that you have connected your Pi to the Viam app. Steps on how to do this can be found in the Adding your Pi on the Viam App section of the Viam Documentation.

Now that we have gotten that out of the way, let’s get back to configuring your robot’s components. First, go to the Viam app at app.viam.com on your web browser, and select the robot’s Config tab.

Screenshot of the Viam app showing the Create Component field on the COMPONENTS subtab of the CONFIG tab.

You will add a board component to represent your single board computer, which in this case is the Raspberry Pi. You’ll create the new component in the Create Component panel. Name the board whatever you like as long as you are consistent when referring to it later; we’ll name it “local” since it is the board we will communicate with directly. For the component Type, select board. For Model, select pi, then click Create Component. Your board component pane will look like this:

Screenshot of the Viam app showing the board configuration on the CONFIG tab. The board is named 'local' and the attributes are shown as empty braces.

Click Save Config at the bottom of the screen.

As you add your board component to your robot in the Viam app, it generates a panel for your board in the Control tab. Here, you can click on Get to get the current status of your pin. The first time you click Get Pin State, it should return “Pin: 8 is low.”

Screenshot of the Viam app showing the board configuration on the **Code Sample** tab. The 'Board Local' row is expanded, and under the 'Get' row, the pin is set to '8.' A red box is around the '**Get Pin State**' button and the output, which reads, 'Pin: 8 is low.'

You can now select Set to control the status of your pin to high. It will look like this:

Screenshot of the Viam app showing the board configuration on the 'Connect' tab. The 'Board Local' row is expanded, and under the 'Set' row, the pin is set to '8.' A red box is around the 'Set Pin State' field.

After setting your pin to high the LED should illuminate. You can play around with values low and high by setting them differently, and you will see your LED toggle on and off depending on whether you have the value set to low or high.

Summary

Congratulations! If you have followed along, you have just successfully used Viam to make an LED blink with a Raspberry Pi! You have learned how the GPIO on a Raspberry Pi works, and how to build circuits for LED bulbs.

You are ready for your next robotics project. We recommend that you check out the next part in this series, How to Make an LED Blink with a Raspberry Pi and the Viam SDK, where you will learn how to use the Viam SDK to control a Raspberry Pi robot with Go or Python.

If you are looking for some more projects that would be a great next step in your robotics journey, check out our other tutorials.

You can also ask questions in the Community Discord and we will be happy to help.

Hardware URL list



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