-
Notifications
You must be signed in to change notification settings - Fork 11
MRAA
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: If using Ubuntu 18.04, for UP Xtreme please follow dedicated installation instruction in the dedicated section 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 libmraa2 libmraa-dev libupm-dev libupm2 upm-examples
sudo apt-get install python-mraa python3-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
After Installing Ubuntu 20.04 Desktop for IoT Platforms, please follow these instructions to install an updated version of MRAA libraries with support for UP Xtreme i11 and UP Squared 6000:
sudo add-apt-repository ppa:up-division/mraa
sudo apt-get update
sudo apt-get install mraa-tools mraa-examples libmraa2 libmraa-dev libupm-dev libupm2 upm-examples
sudo apt-get install python3-mraa libmraa-java
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 for Ubuntu 18.04 still does not include the latest changes. If using Ubuntu 20.04 the official launchpad repository includes the necessary 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:
/usr/share/mraa/examples/
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: For executing the following script root access is required
The following Python script will blink an LED connected to GPIO 22 (pin 15) at a rate of 1Hz:
#!/usr/bin/python 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) 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 pin.write(1) time.sleep(0.5) # Turn the LED off and wait for 0.5 seconds pin.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
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)
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)
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))
NOTE: This wiki is the main source of documentation for developers working with (or contributing to) the UP products. If this is your first time hearing about UP, we recommend starting with our main UP website, and our UP community page.