Skip to main content

How to control ROCK GPIO with MRAA

One of the main features of the ROCK range of Single Board Computers is their input/output capability using the GPIO to control various additional hardware. Many ways exist to do this, but they tend to require expert knowledge and vary between boards.

MRAA is an Open Source project from the Eclipse Foundation that aims to make using the GPIO far easier.

In this article, I show how to use MRAA and Python to control electronics connected to the GPIO on ROCK boards. The example uses the ROCK 4C+, but it can be applied to any of the ROCK range and only requires a few components to get started.

ROCK 4 C and Test Circuit

Difficulty: Easy
Time: 1 Hr
Steps: 7
Credit: None
Licence: The MIT License (MIT) https://projects.eclipse.org/license/mit-license-mit

Step 1: Parts

Only a few easily obtainable components are required to start using the GPIO to connect to electronic devices.

You will need a ROCK board, for example the ROCK 4C+ (249-3158) .

For the breadboard test circuit, the following components are required:

Optionally you can run a test using just a Multimeter (123-1938)

Step 2: Installation

To follow along, you will need a ROCK board with a Debian Operating System installed. You can download the OS for your board from the OKdo Software and Downloads Hub.

MRAA is included in the Debian images so is easy to install with the following on the command-line:

sudo apt update
sudo apt install libmraa2 libmraa-dev libmraa-java python3-mraa mraa-tools

Step 3: ROCK GPIO

Each ROCK board has its own GPIO map which lays out the pin names and their functions. ROCK GPIO Headers are colour-coded to match the Pin Map to make identification simpler.

Warning: It is very important not to exceed the Maximum Voltage and Current ratings documented for each pin as this can permanently damage the board

GPIO Pin Maps along with Absolute Maximum Ratings and Notes can be found on the Radxa documentation site. For example, here’s a link to the page for the ROCK 4C+ board: https://docs.radxa.com/en/rock4/hardware/rock4-gpio

ROCK GPIO Map

Step 4: Circuit

Build a simple test circuit on a breadboard using jumper wires with a Red LED connected to 5V via a 220 Ohm Resistor and an N-channel MOSFET (BS270).

Tip: MRAA Pin numbering refers to the physical pins on the GPIO Header, not the GPIO designation

I have chosen Pin 16 connected to the Gate of the MOSFET to control the LED. 5V power and Ground are connected to Pin 2 (5V) and Pin 14 (GND) respectively.

GPIO pins are not capable of sinking or sourcing much current so it’s good practice to use a MOSFET or BJT to protect the pins. The resistor in this circuit limits the current through the LED.

This MOSFET circuit is driven by the voltage applied to the Gate of the device and almost zero current flows through the GPIO pin it is connected to. When the pin 16 is high, the LED turns on and when it's low it is off.

If you don’t have the parts you can measure the Voltage across Pin 16 and GND with a Multimeter. You would see a voltage of about 3V when the pin is high and 0V when it’s low.

Circuit Board

Step 5: MRAA Commands

MRAA has a command line utility mraa-gpio with various options to control the pins.

Get the MRAA Version:

mraa-gpio version
Version v2.2.0 on ROCK Pi 4

List all available pins and their functions:

mraa-gpio list
01         3V3: 
02          5V: 
03        SDA7: GPIO I2C  
04          5V: 
05        SCL7: GPIO I2C  
06         GND: 
07    SPI2_CLK: GPIO SPI  
08        TXD2: GPIO UART 
09         GND: 
10        RXD2: GPIO UART 
11        PWM0: GPIO PWM  
12    GPIO4_A3: GPIO 
13        PWM1: GPIO PWM  
14         GND: 
15    GPIO4_C5: GPIO 
16    GPIO4_D2: GPIO 
17         3V3: 
18    GPIO4_D4: GPIO 
19 SPI1TX,TXD4: GPIO SPI  UART 
20         GND: 
21 SPI1RX,RXD4: GPIO SPI  UART 
22    GPIO4_D5: GPIO 
23     SPI1CLK: GPIO SPI  
24      SPI1CS: GPIO SPI  
25         GND: 
26     ADC_IN0: AIO  
27        SDA2: GPIO I2C  
28        SCL2: GPIO I2C  
29 SCL6,SPI2RX: GPIO I2C  SPI  
30         GND: 
31 SDA6,SPI2TX: GPIO I2C  SPI  
32    GPIO3_C0: GPIO 
33      SPI2CS: GPIO SPI  
34         GND: 
35    GPIO4_A5: GPIO 
36    GPIO4_A4: GPIO 
37    GPIO4_D6: GPIO 
38    GPIO4_A6: GPIO 
39         GND: 
40    GPIO4_A7: GPIO

Turn on the LED connected to pin 16 then turn it off again:

mraa-gpio set 16 1
mraa-gpio set 16 0

Step 6: Python Programming

MRAA has bindings for C and Python programming languages. Here is a simple example to blink the LED using the test circuit above with Python.

Use your favourite editor to add the following Python code and save the file as blink.py:

import mraa
import time

pin = 16  # set pin num

try:
    gpio = mraa.Gpio(pin)
    gpio.dir(mraa.DIR_OUT)  # set mode as input

    while True:
        gpio.write(1)
        time.sleep(1)
        gpio.write(0)
        time.sleep(1)

except KeyboardInterrupt:
    print("\nstop\n")

Open a Terminal in the same directory as blink.py and run the following command to blink the LED:

python ./blink.py

Use CTRL + C to stop the program

Step 7: Next Steps

MRAA can be programmed with C if you need the best performance, but it is more complicated than with Python.

Other electrical interfaces can also be controlled with MRAA such as I2C, SPI, PWM and UART. Simple examples are listed on the Radxa website: https://docs.radxa.com/en/compute-module/cm5/radxa-os/app-dev/mraa

Further information can be found in the Eclipse Project website and Github repository:

Summary

MRAA aims to be a platform independent way to access and control the GPIO on SBC’s. The Open Source project is run by the Eclipse Foundation so has wide industry support including upstream contributions from Radxa for the ROCK boards.

Hopefully, we have demonstrated how easy it is to start using MRAA in your ROCK projects to control hardware from the GPIO.

I'm an engineer and Linux advocate with probably more SBCs than a Odysseus moon lander

Comments