How to Blink an LED Using NVIDIA Jetson Nano

In this tutorial, we will blink an LED using NVIDIA Jetson Nano. I will show you how to use Python to blink an LED, and I will show you how to use C++ to blink an LED. Here is what you will build:

blink_led_jetson_nano

Prerequisites

You have already set up your NVIDIA Jetson Nano (4GB, B01).

jetson-nano-aerial

You Will Need

This section is the complete list of components you will need for this project (#ad).

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Set Up the Hardware

The first thing we need to do is to set up the hardware. 

Gather all of the items from the “You Will Need” section and lay them out on a table.

The NVIDIA Jetson Nano board has 40  general-purpose input/output pins (GPIO). Each pin can either be an input (e.g. connecting the Jetson Nano to a sensor) or an output (e.g. an LED). GPIO pins on the Jetson Nano use 3.3V by default.

1-pin-diagram-nvidia-jetson-nano

In order to get the LED to blink, we need to connect it to one of the 40 GPIO pins. Make sure you set everything up exactly like this image below:

  • Connect the 220 Ohm resistor to the positive pin of the LED (i.e. the long leg).
  • Connect the other end of this resistor to GPIO pin 7 on the Jetson Nano. 
  • Connect the negative pin of the LED (i.e. the short leg) to the GND pin on the Jetson Nano.
2021-04-03-16.15.26
2021-04-03-16.15.47
2021-04-03-16.16.34

The connections are made using Male-Female jumper wires.

Blink an LED Using Python

Set Up the NVIDIA GPIO Library for Python

Let’s set up NVIDIA Jetson Nano GPIO.

Open a terminal, and type the following command.

sudo pip3 install Jetson.GPIO

If that command above doesn’t work, type the following command:

sudo pip install Jetson.GPIO

When I typed these commands, I got a message which a bunch of warning signs, but at the end of all that the following text was printed to the console:

“Requirement already satisfied…”

This message means that the GPIO library for Python is already installed on my Jetson Nano.

Reconfigure security permissions. Instead of your_user_name, I used automaticaddison, which is my username.

sudo groupadd -f -r gpio
sudo usermod -a -G gpio your_user_name

Reboot the computer.

sudo reboot

Let’s check to see if the library is fully setup.

python3
>>> import Jetson.GPIO as GPIO
>>> GPIO.JETSON_INFO
>>> GPIO.VERSION

Here is what I see:

2021-04-03-16.09.13

Write the Code

Create a new folder. Open up a new terminal, and type:

cd Documents
mkdir led_blink
cd led_blink

Let’s create a program to blink the LED. 

Create a file named gpiodemo.py.

gedit gpiodemo.py
# GPIO library
import Jetson.GPIO as GPIO

# Handles time
import time 

# Pin Definition
led_pin = 7

# Set up the GPIO channel
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(led_pin, GPIO.OUT, initial=GPIO.HIGH) 

print("Press CTRL+C when you want the LED to stop blinking") 

# Blink the LED
while True: 
  time.sleep(2) 
  GPIO.output(led_pin, GPIO.HIGH) 
  print("LED is ON")
  time.sleep(2) 
  GPIO.output(led_pin, GPIO.LOW)
  print("LED is OFF")

Run the Code

You can run your program, gpiodemo.py, in the Terminal. Type this command:

python3 gpiodemo.py

If you get any errors due to security issues, you can run the gpiodemo.py file with the sudo command. 

sudo python3 gpiodemo.py

You should see the LED blinking.

2021-04-03-16.15.27
2021-04-03-16.17.03

You should also see output in the terminal.

Press CTRL+C when you’re ready to stop the program.

To shutdown your Jetson Nano, you can type:

sudo shutdown -h now

Blink an LED Using C++

Set Up the NVIDIA GPIO Library for C++

Let’s install the C++ Jetson GPIO library, so we can see how to blink an LED using C++.

Make sure you’re connected to the Internet, and open a new terminal window.

git clone https://github.com/pjueon/JetsonGPIO
cd JetsonGPIO/build
make all
sudo make install

Reboot your machine.

sudo reboot

See if the header file for the C++ GPIO Library for Jetson Nano is on your machine. The command below will show the path to the header file.

find /home -name JetsonGPIO.h

Write the Code

Go to the led_blink directory.

cd Documents/led_blink

Open a new C++ program.

gedit led_blink.cpp

Write the following code. We want to blink an LED every 0.5 seconds:

// Handles input and output
#include <iostream>

// For delay function
#include <chrono> 

// Handles threads of program execution
#include <thread>

// Signal handling
#include <signal.h>

#include <JetsonGPIO.h>

// Pin Definitions
int led_pin = 7;

// Flag to determine when user wants to end program
bool done = false;

// Function called by Interrupt
void signalHandler (int s){
  done = true;
}

int main() {

  // When CTRL+C pressed, signalHandler will be called
  // to interrupt the programs execution
  signal(SIGINT, signalHandler);

  // Pin Definitions 
  GPIO::setmode(GPIO::BOARD);

  // LED pin gets set to high initially
  GPIO::setup(led_pin, GPIO::OUT, GPIO::HIGH);

  std::cout << "Press CTRL+C to stop the LED" << std::endl;

  int curr_value = GPIO::HIGH;

  // Blink LED every 0.5 seconds
  while(!done) {

    std::this_thread::sleep_for(std::chrono::milliseconds(500));

    curr_value = GPIO::HIGH;

    GPIO::output(led_pin, curr_value);

    std::cout << "LED is ON" << std::endl;

    std::this_thread::sleep_for(std::chrono::milliseconds(500));

    curr_value = GPIO::LOW;

    GPIO::output(led_pin, curr_value);

    std::cout << "LED is OFF" << std::endl;
  }

  GPIO::cleanup();

  return 0;
}

Save the file, and close it.

See if your file is in there.

ls

Run the Code

Compile the program.

g++ -o led_blink led_blink.cpp -lJetsonGPIO 

(That is a lowercase L in front of Jetson above)

See if the executable is in the current directory.

ls

You should see a new file named led_blink.

If you see an error, check your program to see if it is exactly like I wrote it.

Run the program.

./led_blink

You should see the LED blinking every 0.5 seconds (i.e. 500 milliseconds).

2021-04-03-16.15.23
2021-04-03-16.48.56

Press CTRL + C when you’re finished.

That’s it. Keep building!

How to Write a C/C++ Program for NVIDIA Jetson Nano

In this tutorial, we will write a basic C++ program for NVIDIA Jetson Nano.

Prerequisites

Check Your GCC Version

Open a terminal window, and run the following command to see the version of the GCC C/C++ compiler you have.

gcc --version

If gcc is not installed, follow the instructions in the next section.

Install the GCC/G++ Compilers 

Open a new terminal window, and type:

sudo apt-get update

Now use this command to install a bunch of packages, including GCC, G++, and GNU Make:

sudo apt install build-essential

You might see some sort of error about things being locked if you try the following command. If you do, kill all processes that are using the APT package management tool using this command:

sudo killall apt apt-get

Remove the lock files:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock*

Reconfigure the packages and update:

sudo dpkg --configure -a
sudo apt update

Now use this command to install numerous packages, including GCC, G++, and GNU Make:

sudo apt install build-essential

Press Y to continue.

Wait while the whole thing downloads.

Now, install the manual pages about using GNU/Linux for development (note: it might already be installed):

sudo apt-get install manpages-dev

Check to see if both GCC and G++ are installed.

whereis gcc
whereis g++

Check what version you have.

gcc --version
g++ --version

Install the C/C++ Debugger

In this section, we will install the C/C++ debugger. It is called GNU Debugger (GDB) and enables us to detect problems or bugs in the code that we write.

In the terminal window, type the following command:

sudo apt-get install gdb

You will be asked to type your password, and then click Enter.

Type the following command to verify that it is installed:

gdb

Type this command to quit.

quit

Press Enter.

Exit the terminal.

exit

Install Gedit

Install gedit, a text editor that will enable us to write code in C/C++.

Open a terminal window, and type:

sudo apt-get install gedit

Write a Hello World Program in C

Let’s write a program that does nothing but print “Hello Automatic Addison” (i.e. my version of a “Hello World” program) to the screen.

Open a new terminal window, and create a new folder.

mkdir cpp_basics

Move to that folder.

cd cpp_basics

Open a new C program.

gedit hello_automaticaddison_c.c
#include<stdio.h>
int main()
{
printf("Hello Automatic Addison\n");
return 0;
}

Save the file, and close it.

See if your file is in there.

ls

Compile the program.

gcc hello_automaticaddison_c.c -o hello_automaticaddison_c

Run the program.

./hello_automaticaddison_c

Here is what I see.

2021-04-02-160235

Write a Hello World Program in C++

Open a new C++ program.

gedit hello_automaticaddison_cpp.cpp

Write the following code.

// Simple C++ program to display "Hello Automatic Addison"
  
// Header file for input output functions
#include<iostream> 
  
using namespace std;
  
// main function
// where the execution of program begins
int main()
{
    // prints hello world
    cout<<"Hello Automatic Addison";
      
    return 0;
}

Save the file, and close it.

See if your file is in there.

ls

Compile the program. All of this is just a single command.

g++ -o hello_automaticaddison_cpp hello_automaticaddison_cpp.cpp

If you see an error, check your program to see if it is exactly like I wrote it.

Run the program.

./hello_automaticaddison_cpp

Here is what I see.

2021-04-02-160742

That’s it. Keep building!

How to Write a Python Program for NVIDIA Jetson Nano

In this tutorial, we will write a basic Python program for NVIDIA Jetson Nano.

Prerequisites

Install Python

To install Python, open a new terminal window and type:

sudo apt-get install python python3

To find out where the Python interpreter is located, type this command.

which python

You should see:

/usr/bin/python

Install Gedit

Install gedit, a text editor that will enable us to write code in Python.

sudo apt-get install gedit

Install Pip

Let’s begin by installing pip. Pip is a tool that will help us manage software packages for Python.

Software packages are bundles of code written by someone else that are designed to solve a specific problem. Why write code to solve a specific problem from scratch, when someone else has already written code to solve that exact same problem? That is where software packages come into play. They prevent you from having to reinvent the wheel.

Open up a fresh Linux terminal window.

Type the following command to update the list of available packages that can be installed on your system.

sudo apt-get update

Type your password.

Upgrade all the packages. The -y flag in the following command is used to confirm to our computer that we want to upgrade all the packages.

sudo apt-get -y upgrade

Type the following command to check the version of Python you have installed.

python3 --version

My version is 3.6.9. Your version might be different. That’s fine.

Now, let’s install pip.

sudo apt-get install -y python3-pip

If at any point in the future you want to install a Python-related package using pip, you can use the following command:

pip3 install package_name

Create a Virtual Environment

In this section, we will set up a virtual environment. You can think of a virtual environment as an independent workspace with its own set of libraries, settings, packages, and programming language versions installed. 

For example, you might have a project that needs to run using an older version of Python, like Python 2.7. You might have another project that requires Python 3.8. Setting up separate virtual environments for each project will make sure that the projects stay isolated from one another.

Let’s install the virtual environment package. 

sudo apt-get install -y python3-venv

With the software installed, we can now create the virtual environment using the following command. The dot(.) in front of py3venv makes the directory a hidden directory (the dot is optional):

python3 -m venv .py3venv

Type the following command to get a list of all the directories. You should see the .py3venv folder there.

ls -a

List all the contents inside the .py3venv folder.

ls .py3venv/

Now that the virtual environment has been created, we can activate it using the following command:

source ~/.py3venv/bin/activate

Look what happened. There is a prefix on the current line that has the name of the virtual environment we created. This prefix means that the .py3venv virtual environment is currently active.

2021-04-02-155424

When a virtual environment is active that means that when we create software programs here in Python, these programs will use the settings and packages of just this virtual environment. 

Keep your terminal window open. We’re not ready to close it just yet. Move on to the next section so that we can write our first program in Python.

Write a “Hello World” Program

Let’s write a program that does nothing but print “Hello Automatic Addison” (i.e. my version of a “Hello World” program) to the screen.

Create a new folder.

mkdir py_basics

Move to that folder.

cd py_basics

Open a new Python program.

gedit hello_automaticaddison.py

Type the following code in there:

#!/usr/bin/env python 
print("Hello Automatic Addison!")

Save the file, and close it.

See if your file is in there.

ls

Run the program.

python hello_automaticaddison.py
2021-04-02-155716

Deactivate the virtual environment.

deactivate

That’s it. Keep building!