mpl3115a2

package module
v0.0.0-...-817ad95 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2018 License: MIT Imports: 6 Imported by: 1

README

NXP Semiconductors MPL3115A2 pressure and temperature sensor

Build Status Go Report Card GoDoc MIT License

MPL3115A2 (pdf reference) is a popular sensor among Arduino and Raspberry PI developers. Sensor is a compact, piezoresistive, pressure and temperature sensor with an I2C digital interface: image

Here is a library written in Go programming language for Raspberry PI and counterparts, which gives you in the output temperature, atmospheric pressure and altitude values (making all necessary i2c-bus interacting and values computing).

Golang usage

func main() {
	// Create new connection to i2c-bus on 0 line with address 0x60.
	// Use i2cdetect utility to find device address over the i2c-bus
	i2c, err := i2c.NewI2C(0x60, 0)
	if err != nil {
		log.Fatal(err)
	}
	defer i2c.Close()

	sensor := mpl3115a2.NewMPL3115A2()

	// Oversample Ratio - define precision, from low(0) to high(7)
	osr := 3
	p, t, err := sensor.MeasurePressure(i2c, osr)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Pressure = %v Pa, temperature = %v *C", p, t)

	p, t, err = sensor.MeasureAltitude(i2c, osr)
	if err != nil {
		lg.Fatal(err)
	}
	log.Printf("Altitude = %v m, temperature = %v *C", p, t)
}

Getting help

GoDoc documentation

Installation

$ go get -u github.com/d2r2/go-mpl3115a2

Troubleshooting

  • How to obtain fresh Golang installation to RPi device (either any RPi clone): If your RaspberryPI golang installation taken by default from repository is outdated, you may consider to install actual golang manually from official Golang site. Download tar.gz file containing armv6l in the name. Follow installation instructions.

  • How to enable I2C bus on RPi device: If you employ RaspberryPI, use raspi-config utility to activate i2c-bus on the OS level. Go to "Interfacing Options" menu, to active I2C bus. Probably you will need to reboot to load i2c kernel module. Finally you should have device like /dev/i2c-1 present in the system.

  • How to find I2C bus allocation and device address: Use i2cdetect utility in format "i2cdetect -y X", where X may vary from 0 to 5 or more, to discover address occupied by peripheral device. To install utility you should run apt install i2c-tools on debian-kind system. i2cdetect -y 1 sample output:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

Contact

Please use Github issue tracker for filing bugs or feature requests.

License

Go-mpl3115a2 is licensed under MIT License.

Documentation

Index

Constants

View Source
const (
	// Alias for DR_STATUS or F_STATUS
	STATUS = 0x00

	// 20-bit realtime pressure sample
	OUT_PRES_MSB_CSB_LSB = 0x01
	OUT_PRES_BYTES       = 3

	// 12-bit realtime temperature sample
	OUT_TEMP_MSB_LSB = 0x04
	OUT_TEMP_BYTES   = 2

	// Data ready status information
	DR_STATUS = 0x06

	// 20-bit pressure change data
	OUT_PRES_DELTA_MSB_CSB_LSB = 0x07
	OUT_PRES_DELTA_BYTES       = 3

	// 12-bit temperature change data
	OUT_TEMP_DELTA_MSB_LSB = 0x0A
	OUT_TEMP_DELTA_BYTES   = 2

	// Fixed device ID number
	WHO_AM_I = 0x0C

	// FIFO status: no FIFO event detected
	F_STATUS = 0x0D

	// FIFO 8-bit data access
	F_DATA = 0x0E

	// FIFO setup
	F_SETUP = 0x0F

	// Time since FIFO overflow
	TIME_DLY = 0x10

	// Current system mode
	SYSMOD = 0x11

	// Interrupt status
	INT_SOURCE = 0x12

	// Data event flag configuration
	PT_DATA_CFG = 0x13

	// Barometric input for altitude calculation
	BAR_IN_MSB_LSB = 0x14
	BAR_IN_BYTES   = 2

	// Pressure/altitude target
	PRES_TGT_MSB_LSB = 0x16
	PRES_TGT_BYTES   = 2

	// Temperature target value
	T_TGT = 0x18

	// Pressure/altitude window
	PRES_WND_MSB_LSB = 0x19
	PRES_WND_BYTES   = 2

	// Temperature window
	TEMP_WND = 0x1B

	// Minimum pressure/altitude
	PRES_MIN_MSB_CSB_LSB = 0x1C
	PRES_MIN_BYTES       = 3

	// Minimum temperature
	TEMP_MIN_MSB_LSB = 0x1E
	TEMP_MIN_BYTES   = 2

	// Maximum pressure/altitude
	PRES_MAX_MSB_CSB_LSB = 0x21
	PRES_MAX_BYTES       = 3

	// Maximum temperature
	TEMP_MAX_MSB_LSB = 0x24
	TEMP_MAX_BYTES   = 2

	// Control register: Modes, oversampling
	CTRL_REG1 = 0x26

	// Control register: Acquisition time step
	CTRL_REG2 = 0x27

	// Control register: Interrupt pin configuration
	CTRL_REG3 = 0x28

	// Control register: Interrupt enables
	CTRL_REG4 = 0x29

	// Control register: Interrupt output pin assignment
	CTRL_REG5 = 0x2A

	// Pressure data offset
	OFF_PRES = 0x2B

	// Temperature data offset
	OFF_TEMP = 0x2C

	// Altitude data offset
	OFF_H = 0x2D
)

Register map

Variables

This section is empty.

Functions

This section is empty.

Types

type Flag

type Flag byte

Flag can keep any sensor register specific bit flags.

const (
	// DR_STATUS flag: Pressure/altitude or temperature data ready.
	PRES_TEMP_DATA_READY Flag = 0x8
	// DR_STATUS flag: Pressure/altitude new data available.
	PRES_DATA_READY Flag = 0x4
	// DR_STATUS flag: Temperature new data available.
	TEMP_DATA_READY Flag = 0x2
)

type MPL3115A2

type MPL3115A2 struct {
}

MPL3115A2 keeps sensor itself.

func NewMPL3115A2

func NewMPL3115A2() *MPL3115A2

NewMPL3115A2 return new sensor instance.

func (*MPL3115A2) CompensateAltitude

func (v *MPL3115A2) CompensateAltitude(i2c *i2c.I2C, shiftM int8) error

CompensateAltitude shift altitude from -128 to +127 meters. Default value is 0. Can be used for sensor calibration.

func (*MPL3115A2) CompensatePressure

func (v *MPL3115A2) CompensatePressure(i2c *i2c.I2C, shiftPa int16) error

CompensatePressure shift pressure from -512 to +508 Pascal. Default value is 0. Can be user for sensor calibration.

func (*MPL3115A2) CompensateTemperature

func (v *MPL3115A2) CompensateTemperature(i2c *i2c.I2C, shiftTemp float32) error

CompensateTemperature shift temperature from -8 to +7.9375 *C. Default value is 0. Can be used for sensor calibration.

func (*MPL3115A2) GetDefaultSeaLevelPressure

func (v *MPL3115A2) GetDefaultSeaLevelPressure() uint32

GetDefaultSeaLevelPressure return average barometric pressure on the sea level defined as 101325 Pa.

func (*MPL3115A2) MeasureAltitude

func (v *MPL3115A2) MeasureAltitude(i2c *i2c.I2C, oversampleRatio int) (float32, float32, error)

MeasureAltitude measure altitude in meters with specific precision defined by oversample ratio.

func (*MPL3115A2) MeasurePressure

func (v *MPL3115A2) MeasurePressure(i2c *i2c.I2C, oversampleRation int) (float32, float32, error)

MeasurePressure measure pressure in Pa with specific precision defined by oversample ratio.

func (*MPL3115A2) ModifySeaLevelPressure

func (v *MPL3115A2) ModifySeaLevelPressure(i2c *i2c.I2C, pressureAtSeeLevel uint32) error

ModifySeaLevelPressure call allow to change default sea level value 101326 Pa to custom one.

func (*MPL3115A2) Reset

func (v *MPL3115A2) Reset(i2c *i2c.I2C) error

Reset reboot sensor and initialize some sensor registers.

type PressureType

type PressureType int

PressureType signify which type of pressure measurement in use.

const (
	// Measure pressure in Pa
	Barometer PressureType = iota + 1
	// Measure altitude in m
	Altimeter
)

type RawPressure

type RawPressure struct {
	PRES_MSB byte
	PRES_CSB byte
	PRES_LSB byte
}

RawPressure keeps raw pressure data received from sensor.

func (*RawPressure) ConvertToSignedQ16Dot4

func (v *RawPressure) ConvertToSignedQ16Dot4() (int16, uint8)

ConvertToSignedQ16Dot4 convert raw data to signed Q16.4, where integer and fraction parts returned in separate fields. Used for altimeter mode.

func (*RawPressure) ConvertToUnsignedQ18Dot2

func (v *RawPressure) ConvertToUnsignedQ18Dot2() (uint32, uint8)

ConvertToUnsignedQ18Dot2 convert raw data to unsigned Q18.2, where integer and fraction parts returned in separate fields. Used for barometer mode.

type RawTemperature

type RawTemperature struct {
	TEMP_MSB byte
	TEMP_LSB byte
}

RawTemperature keeps raw temperature data received from sensor.

func (*RawTemperature) ConvertToSignedQ8Dot4

func (v *RawTemperature) ConvertToSignedQ8Dot4() (int8, uint8)

ConvertToSignedQ8Dot4 convert raw data to signed Q8.4, where integer and fraction parts returned in separate fields.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL