NVIDIA Jetson TX1, TX2, TX2 NX, AGX Xavier and Nano development board GPIO port configuration and application

NVIDIA Jetson TX1, TX2, TX2 NX, AGX Xavier and Nano development board GPIO port configuration and application

Introduction

Jetson TX1, TX2, TX2 NX, AGX Xavier, and Nano development boards are equipped with a GPIO header containing 40 pins that can be used for digital input and output. As shown below

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-gIr1ItyM-1687860142526)(89dcbf0ee89a41318a67f1a0bf5d1bf4.png)]

​Similar to the 40-pin headers on the Raspberry Pi, these GPIO interfaces can be used to connect various external devices such as sensors, actuators, etc. In order to facilitate the control of these GPIO interfaces, NVIDIA provides the Jetson GPIO Library package, which contains a Python library that can be used to control digital input and output. This library has the same API as the RPi.GPIO library on the Raspberry Pi, so users can easily port applications that previously ran on the Raspberry Pi to the Jetson board. By using this library, users can easily write Python code to read and control the GPIO interface, and can customize the configuration as needed. This makes the Jetson development board a powerful tool that can be used in a variety of embedded applications such as robotics, smart home systems, automation control systems, and more. Add content to these to make them flesh out.

This document will describe what is included in the Jetson GPIO library package, how to configure the system, and run the provided sample applications and library API.

Here we briefly introduce how to use this library about Jetson.GPIO, here is the official detailed description:

https://pypi.org/project/Jetson.GPIO/ or https://github.com/NVIDIA/jetson-gpio

GPIO and BCM comparison table

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-YFAsMlsb-1687860142532)(image-20230627162919341.png)]

Environment configuration

1. Download jetson-gpio:

Execute the command git clone https://github.com/NVIDIA/jetson-gpio

git clone https://github.com/NVIDIA/jetson-gpio

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-3msmsdev-1687860142534)(image-20230627163122434.png)]

2. Move the downloaded file to the directory: If your library exists in this directory in /opt/nvidia, we need to back up the original directory with the following command:

cd /opt/nvidia
sudo mv ~/jetson-gpio jetson-gpio_bak

3. Enter the jetson-gpio library folder and install the library.

cd /opt/nvidia/jetson-gpio
sudo python3 setup.py install

4. Create a gpio group, add your current login user to this group, and grant permission to use it

sudo groupadd -f -r gpio
sudo usermod -a -G gpio user_name 
sudo cp /opt/nvidia/jetson-gpio/lib/python/Jetson/GPIO/99-gpio.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules && sudo udevadm trigger

Note: user_name is the username you use, say nano

5. Configure permissions for all users to use the GPIO library in python

sudo chmod a+rw /dev/gpiochip0
sudo chmod a+rw /dev/gpiochip1
sudo chmod a+rw /dev/gpiochip2

In this case, the software environment is configured

example

After the environment is configured, you can test the routine. Several simple routines are provided on jetson-gpio, we can simply test it, first enter the sample program directory cd ~/opt/nvidia/jetson-gpio/samples/

1、simple_out.py

the code

#!/usr/bin/env python

# Copyright (c) 2019-2022, NVIDIA CORPORATION. All rights reserved.
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import RPi.GPIO as GPIO
import time

# Pin Definitions
output_pin = 18  # BCM pin 18, BOARD pin 12

def main():
    # Pin Setup:
    GPIO.setmode(GPIO.BCM)  # BCM pin-numbering scheme from Raspberry Pi
    # set pin as an output pin with optional initial state of HIGH
    GPIO.setup(output_pin, GPIO.OUT, initial=GPIO.HIGH)

    print("Starting demo now! Press CTRL+C to exit")
    curr_value = GPIO.HIGH
    try:
        while True:
            time.sleep(1)
            # Toggle the output every second
            print("Outputting {} to pin {}".format(curr_value, output_pin))
            GPIO.output(output_pin, curr_value)
            curr_value ^= GPIO.HIGH
    finally:
        GPIO.cleanup()

if __name__ == '__main__':
    main()

This is a simple input program that uses BCM's pin coding mode, which can read the value of PIN12 and print it to the terminal.

Wiring: Connect the positive wire, signal wire, and negative wire of the power supply to pins 1, 12, and 14 respectively, as shown in the figure below:

insert image description here
Then run the program: python3 simple_out.py

 python3 simple_out.py

Expected effect: After running the program, you can see the terminal print information, and the program will output high level and low level (updated alternately every 2 seconds) to the physical pin PIN12

insert image description here

【Note】The 18 here refers to the BCM code, and the PIN12 above refers to the physical code, which is the silk screen code printed on the board.
The working level of Jetson TX2 NX pin is 3.3V, so don't connect it to 5V level when using it.
If you need to connect the pins, please pay attention to the protection circuit to avoid short circuit and burn out the components

2. Control the lower computer through the control relay to realize the palace control of 4 GPIO ports

control.py code:

#!/usr/bin/env python


import RPi.GPIO as GPIO
import time

# Pin Definitions
output_pin1 = 18  # BCM pin 18, BOARD pin 12
output_pin2 = 23  # BCM pin 18, BOARD pin 16
output_pin3 = 24  # BCM pin 18, BOARD pin 18
output_pin4 = 25  # BCM pin 18, BOARD pin 20


def setup_gpio():
    GPIO.setmode(GPIO.BCM)  # BCM pin-numbering scheme from Raspberry Pi
    # set pins as output pins with optional initial state of LOW
    GPIO.setup(output_pin1, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(output_pin2, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(output_pin3, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(output_pin4, GPIO.OUT, initial=GPIO.LOW)


def control_status(flag=0):
    if flag == 0:
        print('0')
    elif flag == 1:
        GPIO.output(output_pin1, GPIO.HIGH)
        time.sleep(2)
        setup_gpio()
        print('1')
    elif flag == 2:
        GPIO.output(output_pin1, GPIO.HIGH)
        GPIO.output(output_pin2, GPIO.HIGH)
        time.sleep(2)
        setup_gpio()
        print('2')
    elif flag == 3:
        GPIO.output(output_pin1, GPIO.HIGH)
        GPIO.output(output_pin2, GPIO.HIGH)
        GPIO.output(output_pin3, GPIO.HIGH)
        time.sleep(1)
        print('3')
    elif flag == 4:
        setup_gpio()
        GPIO.output(output_pin1, GPIO.HIGH)
        GPIO.output(output_pin2, GPIO.HIGH)
        GPIO.output(output_pin3, GPIO.HIGH)
        GPIO.output(output_pin4, GPIO.HIGH)
        time.sleep(1)
        print('4')


while True:
    setup_gpio()
    num = input("input your num")
    control_status(int(num))

This code is used to control the GPIO pin on Raspberry Pi to output high and low levels to control the status of external devices. The specific functions are as follows:

  1. Introduce RPi.GPIO and time modules.
  2. The BCM numbers, BOARD numbers and corresponding pin states of the 4 GPIO pins are defined, and the pins are initialized in the setup_gpio() function.
  3. A function named control_status() is defined, which is used to control the output status of different GPIO pins according to the incoming parameters (0~4), so as to achieve the purpose of controlling external devices.
  4. In the main program, the user input num is continuously received through a loop, and the control_status() function is called to control the external device.

Summarize

Controlling the relay through the Jetson TX2 NX's GPIO interface is very simple. Just connect the relay module to the correct GPIO pins, install the Jetson GPIO library, write a Python script and run it. This technique can help realize various lower computer control applications.

Detailed GPIO and BCM comparison table

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-BCUpsiH6-1687860142538)(image-20230627180051567.png)]

Guess you like

Origin blog.csdn.net/qq_42076902/article/details/131422739