# mikroBUS UPM Sensor

## Tempurature Sensor

Code to interface Tempurature sensor(MCP9808):

```
#!/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(2,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()
```

To run the program, \
Step 1: save the file as mcp9808.py\
Step 2: Run the Program in the board's terminal using&#x20;

```
python3 mcp9808.py
```

Readings of temperature looks like this:

![](https://2940024201-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LbvnQRGvCTc2Zmsi6BG%2F-LqyhGCh9Zu2FjydxqaD%2F-LqylCI2ID5zwy4cvDg0%2Fmcp9808_out%20\(copy\).png?alt=media\&token=863fc85a-b847-4728-9df0-94fd011b0157)

## Accelerometer Sensor

Code to interface Accelerometer Sensor (LSM6DSL):

```
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(2,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()
```

To run the program, \
Step 1: save the file as lsm6dsl.py\
Step 2: Run the Program in the board's terminal using&#x20;

```
python3  lsm6dsl.py
```

Readings of Accelerometer looks like this:

![](https://2940024201-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LbvnQRGvCTc2Zmsi6BG%2F-LqylLtEdqyl51BzTaek%2F-Lqynlqm2B-gsPzRfkmn%2Flsm6dsl_output%20\(copy\).png?alt=media\&token=007eb6d2-a798-47e1-8edc-2941ae288a2a)

## Humidity Sensor

Code to interface Humidity sensor (BME280):

```
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(2,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")
```

To run the program, \
Step 1: save the file as bme280.py\
Step 2: Run the Program in the board's terminal using&#x20;

```
python bme280.py
```

Readings of Humidity, Temputature, Pressure from BME280 sensor looks like this:

![](https://2940024201-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LbvnQRGvCTc2Zmsi6BG%2F-LqynrfRjCwlvQVCLG3k%2F-LqypMm-Gao3tXp5xeAw%2Fbme280_outrput%20\(copy\).png?alt=media\&token=27cd52a2-bb68-405c-9aca-2fa6d08cb8e3)
