Zoran Roncevic
Published © GPL3+

BeagleBone barometer

Example for using BeagleBone, Mikroelektronika Barometer click and Python

BeginnerProtip1,850
BeagleBone barometer

Things used in this project

Hardware components

BeagleBone Black
BeagleBoard.org BeagleBone Black
×1
MIKROE MicroBUS Cape
×1
MIKROE Barometer click
×1

Story

Read more

Code

Main code

Python
Main code for testing Barometer click
from Adafruit_I2C import Adafruit_I2C 
from mikroe_baro import BAROMETER_CLICK

bm = BAROMETER_CLICK()
bm.setActive()
bm.Config()

print format(bm.whoAmI(),'02x')
print format(bm.getStatus(),'08b')
print bm.getTempC()
print bm.getPressure()

raw_input('Press any key....')

Library for Barometer click

Python
Python class for Barometer click
from Adafruit_I2C import Adafruit_I2C 


def twos_comp(val, bits):
    """compute the 2's compliment of int value val"""
    if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
        val = val - (1 << bits)        # compute negative value
    return val
    
class BAROMETER_CLICK:

    address = None
    i2C = None
    WHO_AM_I = 0x0F
    PRESS_OUT_XL = 0x28
    PRESS_OUT_L = 0x29
    PRESS_OUT_H = 0x2A
    CTRL_REG1 = 0x20
    STATUS_REG = 0x27
    TEMP_OUT_L = 0x2B
    TEMP_OUT_H = 0x2C
    
    def __init__(self, address = 0x5D):
        self.address = address
        self.i2C = Adafruit_I2C(self.address)
        
    def whoAmI(self):
        reg_val = self.i2C.readU8(self.WHO_AM_I)
        return reg_val
        
    def setActive(self):
        reg_val = self.i2C.readU8(self.CTRL_REG1)
        reg_val = reg_val | 0x80
        self.i2C.write8(self.CTRL_REG1,reg_val)
        
    def Config(self):
        self.i2C.write8(self.CTRL_REG1,0xB0)
        
    def getStatus(self):
        reg_val = self.i2C.readU8(self.STATUS_REG)
        return reg_val
        
    def getTempC(self):
        reg_H = self.i2C.readU8(self.TEMP_OUT_H)
        reg_L = self.i2C.readU8(self.TEMP_OUT_L)
        res = (reg_H<<8) | (reg_L)
        res = twos_comp(res,16)
        res = res/480.0 + 42.5
        return res
        
    def getPressure(self):
        reg_XL = self.i2C.readU8(self.PRESS_OUT_XL)
        reg_L = self.i2C.readU8(self.PRESS_OUT_L)
        reg_H = self.i2C.readU8(self.PRESS_OUT_H)
        res = (reg_H << 16) | (reg_L << 8) | (reg_XL)
        res = twos_comp(res,24)
        return res/4096.0

Credits

Zoran Roncevic

Zoran Roncevic

19 projects • 127 followers
Hackster Live ambassador in Serbia. Organizer of Maker NS community.
Thanks to Mikroelektronika.

Comments