Setting up the UP Squared* Grove* IoT Development Kit Using Python*

Overview

To interface with low-level IO on the Up Squared board we will use a library called MRAA. To interface with specific sensors at a high-level we will use the UPM library which offers support for 400+ sensors.

To interface with the IO on the GrovePi shield you are required to use the Subplatform API in MRAA. This means you need to add an offset of 512 to any IO calls. So D4 would become 516 (512 + 4) and A1 would become 513 (512 + 1). See the documentation linked below for more information.

The Up Squared also has native IO which is pin compatible with the Raspberry Pi IO header. This can also be accessed using the MRAA library.

Setup

The Ubuntu Server image comes with Python installed. If you want to work with the Grove Sensors using the MRAA and UPM libraries (recommended) run the following command:

sudo apt install python-mraa python-upm

Sensors

The Up Squared Grove IoT Developer Kit includes a selection of sensors. Below is some sample code for communicating with these sensors using the included GrovePi shield.

Documentation

Note: The default upsquared user does not have the required privileges to access low-level devices. As such whenever you run code which is accessing a sensor using MRAA or UPM always run with elevated privileges.

Sample Code

Ensure you have installed the required packages for your preferred programming language before continuing.

Grove LCD RGB Backlight

The LCD screen and RGB backlight is an I2C device so needs to be connected to one of the I2C connectors on the GrovePi shield (e.g. I2C-2).

from upm import pyupm_jhd1313m1 as lcd

# Initialise JHD1313 at 0x3E (LCD_ADDRESS) and 0x62 (RGB_ADDRESS)
myLcd = lcd.Jhd1313m1(0, 0x3E, 0x62)

# RGB Red
myLcd.setColor(255, 0, 0)

myLcd.setCursor(0,0)
myLcd.write('Hello World')
myLcd.setCursor(1,2)
myLcd.write('Hello World')

Rotary Angle Sensor

The Rotary Angle Sensor is an analog device so needs to be connected to one of the analog inputs on the GrovePi shield (e.g. A0).

import mraa
from upm import pyupm_grove as grove

# Initialise GrovePi subplatform
mraa.addSubplatform(mraa.GROVEPI, "0")

# New rotary sensor on AIO pin 0
rotary = grove.GroveRotary(512)

# Read absolute values
abs = rotary.abs_value()
absdeg = rotary.abs_deg()
absrad = rotary.abs_rad()

# Read relative values
rel = rotary.rel_value()
reldeg = rotary.rel_deg()
relrad = rotary.rel_rad()

print("Absolute values | Raw: %4d" % int(abs), " Degrees: %3d" % int(absdeg), " Radian: %5.2f" % absrad)
print("Relative values | Raw: %4d" % int(rel) , " Degrees: %3d" % int(reldeg), " Radian: %5.2f" % relrad)

Grove Light Sensor

The Grove Light Sensor is an analog device so needs to be connected to one of the analog inputs on the GrovePi shield (e.g. A0).

import mraa
from upm import pyupm_grove as grove

# Initialise GrovePi subplatform
mraa.addSubplatform(mraa.GROVEPI, "0")

# Create a light sensor object using AIO pin 0
light = grove.GroveLight(512)

raw = light.raw_value()
lux = light.value();

print("Raw value is %d" % raw + ", which is roughly %d" % lux + " lux");

LED

The LED socket is a digital actuator so needs to be connected to one of the digital inputs on the GrovePi shield (e.g. D4).

import time
import mraa
from upm import pyupm_led

# Initialise GrovePi subplatform
mraa.addSubplatform(mraa.GROVEPI, "0")

# Create the Grove LED object using GPIO pin 4 (D4)
led = pyupm_led.Led(516)

# Turn the LED on and off 10 times, pausing one second between transitions
for i in range (0,10):
    led.on()
    time.sleep(1)
    led.off()
    time.sleep(1)

Button

The Button is a digital actuator so needs to be connected to one of the digital inputs on the GrovePi shield (e.g. D4).

import time
import mraa
from upm import pyupm_grove as grove

# Initialise GrovePi subplatform
mraa.addSubplatform(mraa.GROVEPI, "0")

# Create the button object using GPIO pin 4 (D4)
button = grove.GroveButton(516)

# Read the input and print, waiting one second between readings
while 1:
    print(button.name(), ' value is ', button.value())
    time.sleep(1)

Temperature & Humidity Sensor

The Temperature & Humidity sensor is an I2C sensor so needs to be connected to one of the I2C inputs on the GrovePi shield (e.g. I2C-1).

import mraa
from upm import pyupm_si7005 as upm

# Initialise GrovePi subplatform
mraa.addSubplatform(mraa.GROVEPI, "0")

# Initialise sensor
sensor = upm.SI7005(0, 512)

temp = sensor.getTemperatureCelsius()
hum = sensor.getHumidityRelative()

print("Temperature is currently %d" % temp + " C");
print("Humidity is currently %d" % hum + " %");

Additional Sensors

For a list of additional sensors supported by the UPM library and sample code for various programming languages go HERE.

For more complete information about compiler optimizations, see our Optimization Notice.