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_functionimport time, sys, atexitfrom upm import pyupm_lsm6dsl as sensorObjdefmain():# 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 millisecondswhile (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 temperatureprint("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.
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 pythonfrom__future__import print_functionimport time, sys, atexitfrom upm import pyupm_mcp9808 as MCP9808defmain():# 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 secondsif__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.pyTemperature :24.875Temperature :24.875Temperature :24.875Temperature :24.875Temperature :24.875Temperature :24.9375Temperature :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)KeyboardInterruptroot@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_functionimport time, sys,atexitfrom upm import pyupm_bmp280 as sensorObjdefmain():# 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.pyrandom: python3: uninitialized urandom read (24bytes read)libmraa[125]: libmraa version v2.0.0 initialised by user 'root'with EUID 0libmraa[125]: gpio: platform doesn't support chardev, falling back to sysfslibmraa[125]: libmraa initialised for platform 'Atmel SAMA5' of type20libmraa[125]: i2c_init: Selected bus 2Compensation Temperature:21.790000915527344 C /71.222000122070312 FPressure:65306.53515625 PaComputed Altitude:3552.7236328125 mHumidity:23.5859375%RHCompensation Temperature:28.770000457763672 C /83.786003112792969 FPressure:91468.453125 PaComputed Altitude:854.47808837890625 mHumidity:37.09765625%RHCompensation Temperature:28.879999160766602 C /83.984001159667969 FPressure:91469.4765625 PaComputed Altitude:854.3856201171875 mHumidity:37.416015625%RHCompensation Temperature:28.909999847412109 C /84.038002014160156 FPressure:91469.5625 PaComputed Altitude:854.3778076171875 mHumidity:37.388671875%RHCompensation Temperature:28.920000076293945 C /84.055999755859375 FPressure:91469.34375 PaComputed Altitude:854.3975830078125 mHumidity:37.3603515625%RHCompensation Temperature:29.0 C /84.199996948242188 FPressure:91468.125 PaComputed Altitude:854.50775146484375 mHumidity:38.0966796875%RHCompensation Temperature:30.0 C /86.0 FPressure:91467.4375 PaComputed Altitude:854.5699462890625 mHumidity:41.685546875%RHCompensation Temperature:30.579999923706055 C /87.043998718261719 FPressure:91466.1328125 PaComputed Altitude:854.6878662109375 mHumidity:44.8154296875%RHCompensation Temperature:30.950000762939453 C /87.709999084472656 FPressure:91472.4140625 PaComputed Altitude:854.12005615234375 mHumidity:47.4365234375%RHCompensation Temperature:31.309999465942383 C /88.358001708984375 FPressure:91466.9375 PaComputed Altitude:854.6151123046875 mHumidity:49.5576171875%RHCompensation Temperature:31.590000152587891 C /88.86199951171875 FPressure:91466.296875 PaComputed Altitude:854.67303466796875 mHumidity:51.091796875%RHCompensation Temperature:31.329999923706055 C /88.393997192382812 FPressure:91469.1640625 PaComputed Altitude:854.41387939453125 mHumidity: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)