how to access the gpio pins in python? (nanopi)

Having issues with your DietPi installation, or, found a bug? Post it here.
Post Reply
milmike
Posts: 1
Joined: Sat Nov 14, 2020 5:35 pm

how to access the gpio pins in python? (nanopi)

Post by milmike »

Hi,

is there a way to access all the gpio pins programmatically using python?
My device: nanopi air. I can use the pins with the original OS (I think via RPi.GPIO or something).
I can use the gpio command in terminal, it shows me something like this:

Code: Select all

root@DietPi:~# gpio readall
+-----+-----+----------+------+---+-NanoPi-NEO-Air--+------+----------+-----+-----+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
|     |     |     3.3V |      |   |  1 || 2  |   |      | 5V       |     |     |
|  12 |   8 |  GPIOA12 | ALT5 | 0 |  3 || 4  |   |      | 5V       |     |     |
|  11 |   9 |  GPIOA11 | ALT5 | 0 |  5 || 6  |   |      | 0v       |     |     |
| 203 |   7 |  GPIOG11 |  OFF | 0 |  7 || 8  | 0 | ALT5 | GPIOG6   | 15  | 198 |
|     |     |       0v |      |   |  9 || 10 | 0 | ALT5 | GPIOG7   | 16  | 199 |
|   0 |   0 |   GPIOA0 | ALT5 | 0 | 11 || 12 | 0 | OUT  | GPIOA6   | 1   | 6   |
|   2 |   2 |   GPIOA2 |  OFF | 0 | 13 || 14 |   |      | 0v       |     |     |
|   3 |   3 |   GPIOA3 |  OFF | 0 | 15 || 16 | 0 | OFF  | GPIOG8   | 4   | 200 |
|     |     |     3.3v |      |   | 17 || 18 | 0 | OFF  | GPIOG9   | 5   | 201 |
|  64 |  12 |   GPIOC0 | ALT4 | 0 | 19 || 20 |   |      | 0v       |     |     |
|  65 |  13 |   GPIOC1 | ALT4 | 0 | 21 || 22 | 0 | ALT5 | GPIOA1   | 6   | 1   |
|  66 |  14 |   GPIOC2 | ALT4 | 0 | 23 || 24 | 1 | OUT  | GPIOC3   | 10  | 67  |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+-NanoPi-NEO-Air--+------+----------+-----+-----+
+-----+----NanoPi-NEO-Air Debug UART-+----+
| BCM | wPi |   Name   | Mode | V | Ph |
+-----+-----+----------+------+---+----+
|   4 |  17 |   GPIOA4 | ALT5 | 0 | 37 |
|   5 |  18 |   GPIOA5 | ALT5 | 0 | 38 |
+-----+-----+----------+------+---+----+
so I guess DietPi recognized the pins correctly?

Here is a simple test code (found on the nanopi developer)

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
PIN_NUM = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN_NUM,GPIO.OUT)
while True:
    GPIO.output(PIN_NUM,True)
    time.sleep(1)
    GPIO.output(PIN_NUM,False)
    time.sleep(1)
When I installed the RPi.GPIO (via pip) and tried to use that module, I get:

Code: Select all

root@DietPi:~# python3 gpio.py
Traceback (most recent call last):
File "gpio.py", line 2, in <module>
import RPi.GPIO as GPIO
File "/usr/local/lib/python3.5/dist-packages/RPi/GPIO/__init__.py", line 23, in <module>
from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!
I tried it also with python2.
Any idea how I could use these ports in python or some other language?
francolargo
Posts: 5
Joined: Thu Mar 26, 2020 3:36 pm

Re: how to access the gpio pins in python? (nanopi)

Post by francolargo »

RPi.GPIO is a Python library of functions that only work with the RPi configuration of ports. You import it into a Python program and it simplifies the handling of GPIO. It is not directly related to any bash functions (like gpio readall) that may be executed from the linux command line.

Read more about it here: https://sourceforge.net/projects/raspberry-gpio-python/
Post Reply