Home Code Raspberry Pi and LPS22HB absolute pressure sensor example

Raspberry Pi and LPS22HB absolute pressure sensor example

by shedboy71

In this article we look at another absolute pressure sensor – this time its the LPS22HB

The LPS22HB is an ultra-compact piezoresistive absolute pressure sensor which functions as a digital output barometer. The device comprises a sensing element and an IC interface which communicates through I2C or SPI from the sensing element to the application.

The sensing element, which detects absolute pressure, consists of a suspended membrane manufactured using a dedicated process developed by ST.
The LPS22HB is available in a full-mold, holed LGA package (HLGA). It is guaranteed to operate over a temperature range extending from -40 °C to +85 °C. The package is holed to allow external pressure to reach the sensing element.

Features

  • 260 to 1260 hPa absolute pressure range
  • Current consumption down to 3 μA
  • High overpressure capability: 20x full-scale
  • Embedded temperature compensation
  • 24-bit pressure data output
  • 16-bit temperature data output
  • ODR from 1 Hz to 75 Hz
  • SPI and I²C interfaces
  • Embedded FIFO
  • Interrupt functions: Data Ready, FIFO flags, pressure thresholds
  • Supply voltage: 1.7 to 3.6 V
  • High shock survivability: 22,000 g

 

Parts Required

 

Name Link
Raspberry Pi Raspberry Pi 4 Model B Development Board
LPS22HB Semoic Lps22Hb Pressure Resistance Pressure Sensor Module for High Intensity Industrial Control
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

Raspberry Pi Sensor
3.3v 3.3v
Gnd Gnd
SDA SDA
SCL SCL

 

Code Example

I copied the following code into the thonny ide and ran it

#!/usr/bin/python
# -*- coding:utf-8 -*-
import time
import smbus
#i2c address
LPS22HB_I2C_ADDRESS   =  0x5D
#
LPS_ID                =  0xB1
#Register 
LPS_INT_CFG           =  0x0B        #Interrupt register
LPS_THS_P_L           =  0x0C        #Pressure threshold registers 
LPS_THS_P_H           =  0x0D        
LPS_WHO_AM_I          =  0x0F        #Who am I        
LPS_CTRL_REG1         =  0x10        #Control registers
LPS_CTRL_REG2         =  0x11
LPS_CTRL_REG3         =  0x12
LPS_FIFO_CTRL         =  0x14        #FIFO configuration register 
LPS_REF_P_XL          =  0x15        #Reference pressure registers
LPS_REF_P_L           =  0x16
LPS_REF_P_H           =  0x17
LPS_RPDS_L            =  0x18        #Pressure offset registers
LPS_RPDS_H            =  0x19        
LPS_RES_CONF          =  0x1A        #Resolution register
LPS_INT_SOURCE        =  0x25        #Interrupt register
LPS_FIFO_STATUS       =  0x26        #FIFO status register
LPS_STATUS            =  0x27        #Status register
LPS_PRESS_OUT_XL      =  0x28        #Pressure output registers
LPS_PRESS_OUT_L       =  0x29
LPS_PRESS_OUT_H       =  0x2A
LPS_TEMP_OUT_L        =  0x2B        #Temperature output registers
LPS_TEMP_OUT_H        =  0x2C
LPS_RES               =  0x33        #Filter reset register
 
class LPS22HB(object):
    def __init__(self,address=LPS22HB_I2C_ADDRESS):
        self._address = address
        self._bus = smbus.SMBus(1)
        self.LPS22HB_RESET()                         #Wait for reset to complete
        self._write_byte(LPS_CTRL_REG1 ,0x02)        #Low-pass filter disabled , output registers not updated until MSB and LSB have been read , Enable Block Data Update , Set Output Data Rate to 0 
    def LPS22HB_RESET(self):
        Buf=self._read_u16(LPS_CTRL_REG2)
        Buf|=0x04                                         
        self._write_byte(LPS_CTRL_REG2,Buf)               #SWRESET Set 1
        while Buf:
            Buf=self._read_u16(LPS_CTRL_REG2)
            Buf&=0x04
    def LPS22HB_START_ONESHOT(self):
        Buf=self._read_u16(LPS_CTRL_REG2)
        Buf|=0x01                                         #ONE_SHOT Set 1
        self._write_byte(LPS_CTRL_REG2,Buf)
    def _read_byte(self,cmd):
        return self._bus.read_byte_data(self._address,cmd)
    def _read_u16(self,cmd):
        LSB = self._bus.read_byte_data(self._address,cmd)
        MSB = self._bus.read_byte_data(self._address,cmd+1)
        return (MSB << 8) + LSB
    def _write_byte(self,cmd,val):
        self._bus.write_byte_data(self._address,cmd,val)
 
if __name__ == '__main__':
    PRESS_DATA = 0.0
    TEMP_DATA = 0.0
    u8Buf=[0,0,0]
    print("\nPressure Sensor Test Program ...\n")
    lps22hb=LPS22HB()
    while True:
        time.sleep(0.1)
        lps22hb.LPS22HB_START_ONESHOT()
        if (lps22hb._read_byte(LPS_STATUS)&0x01)==0x01:  # a new pressure data is generated
            u8Buf[0]=lps22hb._read_byte(LPS_PRESS_OUT_XL)
            u8Buf[1]=lps22hb._read_byte(LPS_PRESS_OUT_L)
            u8Buf[2]=lps22hb._read_byte(LPS_PRESS_OUT_H)
            PRESS_DATA=((u8Buf[2]<<16)+(u8Buf[1]<<8)+u8Buf[0])/4096.0
        if (lps22hb._read_byte(LPS_STATUS)&0x02)==0x02:   # a new pressure data is generated
            u8Buf[0]=lps22hb._read_byte(LPS_TEMP_OUT_L)
            u8Buf[1]=lps22hb._read_byte(LPS_TEMP_OUT_H)
            TEMP_DATA=((u8Buf[1]<<8)+u8Buf[0])/100.0
        print('Pressure = %6.2f hPa , Temperature = %6.2f °C\r\n'%(PRESS_DATA,TEMP_DATA))

Output

In the shell window I saw the following

Pressure = 953.48 hPa , Temperature = 17.19 °C

Pressure = 945.53 hPa , Temperature = 17.19 °C

Pressure = 950.85 hPa , Temperature = 22.05 °C

 

Links

https://www.st.com/resource/en/datasheet/lps22hb.pdf

 

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More