MRAA/UPM

From UpWiki
Jump to: navigation, search

Introduction

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) and is included with ubilinux.

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 is also included with ubilinux.

Setup

mraa and upm for UP are already installed by default with ubilinux. To ensure that the most up-to-date version is installed, please run the following commands:

sudo apt-get update
sudo apt-get install mraa upm

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

Usage

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

Examples are installed on UP at the following path:

/usr/share/mraa/examples/
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 4 (pin 7) at a rate of 1Hz:

#!/usr/bin/python

import sys
import mraa
import time

# Use pin 7 by default
pin_no = 7

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

# Small delay to allow udev rules to execute (necessary only on up)
time.sleep(0.1)

# Configure the pin direction
pin.dir(mraa.DIR_OUT)

# Loop
while True:
    # Turn the LED on and wait for 0.5 seconds
    x.write(1)
    time.sleep(0.5)
    # Turn the LED off and wait for 0.5 seconds
    x.write(0)
    time.sleep(0.5)

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.

#!/usr/bin/python

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
time.sleep(0.1)

# Configure the pin directions
led.dir(mraa.DIR_OUT)
button.dir(mraa.DIR_IN)

# Loop
while True:
    # If button input is HIGH, turn LED off
    value = button.read()
    if value:            # If button input is LOW, turn LED on
        led.write(0) 
    else:
        led.write(1)

RTENOTITLE

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

#!/usr/bin/python

import sys
import mraa
import time

ledPin = 32

led = mraa.Pwm(ledPin)
led.enable(True)
duty = 0.0

try:
    while 1:
        while duty < 1.0:
            led.write(duty)
            duty += 0.05
            time.sleep(0.1)
        while duty > 0.0:
            led.write(duty)
            duty -= 0.05
            time.sleep(0.1)
except KeyboardInterrupt:
    pass

led.write(0)
led.enable(False)

RTENOTITLE

Example4: Control an I2C device

This script controls the voltage on a MCP4725 DAC from Sparkfun (https://www.sparkfun.com/products/12918) connected to I2C-1 (pins 3, 5)

#!/usr/bin/python

import mraa
import sys

dac = mraa.I2c(0)
dac.address(0x60)

DEVICE_REG = 0x40

# 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]
dac.write(bytearray(data))