Example Code for Mango-Pi-GPIO Control

The article offers a detailed guide on how to control GPIO pins on a Mango-Pi board. It includes example code demonstrating GPIO mapping, direction setting, and output level control using sysfs and the Pinpong library, with practical examples like making an LED blink and controlling its state with a button.

Requirements

  • Mango-Pi board with firmware burned ×1
  • Button ×1
  • LED ×1

GPIO mapping relationship under sysfs file system

Mapping pin formula: 32×pin group serial number + pin sub-number = mapped pin number

Pin Group

Pin Classification Mapped Serail Number
Pin start with PA 0
Pin start with PB 1
Pin start with PC 2
Pin start with PD 3
Pin start with PE 4
Pin start with PF 5

And so on.

Example

  • Example 1: Suppose you want to operate the pin PE4

    The serial number of PE is 4

    The corresponding position of PE4 is the fourth pin under PE

    Plug it into the formula, you get: 32×4+4=132

    Which is, PE4 mapped to GPIO is pin 132

  • Example 2: Suppose you want to operate the pin PA6

    The serial number of PA is 0

    The corresponding position of PA4 is the sixth pin under PA

    Plug it into the formula, you get: 32×0+6=6

    Which is, PA6 mapped to GPIO is pin 6

  • Example 3: Suppose you want to operate the pin PF1

    The serial number of PF is 5

    The corresponding position of PF1 is the first pin under PF

    Plug it into the formula, you get: 32×5+1=161

    Which is, PF1 mapped to GPIO is pin 161

Start Mapping

  • Export the GPIO we need
# ls /sys/class/gpio
export  gpiochip0  unexport
# echo 132 > /sys/class/gpio/export
# ls /sys/class/gpio/
export     gpio132    gpiochip0  unexport
  • Set GPIO as output direction
echo out > /sys/class/gpio/gpio132/direction
  • Set the output level of GPIO
echo 1 > /sys/class/gpio/gpio132/value # Set GPIO output high level
echo 0 > /sys/class/gpio/gpio132/value # Set GPIO output low level
  • Set GPIO as input direction
echo in > /sys/class/gpio/gpio133/direction
  • Read GPIO level in the input mode
#cat /sys/class/gpio/gpio133/value
1
  • Delete GPIO configuration file
#echo 132 > /sys/class/gpio/unexport
#ls /sys/class/gpio
export  gpiochip0  unexport

Control GPIO via Pinpong

  • Blink(Output mode test)

Make the LED blink.

import time
from pinpong.board import Board, Pin

b = Board("F1C")
b.begin()
led = Pin(3, Pin.OUT)

while True:
    led.value(1)
    time.sleep(1)
    led.value(0)
    time.sleep(1)
  • Test Result

Connect an LED light to pin 131 (PE3)

52

The LED flashes after running the program.

  • Button(Input mode + output mode test)

Turn on/off the LED.

import time
from pinpong.board import Board, Pin

pyb = Board("PYB").begin()
btn = Pin(130, Pin.IN)
led = Pin(131, Pin.OUT)

while True:
    v = btn.value()
    print(v)
    led.value(v)
    time.sleep(0.1)
  • Test Result

Connect an LED light to pin 131 (PE3); Connect a button to pin 130 (PE2)

53

After running, you can turn on and off the LED lights by pressing the button

Was this article helpful?

TOP