The UPM repository provides software drivers for a wide variety of commonly used sensors and actuators. These software drivers interact with the underlying hardware platform (or microcontroller), as well as with the attached sensors, through calls to [MRAA](https://github.com/intel-iot-devkit/mraa) APIs.
Programmers can access the interfaces for each sensor by including the sensor’s corresponding header file and instantiating the associated sensor class. In the typical use case, a constructor initializes the sensor based on parameters that identify the sensor, the I/O protocol used and the pin location of the sensor.
We endorse additions that implement the generic C and C++ interfaces provided with the libraries. Multiple sensor and actuator types have been defined, for instance:
Light controller
Light sensor
Temperature sensor
Humidity sensor
Pressure sensor
Gas sensor
Analog to digital converter
Simple diagram below depicts the use of Lib-UPM to interface various sensor modules.
1. 3D Digital accelerometer and 3D Digital gyroscope (lsm6sdl) Testing with upm library
Description
To test the Temperature sensor on a5d2x-rugged board.
Required Hardware
Sensor Tag
a5d2x-rugged board
USB cable
Step-by-step guide
Connect the sensor tag on M1 connector of the Rugged board.
Boot the board with SD card/NOR.
Copy the below file to mnt directory of rugged board through tftp protocol.
vi lsm6sdl.py
from __future__ import print_function
import time, sys, atexit
from upm import pyupm_lsm6dsl as sensorObj
def main():
# Instantiate a BMP250E instance using default i2c bus and address
sensor = sensorObj.LSM6DSL(0,0x6b,-1)
# For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS:
#LSM6DSL(0, -1, 10);
# now output data every 250 milliseconds
while (1):
sensor.update()
data = sensor.getAccelerometer()
print("Accelerometer x:", data[0], end=' ')
print(" y:", data[1], end=' ')
print(" z:", data[2], end=' ')
print(" g")
data = sensor.getGyroscope()
print("Gyroscope x:", data[0], end=' ')
print(" y:", data[1], end=' ')
print(" z:", data[2], end=' ')
print(" dps")
# we show both C and F for temperature
print("Compensation Temperature:", sensor.getTemperature(), "C /", end=' ')
print(sensor.getTemperature(True), "F")
print()
time.sleep(.250)
if __name__ == '__main__':
main()
Run the above code with below command in rugged board.
$ python3 lsm6sdl.py
Expected Output
root@rugged-board-a5d2x-sd1:/mnt#python3 lsm6sdl.py
Accelerometer x: 0.065086998045444489 y: -0.045079004019498825 z: 1.0196759700775146 g
Gyroscope x: 0.25900000333786011 y: -0.29800000786781311 z: -0.34900000691413879 dps
Compensation Temperature: 24.995330810546875 C / 76.991592407226562 F
Accelerometer x: 0.065880000591278076 y: -0.043554000556468964 z: 1.0225430727005005 g
Gyroscope x: 0.25299999117851257 y: -0.29300001263618469 z: -0.35199999809265137 dps
Compensation Temperature: 24.995513916015625 C / 76.991928100585938 F
Accelerometer x: 0.065758004784584045 y: -0.043248999863862991 z: 1.0232139825820923 g
Gyroscope x: 0.25600001215934753 y: -0.29800000786781311 z: -0.35100001096725464 dps
Compensation Temperature: 24.99554443359375 C / 76.991981506347656 F
Accelerometer x: 0.065026000142097473 y: -0.043615002185106277 z: 1.0236409902572632 g
Gyroscope x: 0.25299999117851257 y: -0.29499998688697815 z: -0.3529999852180481 dps
Compensation Temperature: 24.9957275390625 C / 76.9923095703125 F
Accelerometer x: 0.065940998494625092 y: -0.044590998440980911 z: 1.0250440835952759 g
Gyroscope x: 0.25499999523162842 y: -0.30300000309944153 z: -0.34999999403953552 dps
Compensation Temperature: 24.995635986328125 C / 76.992141723632812 F
Accelerometer x: 0.063195995986461639 y: -0.044225003570318222 z: 1.0248609781265259 g
Gyroscope x: 0.25799998641014099 y: -0.30899998545646667 z: -0.35199999809265137 dps
Compensation Temperature: 24.99578857421875 C / 76.992416381835938 F
Accelerometer x: 0.067344002425670624 y: -0.047091998159885406 z: 1.0254100561141968 g
Gyroscope x: 0.24799999594688416 y: -0.28499999642372131 z: -0.35499998927116394 dps
Compensation Temperature: 24.995742797851562 C / 76.992340087890625 F
Accelerometer x: -0.0023789999540895224 y: -0.087779000401496887 z: 0.98210000991821289 g
Gyroscope x: 0.12999999523162842 y: 0.71799999475479126 z: -0.26899999380111694 dps
Compensation Temperature: 24.995712280273438 C / 76.992279052734375 F
Accelerometer x: -0.42687800526618958 y: -0.033184003084897995 z: 1.0443810224533081 g
Gyroscope x: -2.9779999256134033 y: 4.499000072479248 z: -0.9309999942779541 dps
Compensation Temperature: 24.995941162109375 C / 76.992691040039062 F
Accelerometer x: -0.47592198848724365 y: -0.20794901251792908 z: 0.70461100339889526 g
Gyroscope x: 8.3970003128051758 y: 32.763999938964844 z: 1.5110000371932983 dps
Compensation Temperature: 24.995880126953125 C / 76.992584228515625 F
Accelerometer x: 0.012810000218451023 y: -0.19733500480651855 z: 0.67978399991989136 g
Gyroscope x: -1.1679999828338623 y: 3.752000093460083 z: -0.17200000584125519 dps
Compensation Temperature: 24.995895385742188 C / 76.99261474609375 F
Accelerometer x: -0.10077200084924698 y: -0.12395200133323669 z: 1.0668900012969971 g
Gyroscope x: -0.62699997425079346 y: -18.709999084472656 z: 0.61000001430511475 dps
Compensation Temperature: 24.996047973632812 C / 76.992889404296875 F
Accelerometer x: 0.058681998401880264 y: -0.037758998572826385 z: 1.0333399772644043 g
Gyroscope x: 0.10999999940395355 y: 0.25099998712539673 z: -0.31799998879432678 dps
Compensation Temperature: 24.995986938476562 C / 76.992774963378906 F
^CTraceback (most recent call last):
File "lsm6dsl.py", line 37, in <module>
main()
File "lsm6dsl.py", line 34, in main
2. Temperature sensor(mcp9808)- Interfacing using UPM library:
Description
To test the Temperature sensor on a5d2x-rugged board.
Required Hardware
Sensor Tag
a5d2x-rugged board
USB cable
Step-by-step guide
Connect the sensor tag on M1 connector of the Rugged board.
Boot the board with SD card/NOR.
Copy the below file to mnt directory of rugged board through tftp protocol.
vi mcp9808.py
#!/usr/bin/env python
from __future__ import print_function
import time, sys, atexit
from upm import pyupm_mcp9808 as MCP9808
def main():
# Instantiate the Infrared-Thermopile Sensor on I2C on bus 1
mySensor = MCP9808.MCP9808(0,0x18)
while(1):
print("Temperature : "+str(mySensor.getTemp()))
time.sleep(.5)
# Print out temperature value and config-reg in hex every 0.5 seconds
if __name__ == '__main__':
main()
Run the above code with below command in rugged board.
$python3 mcp9808.py
Expected Output
root@rugged-board-a5d2x-sd1:/mnt/python3 mcp9808.py
Temperature : 24.875
Temperature : 24.875
Temperature : 24.875
Temperature : 24.875
Temperature : 24.875
Temperature : 24.9375
Temperature : 24.875
^CTraceback (most recent call last):
File "mcp9808.py", line 20, in <module>
main()
File "mcp9808.py", line 15, in main
time.sleep(.5)
KeyboardInterrupt
root@sama5d27-som1-ek-sd:/mnt/microBus-wrkng-upm#
3. Preassure and Humidity (bme280) sensor Interfacing using UPM library
Description
To test the Pressure and humidity sensor on a5d2x-rugged board.
Required Hardware
Sensor shield
a5d2x-rugged board
USB cable
Step-by-step guide
Connect the sensor shield on M1 connector of the Rugged board.
Boot the board with SD card/NOR.
Copy the below file to mnt directory of rugged board through tftp protocol.
vi bme280.py
from __future__ import print_function
import time, sys,atexit
from upm import pyupm_bmp280 as sensorObj
def main():
# Instantiate a BME280 instance using default i2c bus and address
sensor = sensorObj.BME280(0,0x76,-1)
# For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS:
#BME280(3, -1, 60)
while (1):
sensor.update()
print("Compensation Temperature:", sensor.getTemperature(), "C /", end=' ')
print(sensor.getTemperature(True), "F")
print("Pressure: ", sensor.getPressure(), "Pa")
print("Computed Altitude:", sensor.getAltitude(), "m")
print("Humidity:", sensor.getHumidity(), "%RH")
print()
time.sleep(1)
if __name__ == '__main__':
main()
Run the above code with below command in rugged board.
$python3 bme280.py
Expected Output
root@ruggedboard-a5d2x:~# python3 bme280.py
random: python3: uninitialized urandom read (24 bytes read)
libmraa[125]: libmraa version v2.0.0 initialised by user 'root' with EUID 0
libmraa[125]: gpio: platform doesn't support chardev, falling back to sysfs
libmraa[125]: libmraa initialised for platform 'Atmel SAMA5' of type 20
libmraa[125]: i2c_init: Selected bus 2
Compensation Temperature: 21.790000915527344 C / 71.222000122070312 F
Pressure: 65306.53515625 Pa
Computed Altitude: 3552.7236328125 m
Humidity: 23.5859375 %RH
Compensation Temperature: 28.770000457763672 C / 83.786003112792969 F
Pressure: 91468.453125 Pa
Computed Altitude: 854.47808837890625 m
Humidity: 37.09765625 %RH
Compensation Temperature: 28.879999160766602 C / 83.984001159667969 F
Pressure: 91469.4765625 Pa
Computed Altitude: 854.3856201171875 m
Humidity: 37.416015625 %RH
Compensation Temperature: 28.909999847412109 C / 84.038002014160156 F
Pressure: 91469.5625 Pa
Computed Altitude: 854.3778076171875 m
Humidity: 37.388671875 %RH
Compensation Temperature: 28.920000076293945 C / 84.055999755859375 F
Pressure: 91469.34375 Pa
Computed Altitude: 854.3975830078125 m
Humidity: 37.3603515625 %RH
Compensation Temperature: 29.0 C / 84.199996948242188 F
Pressure: 91468.125 Pa
Computed Altitude: 854.50775146484375 m
Humidity: 38.0966796875 %RH
Compensation Temperature: 30.0 C / 86.0 F
Pressure: 91467.4375 Pa
Computed Altitude: 854.5699462890625 m
Humidity: 41.685546875 %RH
Compensation Temperature: 30.579999923706055 C / 87.043998718261719 F
Pressure: 91466.1328125 Pa
Computed Altitude: 854.6878662109375 m
Humidity: 44.8154296875 %RH
Compensation Temperature: 30.950000762939453 C / 87.709999084472656 F
Pressure: 91472.4140625 Pa
Computed Altitude: 854.12005615234375 m
Humidity: 47.4365234375 %RH
Compensation Temperature: 31.309999465942383 C / 88.358001708984375 F
Pressure: 91466.9375 Pa
Computed Altitude: 854.6151123046875 m
Humidity: 49.5576171875 %RH
Compensation Temperature: 31.590000152587891 C / 88.86199951171875 F
Pressure: 91466.296875 Pa
Computed Altitude: 854.67303466796875 m
Humidity: 51.091796875 %RH
Compensation Temperature: 31.329999923706055 C / 88.393997192382812 F
Pressure: 91469.1640625 Pa
Computed Altitude: 854.41387939453125 m
Humidity: 50.951171875 %RH
^CTraceback (most recent call last):
File "bme280.py", line 33, in <module>
main()
File "bme280.py", line 30, in main
time.sleep(1)