📌 25 de Setembro, 2021

NanoPi M4: GPIO Tutorial

ARM / Single Board Computer · Informática · Linux

📌 25 de Setembro, 2021

NanoPi M4: GPIO Tutorial

ARM / Single Board Computer · Informática · Linux

In this guide you’ll find out how to read and write values to your NanoPi M4 or M4V2 digital pins also known as GPIO. This low level interface allows you to control digital electronic circuits directly from Linux.

Under Linux General-Purpose Input/Output (GPIO) pins are available as folder at /sys/class/gpio. This generic interface is an abstraction that allows you to control such pins in an easy and convenient way directly from bash.

Before we start we need to know the GPIO mapping for the NanoPi M4. The following table shows what’s the virtual GPIO number for each physical pin the board:

 +------+----------+------+ Model  NanoPi-M4 +------+----------+------+
 | GPIO |   Name   | Mode | V | Physical | V | Mode |   Name   | GPIO |
 +------+----------+------+---+----++----+---+------+----------+------+
 |      |     3.3V |      |   |  1 || 2  |   |      | 5V       |      |
 |      | I2C2_SDA |      |   |  3 || 4  |   |      | 5V       |      |
 |      | I2C2_SCL |      |   |  5 || 6  |   |      | GND(0V)  |      |
 |   32 | GPIO1_A0 |  OUT | 0 |  7 || 8  |   | ALT  | GPIO4_C1 |  145 |
 |      |  GND(0V) |      |   |  9 || 10 |   | ALT  | GPIO4_C0 |  144 |
 |   33 | GPIO1_A1 |   IN | 0 | 11 || 12 | 1 | IN   | GPIO1_C2 |  50  |
 |   35 | GPIO1_A3 |   IN | 0 | 13 || 14 |   |      | GND(0V)  |      |
 |   36 | GPIO1_A4 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO1_C6 |  54  |
 |      |     3.3V |      |   | 17 || 18 | 0 | IN   | GPIO1_C7 |  55  |
 |      | UART4_TX |      |   | 19 || 20 |   |      | GND(0V)  |      |
 |      | UART4_RX |      |   | 21 || 22 | 0 | IN   | GPIO1_D0 |  56  |
 |      | SPI1_CLK |      |   | 23 || 24 |   |      | SPI1_CSn |      |
 |      |  GND(0V) |      |   | 25 || 26 |   | ALT  | GPIO4_C5 |  149 |
 |      | I2C2_SDA |      |   | 27 || 28 |   |      | I2C2_SCL |      |
 |      | I2S0_LRX |      |   | 29 || 30 |   |      | GND(0V)  |      |
 |      | I2S0_LTX |      |   | 31 || 32 |   |      | I2S_CLK  |      |
 |      | I2S0_SCL |      |   | 33 || 34 |   |      | GND(0V)  |      |
 |      | I2S0SDI0 |      |   | 35 || 36 |   |      | I2S0SDO0 |      |
 |      | I2S0I1O3 |      |   | 37 || 38 |   |      | I2S0I2O2 |      |
 |      |  GND(0V) |      |   | 39 || 40 |   |      | I2S0I3O1 |      |
 +------+----------+------+---+----++----+---+------+----------+------+

GPIO Output: Write Values

The physical pin 15 or GPIO1_A4, also known to our kernel as GPIO 36 and in order to use it need to export it and then set it as an output GPIO:

echo 36 > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio36/direction

Now we can write 1 in order to have the physical pin 15 set to HIGH or 0 for LOW:

echo "1" > /sys/class/gpio/gpio36/value

Now the pin should be 3v when measured with a voltmeter.

GPIO Input: Read Values

In order to detect when a push button is pressed we need to read the value of a GPIO. Let’s consider the following switch connected to GPIO1_C7:

Our GPIO pin is connected to the 3v3 pin whenever the button is pressed causing a status change from LOW to HIGH. The 1k resistor is optional but recommend – it ensures that even if you mistakenly change the pin direction your NanoPi M4 won’t get fried. You may also use a higher value resistor such as 5k or 10k but bear in mind that the higher the value the slower the GPIO.

Note: In my experience the NanoPi M4 seems to conveniently use an internal pull down resistor on GPIOs. This means we don’t need to add an extra resistor in order to have stable readings. If you get unstable readings you’ll need a circuit with an external pull down:

Now let’s set up the pin:

echo 55 > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio55/direction

At this point if we try to read it:

$ cat /sys/class/gpio/gpio55/value
0

# Now with the button pressed:

$ cat /sys/class/gpio/gpio55/value
1

That’s it! Now you know how to use your NanoPi M4’s digital pins. Enjoy!