BME280 sensor.


Closed Thread
Results 1 to 20 of 20

Thread: BME280 sensor.

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516

    Default BME280 sensor.

    Has anyone been able to interface the BME280 temp, humidity and pressure sensor to PBP?
    I've been messing with it for two days. Getting temperature to read correctly (at least I think it is) took a couple of hours but the compensation formulas for the pressure, let alone humidity is about as convoluted as they get and I'm really struggling. They do provide code in the datasheet but no real explaination or formulas.

    Take a look at the fixed point version of the compensation code in the datasheet and tell me if you can figure out what it's doing :-) Like I said, I managed to get temperature working (at least it seems to be, don't know what happens when temp goes negative...) but just look at the code for pressure and humidity, they're using bloody 64bit signed variables, leftshift 47 times, rightshift 33 times...jeeez :-)

    Of course there is Arduino code available but it pretty much copies the datasheet.

    Anyone already done it? Anyone interested?

    /Henrik.

    EDIT:Link to datasheet

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: BME280 sensor.

    Yer, the compensation is horrible. Sorry don't have PICBASIC code but do have working code from the dark side.
    George

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: BME280 sensor.

    i posted this some time ago and the topic seems to have disappeared , i can't remember who's post it was and the context
    but the calcs work but for the ones that need 64bit vars

    Code:
    ' DEVICE CONFIGURATION
    
    #CONFIG
        CONFIG  OSC = IRCIO
        CONFIG  FCMEN = OFF
        CONFIG  IESO = OFF
        CONFIG  PWRTEN = OFF
        CONFIG  BOREN = OFF
        CONFIG  BORV = 20
        CONFIG  WDTEN = OFF
        CONFIG  WDPS = 512
        CONFIG  WINEN = OFF
        CONFIG  PWMPIN = OFF
        CONFIG  LPOL = HIGH           ; PWM0, 2, 4 and 6 are active-high
        CONFIG  HPOL = HIGH           ; PWM1, 3, 5 and 7 are active-high
        CONFIG  T1OSCMX = ON          ; Low-power Timer1 operation when microcontroller is in Sleep mode
        CONFIG  MCLRE = OFF
        CONFIG  STVREN = ON           ; Stack full/underflow will cause Reset
        CONFIG  LVP = OFF             ; Low-voltage ICSP disabled
        CONFIG  DEBUG = OFF           ; Background debugger disabled; RB6 and RB7 configured as general purpose I/O pins
        CONFIG  CP0 = OFF             ; Block 0 (000200-000FFFh) not code-protected
        CONFIG  CP1 = OFF             ; Block 1 (001000-001FFF) not code-protected
        CONFIG  CP2 = OFF             ; Block 2 (002000-002FFFh) not code-protected
        CONFIG  CP3 = OFF             ; Block 3 (003000-003FFFh) not code-protected
        CONFIG  CPB = OFF             ; Boot Block (000000-0001FFh) not code-protected
        CONFIG  CPD = OFF             ; Data EEPROM not code-protected
        CONFIG  WRT0 = OFF            ; Block 0 (000200-000FFFh) not write-protected
        CONFIG  WRT1 = OFF            ; Block 1 (001000-001FFF) not write-protected
        CONFIG  WRT2 = OFF            ; Block 2 (002000-002FFFh) not write-protected
        CONFIG  WRT3 = OFF            ; Block 3 (003000-003FFFh) not write-protected
        CONFIG  WRTC = OFF            ; Configuration registers (300000-3000FFh) not write-protected
        CONFIG  WRTB = OFF            ; Boot Block (000000-0001FFh) not write-protected
        CONFIG  WRTD = OFF            ; Data EEPROM not write-protected
        CONFIG  EBTR0 = OFF           ; Block 0 (000200-000FFFh) not protected from table reads executed in other blocks
        CONFIG  EBTR1 = OFF           ; Block 1 (001000-001FFF) not protected from table reads executed in other blocks
        CONFIG  EBTR2 = OFF           ; Block 2 (002000-002FFFh) not protected from table reads executed in other blocks
        CONFIG  EBTR3 = OFF           ; Block 3 (003000-003FFFh) not protected from table reads executed in other blocks
        CONFIG  EBTRB = OFF           ; Boot Block (000000-0001FFh) not protected from table reads executed in other blocks
    #ENDCONFIG
    
    ' DEFINEs
        DEFINE    OSC    8                            ' Internal 8MHz
    
    
    ' Pin ALIASES
        LED1            VAR    PORTB.0                    ' Orange Lights
        GRNLED            VAR    PORTB.4                    '
        ClockPin        VAR    PORTA.6                    ' IIC BME280
        DataPin            VAR    PORTA.7                    ' IIC BME280
    
      
    ' CONSTANTS
    
        ' --- BME280 Registers---
        BMEADDR            CON    $EC                    ' Chip Address
        ID            CON    $D0                    ' Chip ID (0x60)
        ctrl_hum        CON    $F2                    ' Humidity Control Register
        stat            CON    $F3                    ' Status Register
        ctrl_meas        CON    $F4                    ' Temperature/Pressure Control Register
        config            CON    $F5                    ' Cofiguration
        cal_st_addr        CON    $88                    ' Calibration Start Address
    
    
    ' VARIABLES
        FLAG            VAR    BYTE
        IIC_RX            VAR    BYTE[8]                    ' IIC Received Data BME280
       i var byte
        T1            VAR    LONG
        T2            VAR    LONG
        T3            VAR    LONG
        adc_T            VAR    LONG
        var1            VAR    LONG
        var2            VAR    LONG
        T            VAR    LONG
        adc_Tr3            VAR    LONG
    '    T1l1            VAR    LONG
    '    T2r11            VAR    LONG
    '    adc_Tr4            VAR    LONG
    '    T1r12            VAR    LONG
    '    T3r14            VAR    LONG
        t_fine            VAR    LONG
    '    TT            VAR    LONG
    
    
    ' INITIALIZE REGISTERS
        OSCCON = %01110000                            ' Set internal osc to 8MHZ
        ANSEL0 = %00000000                            ' Set to Digital PortA
                                            ' -HW RS232 Setup-
    
        
    TXSTA = $20   ' Enable transmit, BRGH = 0
    SPBRG = 51    ' 9600 Baud @ 8MHz, 0.16%
    SPBRGH = 0
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    
        
        
        
                                            ' -HW Timer Setup-
        TMR0H = 0                                ' HighByte of preload value
            TMR0L = 0                                ' LowByte of preload value (the order is important!)
            INTCON.2 = 0                                                ' Clear the flag bit
        T0CON = %10000111                            ' Setup and Enable TMR0 (8.388s)
        
    
        TRISA = %00000000                            ' Set ports as outputs (2-4 I/P)
        TRISB = %00000000
        TRISC = %00000000                            ' Set ports as outputs
    
    CLEAR
    ' PROGRAM Start
    
        PORTA = 0                                ' Clear PORTA
        PORTB = 0
        PORTC = 0
    
        IIC_RX = 0
    Init:
        GOSUB P500                                ' Let everything settle
        GOSUB VER_BME                                ' Is the BME280 Connected. Verify
    
    
    MAIN:
        GOSUB GETTEMPBME                            ' Get the temperature reading from BME280
        GOSUB P1
    
        GOTO MAIN
                                            ' Go back to mainloop
    END
    
    
    GETTEMPBME: 
        'I2CREAD DataPin, ClockPin, BMEADDR, stat ,[IIC_RX]            ' Check to see if busy Converting Measurements, or writing data        
        'If IIC_RX[0] != 0 THEN                         ' If not, it should read as 0
        '    GOSUB P100
        '    GOSUB GETTEMPBME
        'ENDIF
    
    '    DO
    '        IF FLAG.0 = 1 THEN GOSUB P100                    ' If flag is set the BME280 was not ready (Try again)
    '        TOGGLE LED1
    '        I2CREAD DataPin, ClockPin, BMEADDR, stat ,[IIC_RX]        ' Check to see if busy Converting Measurements, or writing data    
    '        FLAG.0 = 1
    '    LOOP WHILE IIC_RX[0] != 0                        ' 
    '    FLAG.0 = 0                                ' Clear Flag
    
    '    I2CREAD DataPin, ClockPin, BMEADDR, $F7 ,[STR IIC_RX\8]            ' Read Temp/Hum/Press registers
    arraywrite  IIC_RX,[ $55 ,$2F ,$D0 ,$82 ,$D7 ,$90 ,$85 ,$A6 ]
        for i=0 to  6
        HSEROUT[hex2  IIC_RX[i],","]                    
    next
        HSEROUT[hex2  IIC_RX[i],13,10]
    '    GOSUB P1
    
        
        adc_T.Byte0 = IIC_RX[5]                            ' Store Raw Temerature data
        adc_T.Byte1 = IIC_RX[4]
        adc_T.Byte2 = IIC_RX[3]
    '    GOSUB P1
    
    
        
        adc_T=adc_T >> 4   
        var1 = ((((adc_T >> 3) - (T1 << 1))) * (T2)) >> 11
        var2 = (((((adc_T >> 4) - (T1)) *((adc_T >> 4) - (T1))) >>12) *(T3)) >>14
        t_fine = var1 + var2
        T = ((t_fine * 5) + 128)>>8
        HSEROUT["T " ,#T/100,".",#T//100,13,10]
       
    
    
        
    
    
        RETURN
    
    VER_BME:
    '    I2CREAD DataPin, ClockPin, BMEADDR, ID ,[IIC_RX]            ' Read the BME's ID
    '    IF IIC_RX[0] != $60 THEN ERROR2                        ' Cannot Connect to the BME280
    
    '                                        ' -Read Calibration Data for Temperature-
    '    I2CREAD DataPin, ClockPin, BMEADDR, cal_st_addr ,[STR IIC_RX\6]        '
    arraywrite  IIC_RX,[$6C ,$6E ,$77 ,$66 ,$32 ,$00] 
    for i=0 to  4
        HSEROUT[hex2  IIC_RX[i],","]                        ' Display -> 6C 6E 77 66 32 00 0D 0A
    next
        HSEROUT[hex2  IIC_RX[i],13,10]
        
        T1.Byte0 = IIC_RX[0]                            ' Calibration data into Unsigned Word
           T1.Byte1 = IIC_RX[1]
        T2.Byte0 = IIC_RX[2]                            ' Calibration data into Signed Long
           T2.Byte1 = IIC_RX[3]
        T3.Byte0 = IIC_RX[4]                            ' Calibration data into Signed Long
           T3.Byte1 = IIC_RX[5]
    
    
                                            ' -Initialise the Sensor-    
    '    I2CWRITE DataPin, ClockPin, BMEADDR, config ,[%10101000]        ' Config Setting (1000ms Standby, IIR Filter @ 4)
    '    I2CWRITE DataPin, ClockPin, BMEADDR, ctrl_hum ,[%00000010]        ' Humidity Setting (x2 Oversampling)
    '    I2CWRITE DataPin, ClockPin, BMEADDR, ctrl_meas ,[%01001011]        ' Temp/Pres Settings (x2 Oversampling, Normal Mode)
    
    '    GOSUB P250
        RETURN
    
    ERROR1:
        DO
            TOGGLE LED1                            ' Flash the Orange LED every 1/2 second
            GOSUB P500
        LOOP
    
    ERROR2:                                        ' BME280 Connection Error
        LED1 = 1
        GOSUB P250
        LED1 = 0
        GOSUB P250
        LED1 = 1
        GOSUB P250
        LED1 = 0
        GOSUB P1
        GOTO ERROR2
    
    P100:
        PAUSE 100                                ' Just a pause..
        RETURN
    
    P250:
        PAUSE 250                                ' Just a pause..
        RETURN
    
    P500:
        PAUSE 500                                ' Just a pause..
        RETURN
    
    P1:
        PAUSE 1000                                ' Just a pause..
        RETURN
    Warning I'm not a teacher

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: BME280 sensor.

    I wonder what was thinking the team that designed that sensor...

    Anyway maybe this can be of some help
    http://dt.picbasic.co.uk/CODEX/N-BitMath

    Ioannis

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: BME280 sensor.

    Thank you Richard!
    I see you too got the temperature conversion covered but not pressure and/or humidity which is where I'm currently got stuck. Haven't done anything with it for a couple of days, perhaps I'll get back to it - or I won't.

    Ioannis,
    Yes, I know about the N-bit routines and if I don't succeed in getting the "native" 32bit fixed point routines they do show in the datasheet to work I might try with N-bit but I suspect it won't actually make it much easier.

    Right now, my guess is that the problem has to do with me niot be able to "translate" their mix of signed and unsigned numbers of various bit lengths into PBP lingo.
    I'm reading the compensation values from the sensor and "manually" casts them into LONGs in order to make them signed. But then not all of them are supposed to BE signed but in PBP all longs ARE signed and it becomes a mess quite quickly. It would be nice to have set of input values with a known output so one could split the calculation in steps and figure out where it goes wrong.

    /Henrik.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: BME280 sensor.

    Does anyone have a BME280 sensor laying around?
    Can you read the compensation values and post them?

    I think I've now got pressure working as well but I'm curious to know another sensors comp values :-)

    /Henrik.

Similar Threads

  1. MQ-7 Sensor
    By chipmaker in forum General
    Replies: 3
    Last Post: - 9th June 2017, 01:05
  2. Speed Sensor Anyone???
    By Ramius in forum General
    Replies: 19
    Last Post: - 18th May 2012, 05:37
  3. Pir Sensor
    By azmax100 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 20th May 2009, 17:34
  4. Accelerometer Sensor
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 27th August 2008, 14:26
  5. Replies: 6
    Last Post: - 18th January 2008, 09:17

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts