Mraa Usage
MRAA Introduction
Eclipse Mraa (Libmraa) is a C/C++ library with Java, Python, and JavaScript bindings for connecting I/O pins and buses on a variety of IoT and edge platforms, with a well-structured API and port names/numbers that match the boards on which they are located.
Install MRAA
sudo apt-get update -y
sudo apt-get install libmraa2 libmraa-dev libmraa-java python3-mraa mraa-tools -y
MRAA Command Line Tools
GPIO
- mraa-gpio list: List all available pins
- mraa-gpio get pin: Get Pin Status
- mraa-gpio set pin level: Set Pin Status
- mraa-gpio version: Get MRAA Version
I2C
- mraa-i2c list: List available I2C buses
- mraa-i2c version: Get mraa version and board name
- mraa-i2c detect bus: List detected devices on specified bus
- mraa-i2c get bus device reg: Get value from specified device register
- mraa-i2c set bus device reg value: Set specified device register to value
UART
- mraa-uart list: Lists UARTs on the system
- mraa-uart dev dev_num baud customized_baud send str: send str to port dev_num
- mraa-uart dev dev_num baud customized_baud recv 1000: receive 1000 bytes from port dev_num
mraa-uart
usage exampleSample Code
GPIO
- C
- Python
blink.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa/gpio.h"
/* gpio declaration */
#define GPIO_PIN 3
volatile sig_atomic_t flag = 1;
void sig_handler(int signum) {
if(signum == SIGINT) {
fprintf(stdout, "Exiting...\n");
flag = 0;
}
}
int main(void) {
mraa_result_t status = MRAA_SUCCESS;
mraa_gpio_context gpio;
signal(SIGINT, sig_handler);
mraa_init();
/* initialize GPIO */
gpio = mraa_gpio_init(GPIO_PIN);
if(gpio == NULL) {
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN);
mraa_deinit();
return EXIT_FAILURE;
}
/* set output */
status = mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
if(status != MRAA_SUCCESS) {
goto err_exit;
}
while(flag) {
status = mraa_gpio_write(gpio, 1);
if(status != MRAA_SUCCESS) {
goto err_exit;
}
sleep(1);
status = mraa_gpio_write(gpio, 0);
if(status != MRAA_SUCCESS) {
goto err_exit;
}
sleep(1);
}
/* release gpio's */
status = mraa_gpio_close(gpio);
if(status != MRAA_SUCCESS) {
goto err_exit;
}
mraa_deinit();
return EXIT_SUCCESS;
err_exit:
mraa_result_print(status);
mraa_deinit();
return EXIT_FAILURE;
}
Usage:
-
Connect the led signal pin to PIN3, VCC to the board VCC, GND to the board GND
-
Test
gcc blink.c -lmraa -o blink && sudo . /blink
If the test is successful, the led will have a blinking effect.
blink.py
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import mraa
import time
pin = 3 # 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")
Usage:
-
Connect the led signal pin to PIN3, VCC to the board VCC, GND to the board GND
-
Test
sudo python3 Blink.py
If the test is successful, the led will have a blinking effect.
I2C
- C
- Python
led_i2c_blink.c
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa/i2c.h"
#define I2C_BUS 2
#define LED_ADDRESS 0x20
volatile sig_atomic_t flag = 1;
void sig_handler(int signum) {
if (signum == SIGINT) {
fprintf(stdout, "Exiting...\n");
flag = 0;
}
}
int main() {
mraa_result_t status = MRAA_SUCCESS;
mraa_i2c_context i2c;
signal(SIGINT, sig_handler);
mraa_init();
i2c = mraa_i2c_init(I2C_BUS);
if(i2c == NULL) {
fprintf(stderr, "Failed to initialize I2C\n");
mraa_deinit();
return EXIT_FAILURE;
}
/* set slave address */
status = mraa_i2c_address(i2c, LED_ADDRESS);
if(status != MRAA_SUCCESS) {
return -1;
}
while(1) {
/**
* Write a single word to an i2c context
*
* @param dev The i2c context
* @param data The word to write
* @param command The register
* @return Result of operation
*/
mraa_i2c_write_byte_data(i2c, 0x23, 0x08);
sleep(1);
mraa_i2c_write_byte_data(i2c, 0x21, 0x01);
sleep(1);
mraa_i2c_write_byte_data(i2c, 0x33, 0x12);
sleep(1);
mraa_i2c_write_byte_data(i2c, 0x1f, 0x42);
sleep(1);
}
return 0;
}
Usage:
-
Turn on Overlay of I2C2 and reboot.
-
Wiring: SDA <--> SDA, SCL <--> SCL, GDN <--> GND, VCC <--> VCC
-
Open the terminal and enter the following command to test
gcc led_i2c_blink.c -lmraa -o led_i2c_blink && sudo ./led_i2c_blink
If the test is successful, the I2C Led will have a running light effect.
led_i2c_blink.py
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import mraa
import time
# initialise I2C
x = mraa.I2c(2)
x.address(0x20)
while True:
x.writeReg(0x11, 0x01)
time.sleep(0.5)
x.writeReg(0x12, 0x02)
time.sleep(0.5)
x.writeReg(0x13, 0x03)
time.sleep(0.5)
x.writeReg(0x14, 0x04)
time.sleep(0.5)
x.writeReg(0x15, 0x05)
time.sleep(0.5)
x.writeReg(0x16, 0x06)
Usage:
-
Turn on Overlay for I2C2 and restart the
-
Wiring: SDA <--> SDA, SCL <--> SCL, GDN <--> GND, VCC <--> VCC
-
Open a terminal and enter the following command to test
sudo python3 led_i2c_blink.py
PWM
Due to changes in the pwmchip driver, the PWM function of the MRAA does not work properly on the 6.1 kernel
- C
- Python
led_pwm_fade_out.c
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "mraa/pwm.h"
/* PWM period in us */
#define PWM_FREQ 1e3
char *usage =
"Usage: %s pwm_pin\n"
"Example: %s 3\n";
int main(int argc, char *argv[]) {
char usagestr[130];
char *str = NULL;
int invalid = 0, pwm_num = 0;
mraa_result_t status = MRAA_SUCCESS;
mraa_pwm_context pwm;
float output = 0.0;
float duty_cycle = 0.0;
float step = 0.05;
memset(usagestr, '\0', 130);
// expect only 1 argument => argc must be 2
if(argc != 2) {
snprintf(usagestr, 129, usage, argv[0], argv[0]);
puts(usagestr);
return -1;
}
// check for a valid, numeric argument
str = argv[1];
while(*str != '\0') {
if(!isdigit(*str)) {
invalid = 1;
}
str++;
}
if(invalid == 1) {
printf("%s: Invalid GPIO %s\n", argv[0], argv[2]);
return -1;
}
pwm_num = atoi(argv[1]);
mraa_init();
/* init pwm */
if((pwm = mraa_pwm_init(pwm_num)) == NULL) {
printf("Failed to initialize PWM\n");
mraa_deinit();
return -1;
}
/* set period */
if((status = mraa_pwm_period_us(pwm, PWM_FREQ))) {
printf("Failed to set pwm period\n");
return -1;
}
/* enable pwm */
if((status = mraa_pwm_enable(pwm, 1)) != MRAA_SUCCESS) {
printf("Failed to Enable PWM\n");
return -1;
}
while (1) {
// Set pwm duty cycle
mraa_pwm_write(pwm, duty_cycle);
// sleep 100 ms
usleep(100000);
// change the duty cycle
duty_cycle += step;
if (duty_cycle > 1.0) {
duty_cycle = 0.0;
}
printf("PWM value: %f\n", mraa_pwm_read(pwm));
}
mraa_pwm_close(pwm);
return 0;
}
Usage:
-
Turn on the PWM Overlay of a certain PIN and restart, take the PWM of PIN28 as an example.
-
Connect the signal pin of the led to the PIN28 pin, VCC to the VCC of the board, and GDN to the GND of the board.
-
Test
gcc led_pwm_fade_out.c -lmraa -o led_pwm_fade_out && sudo ./led_pwm_fade_out 28
If the test is successful, the led will have a breathing light effect.
led_pwm_fade_out.py
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import mraa
import time
class Led_pwm_fade_out:
def __init__(self, pwm_pin):
self.pwm_pin = pwm_pin
self.pwm = mraa.Pwm(pwm_pin)
self.duty_cycle = 0.0
self.pwm_freq = 1e3
self.pwm_step = 0.05
def initialize(self):
if self.pwm == None:
exit()
if self.pwm.period_us(self.pwm_freq) != mraa.SUCCESS:
mraa.Pwm.close()
exit()
if self.pwm.enable(True) != mraa.SUCCESS:
mraa.Pwm.close()
exit()
def set_duty_cycle(self, duty_cycle):
self.duty_cycle = duty_cycle
self.pwm.write(duty_cycle)
def run(self):
try:
while True:
self.set_duty_cycle(self.duty_cycle)
self.duty_cycle += self.pwm_step
if self.duty_cycle > 1.0:
self.duty_cycle = 0.0
print("0x%x\n" %self.pwm.read(self.pwm))
time.sleep(1)
except:
print("Error")
finally:
self.pwm.write(0) # Stop PWM
self.pwm.disable() # Disable PWM
self.pwm.close() # Close PWM
if __name__ == '__main__':
pwm_controller = Led_pwm_fade_out(28)
pwm_controller.initialize()
pwm_controller.run()
How to use:
-
Turn on the PWM Overlay of a certain PIN and restart, take the PWM of PIN28 as an example.
-
Connect the signal pin of the led to the PIN28 pin, VCC to the VCC of the board, and GDN to the GND of the board.
-
Test
sudo python3 Led_pwm_fade_out.py
If the test is successful, the led will have a breathing light effect.