From UpWiki
Jump to: navigation, search


libmraa is a low-level library, developed by Intel, for accessing the I/O functions (GPIO, I2C, SPI, PWM, UART) on a variety of boards such as Intel's Galileo and Edison boards, MinnowBoard Max, Raspberry Pi, and more. It is written in C/C++ and provides Python and Javascript bindings. libmraa supports the UP board since (v0.9.5).

upm is a high-level library that makes use of mraa, and provides packages/modules to manage a variety of sensors and actuators. v0.5.1.


Note: For UP Xtreme please follow dedicated installation instruction below

To install and ensure that the most up-to-date version is installed, please run the following commands:

sudo add-apt-repository ppa:mraa/mraa
sudo apt-get update
sudo apt-get install mraa-tools mraa-examples libmraa1 libmraa-dev libupm-dev libupm1 upm-examples
sudo apt-get install python-mraa python3-mraa node-mraa libmraa-java

For javascript/nodejs support, it is recommended to install mraa using npm instead:

$ sudo apt-get update
$ sudo apt-get install nodejs nodejs-legacy npm
$ npm install mraa

Setup for UP Xtreme

The necessary changes for supporting UP Xtreme have been upstreamed to the official MRAA Github Repository:

Unfortunately the deb pre-packaged version available on the official launchpad repository still does not include the latest changes. We have compiled and pre-packaged the MRAA libraries in our repository and you can download and install following these steps:

sudo add-apt-repository ppa:aaeonaeu/libmraa
sudo apt-get update
sudo apt-get install mraa-tools mraa-examples libmraa-dev libmraa2
sudo apt-get install python-mraa python3-mraa node-mraa libmraa-java
sudo reboot


The following page contains links to examples and API documentation for mraa :

Examples are installed on UP at the following path:

Warning Warning: in mraa the GPIO pins are numbered according to the physical pin numbers (1-40) instead of using Rpi notation (see the pinout reference)
Warning Warning: For executing the following script root access is required

Example 1: Blink an LED on a GPIO output pin

The following Python script will blink an LED connected to GPIO 22 (pin 15) at a rate of 1Hz:


import sys
import mraa
import time

# Use pin 7 by default
pin_no = 15

# Export the GPIO pin for use
pin = mraa.Gpio(pin_no)

# Small delay to allow udev rules to execute (necessary only on up)

# Configure the pin direction

# Loop
while True:
    # Turn the LED on and wait for 0.5 seconds
    # Turn the LED off and wait for 0.5 seconds

This is javascript version of the example above:

var m = require('mraa'); //require mraa

var myLed = new m.Gpio(7); //LED hooked up to digital pin 7
myLed.dir(m.DIR_OUT);      //set the gpio direction to output
var ledState = true;       //Boolean to hold the state of Led

function periodicActivity()
    myLed.write(ledState?1:0);           //if ledState is true then write a '1' (high) otherwise write a '0' (low)
    ledState = !ledState;                //invert the ledState
    setTimeout(periodicActivity,500);    //call the indicated function after 1 second (1000 milliseconds)

periodicActivity(); //call the periodicActivity function

Led blink

Example 2: Turn an LED on/off by detecting a button press on a GPIO

In this example, an LED is connected to GPIO output 23 (pin 16) and a button connected to GPIO input 24 (pin 18). The LED will turn on when the button is pressed.


import sys
import mraa
import time

ledPin = 16
buttonPin = 18

# Export the GPIO pins for use
led = mraa.Gpio(ledPin)
button = mraa.Gpio(buttonPin)

# Small delay to allow udev rules to execute

# Configure the pin directions

# Loop
while True:
    # If button input is HIGH, turn LED off
    value =
    if value:            # If button input is LOW, turn LED on


Example 3: Fade an LED using hardware PWM

This script will slowly fade on/off an LED connected to PWM0 (pin 32)

NOTE: this needs to be run as root user


import sys
import mraa
import time

ledPin = 32

led = mraa.Pwm(ledPin)
duty = 0.0

    while 1:
        while duty < 1.0:
            duty += 0.05
        while duty > 0.0:
            duty -= 0.05
except KeyboardInterrupt:



Example4: Control an I2C device

This script controls the voltage on a MCP4725 DAC from Sparkfun ( connected to I2C-1 (pins 3, 5)


import mraa
import sys

dac = mraa.I2c(0)


# DAC VOUT level is a 12-bit number (0-4095)
# 0 = 0V, 4095 = VCC

level = 2048
if (len(sys.argv) > 1):
    level = int(sys.argv[1])

if (level > 4095):
    level = 4095
if (level < 0):
    level = 0

data = [DEVICE_REG, level >> 4, (level & 0xF) << 4]