Yesterday 21:07
Forum: Proton Plus Compiler v3
Starter: Henk57
Views: 0
Replies: 1
Go to last post By: Lester
Yesterday 20:59
Forum: Website / Forum Issues
Starter: Lester
Views: 0
Replies: 10
Yesterday 20:40
Forum: New devices and techniques
Starter: normnet
Views: 861
Replies: 2
+ Reply to Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 39
  1. #1
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    451
    Thumbs Up
    Received: 39
    Given: 17
    Total Downloaded
    1.39 GB

    5 Not allowed!

    Default BME280 Humidity/Pressure/Temperature code

    For those interested in this Bosch sensor, here's some Proton code to get you started.

    Code:
    '****************************************************************
    '*  Name    : BME280.BAS                                        *
    '*  Author  : Bob Marshall                                      *
    '*  Date    : 26/07/2016                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : This program uses a BME280 sensor to measure      *
    '*          : current pressure, temperature, humidity and       *
    '*          : altitude.                                         *
    '****************************************************************
    
    
      Include "Amicus18_mk2.inc"
    
      Declare Float_Display_Type = Fast
      Declare Hbus_Bitrate 400
    
      Symbol BME280 = $EC  'BME280 I2C address
      Symbol CalDatStAd = $88 'Start address for cal. data.
      Symbol HumCalDatAd = $E1 'Start address for MAIN humidity cal. data.
      Symbol BME280HumCtrlReg = $F2 'BME280 humidity control register
      Symbol BME280ContReg = $F4 'BME280 control Register
      Symbol BME280ConfigReg = $F5 'BME280 configuration register
      Symbol BME280DataReg = $F7 'Uncompensated Data start register
    
    'Values of 0-3 acceptable but see data sheet. 3 = Normal Mode
      Symbol PowerMode = 3
    
    'Values of 0-5 acceptable but see data sheet. 5 = 20 bit res.
      Symbol TempRes =  5
    
    'Values of 0-5 acceptable but see data sheet. 5 = 20 bit res.
      Symbol PresRes = 5
    
    'Values of 0-7 acceptable. See data sheet before changing.
      Symbol Filter = 4
    
    'Values of 0-7 acceptable but see data sheet. 4 = 500ms.
      Symbol Standby =  4
    
    'Values of 0-5 acceptable but see data sheet.
      Symbol BME280CtrlHum = 5
      
    'Temperature related variables.
      Dim T1 As Word
      Dim T2 As SWord
      Dim T3 As SWord
    
      Dim UT As Word  'Uncompensated temperature
      Dim XLSB_t As Byte
      Dim UTLong As SDword
      Dim Temperature As Float
      Dim T_fine As Float
    
    'Pressure related variables
      Dim P1 As Word
      Dim P2 As SWord
      Dim P3 As SWord
      Dim P4 As SWord
      Dim P5 As SWord
      Dim P6 As SWord
      Dim P7 As SWord
      Dim P8 As SWord
      Dim P9 As SWord
    
      Dim UP As Word 'Uncompensated pressure.
      Dim UPLong As SDword
      Dim XLSB_p As Byte
      Dim P As Float
    
    'Humidity related variables.
      Dim H1 As Byte
      Dim H2 As SWord
      Dim H3 As Byte
      Dim H4 As SWord
      Dim H5 As SWord
      Dim H6 As Byte
      Dim H4p1 As Byte      'H4 high bits
      Dim H4p2H5p2 As Byte  'H4 & H5 low bits
      Dim H5p1 As Byte      'H5 high bits
    
      Dim UH As Word   'Uncompensated Humidity
      Dim H As Float
    
    'BME280 related variables.
      Dim BME280Config As Byte
      Dim BME280Control As Byte
    
    'General variables
      Dim var1 As Float
      Dim var2 As Float
      Dim Altitude As Float
      Dim SeaLevel As Dword
      Main:
    
      DelayMS 500 'Let things settle down.
      Clear
    
      'Setup BME280 Control and configuration registers.
    
      
      HBusOut BME280,BME280HumCtrlReg,[BME280CtrlHum]
    
      BME280Config = (Standby <<5) + (Filter <<2) ' Combine configuration bits
      HBusOut BME280,BME280ConfigReg,[BME280Config]
    
      BME280Control = (TempRes <<5) + (PresRes <<2) + PowerMode ' Combine BME280Control bits.
      HBusOut BME280,BME280ContReg,[BME280Control]
    
    
    ' Read calibration data
      HBusIn BME280,CalDatStAd,[T1,T2,T3,P1,P2,P3,P4,P5,P6,P7,P8,P9]
      HBusIn BME280, $A1,[H1]'First byte of humdity comp. data held in $A1
      HBusIn BME280,HumCalDatAd,[H2,H3,H4p1,H4p2H5p2,H5p1,H6]
    
      H4 = (H4p1 << 4)|(H4p2H5p2 & $0F)
      H5 = (H5p1 << 4)|(H4p2H5p2 >> 4)
    
      Swap T1.Byte0,T1.Byte1 : Swap T2.Byte0,T2.Byte1 : Swap T3.Byte0,T3.Byte1
      Swap P1.Byte0,P1.Byte1 : Swap P2.Byte0,P2.Byte1 : Swap P3.Byte0,P3.Byte1
      Swap P4.Byte0,P4.Byte1 : Swap P5.Byte0,P5.Byte1 : Swap P6.Byte0,P6.Byte1
      Swap P7.Byte0,P7.Byte1 : Swap P8.Byte0,P8.Byte1 : Swap P9.Byte0,P9.Byte1
      Swap H2.Byte0,H2.Byte1
    
      While
    'Read the uncompensated temperature and pressure values.
          HBusIn BME280,BME280DataReg,[UP,XLSB_p,UT,XLSB_t,UH]
          UTLong = UT * 16 + (XLSB_t >> 4)
          UPLong = UP * 16 + (XLSB_p >> 4)
    
    ' Test data from the Bosch datasheet. Used to check calculations.
    '         T1=27504  : T2=26435  : T3=-1000
    '         P1=36477  : P2=-10685 : P3=3024
    '         P4=2855   : P5=140    : P6=-7
    '         P7=15500  : P8=-14600 : P9=6000
    '         UTlong=519888 : UPlong=415148
    ' Expected results: Temperature = 25.08, Pressure =  100656.26
    
          var1=(UTLong/16384-T1/1024)*T2
          var2=(UTLong/131072-T1/8192)*(UTLong/131072-T1/8192)*T3
          Temperature=(var1+var2)/5120
          T_fine=(var1+var2)
    
    
    ' $B0 is the hex value for the degree symbol. Not correctly displayed on some terminals.
          HSerOut ["Current temperature is: ",Dec2 Temperature,$B0,"C",13]
    
    ' Pressure calculations start here.
    
          var1=T_fine/2-64000
          var2=var1*var1*P6/32768
          var2=var2+var1*P5 * 2
          var2=(var2/4)+ P4 * 65536
          var1=(P3*var1*var1/524288+P2*var1)/524288
          var1=(1+var1/32768)*P1
          P=1048576-UPLong
          P=(P-var2/4096)*6250/var1
    
    ' The *-1 is needed here to negate the value of var1
    ' See p23 of the Bosch data sheet (example calculation).
          var1=(P9*P*P/2147483648)*-1
    
          var2=P*P8/32768
          P=P+(var1+var2+P7)/16
    
          HSerOut ["Current pressure: ",Dec2 P,"Pa",13]
    
    ' Note: The altitude calculation takes about 1.5k of program memory.
    '     : Remove if not needed!
    
    ' Altitude = 44330*((1-(p/101325)) ^ (1/5.255))
    ' 101325 represents the 'standard' pressure at sea level. Adjust to suit.
    
          SeaLevel = 101200 ' Air pressure in pascals at sea level
          var1 = pow((P/SeaLevel),0.190295)
          Altitude =  44330 *(1-var1)
          HSerOut ["Current altitude: ",Dec2 Altitude ,"m",13]
    
    'Humidity Calculations.
          H = T_fine - 76800.0
          H  = (UH - (H4 * 64.0 + H5 / 16384.8 * H)) * (H2 / 65536.0 * (1.0 + H6 / 67108864.0 * H * (1.0 + H3 / 67108864.0 * H)))
          H = H * (1.0 - H1 * H / 524288.0)
          If H > 100 Then
              H = 100
          Else If H < 0 Then
              H = 0
          EndIf
    
          HSerOut ["Current humidity: ",Dec2 H ,"%",13,10,13]
    
          DelayMS 2000
    
      Wend
    
    ' End of program
    Have fun.

    Bob

  2. #2
    Prolific Poster rcurl's Avatar
    Join Date
    Mar 2002
    Posts
    1,908
    Thumbs Up
    Received: 26
    Given: 36
    Total Downloaded
    1.20 GB

    0 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    Nice one, Bob! I've got a BME20 spec'ed in for a future project. This will come in handy.

    Thanks for sharing.

    -Rick

  3. #3
    Member Henk57's Avatar
    Join Date
    Jan 2014
    Posts
    208
    Thumbs Up
    Received: 10
    Given: 18
    Total Downloaded
    192.60 MB

    0 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    I second, thank you Bob.

    I have used an internal humidity sensor in my sprouter, if you might know, but I need an average value.
    So an extra sensor or calculate values after some decades of minutes, could be the answer, but I'm not sure about this.

    My point is, I need 3-points measure, first a minimum value an average value and a maximum value, reason of this, if the maximum value is reached then the fan's must be switched on, and if the minimum value is reached the fan's must be switched of.

    So I must find a correct calculation routine.

    Some background;
    The average value (time depending) for humidity is needed to get the spread, let's say the value = 2, then if the average is 60% the min. value should be 58% and the max. should be 62% which are then the switch point between On/Off.

    Maybe I need this Sensor which has also the Pressure readings to predict the rainy days.
    For I forget, Thumbs Up!!

  4. #4
    Member Henk57's Avatar
    Join Date
    Jan 2014
    Posts
    208
    Thumbs Up
    Received: 10
    Given: 18
    Total Downloaded
    192.60 MB

    0 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    Yesterday, I become my BME280 (Adafruit), next week I 'll try it out.
    Humidity calcs. are in Beta and working, but can still be aproved (depends on testing, learning and result).

    Henk

  5. #5
    Prolific Poster towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    1,360
    Thumbs Up
    Received: 49
    Given: 124
    Total Downloaded
    2.37 GB

    0 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    Nice code, thanks.
    George

  6. #6
    Junior Member superdat's Avatar
    Join Date
    Nov 2017
    Posts
    9
    Thumbs Up
    Received: 2
    Given: 0
    Total Downloaded
    0

    0 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    Hi
    I've been trying to convert this to SPi. Everything works with one major problem I don't get P,T&H data back.
    The data analyser shows 0x00,0x00,0x80.0x00,0x00,0x80.0x00,0x00,0x80 clearly NOT real data.
    I've tried various permutations of SPI format.
    It's not the BME280 because I can write/read everything with GCB and Oshonsoft Compilers. GCB = HW, Oshonsoft = SW
    Both of these compilers have limited choices for variables, thats why I'm now trying Proton.
    I can write to registers OK, read calibration data OK, I've tried Forced and Normal modes, no change.
    In fact everything I've done makes no difference. The same repeating pattern of 128 (decimal)
    Ideas?

  7. #7
    Prolific Poster towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    1,360
    Thumbs Up
    Received: 49
    Given: 124
    Total Downloaded
    2.37 GB

    0 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    @sperdat Why don't you want to run I2C?
    George

  8. #8
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    451
    Thumbs Up
    Received: 39
    Given: 17
    Total Downloaded
    1.39 GB

    0 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    Can you post your code please?

  9. #9
    Junior Member superdat's Avatar
    Join Date
    Nov 2017
    Posts
    9
    Thumbs Up
    Received: 2
    Given: 0
    Total Downloaded
    0

    1 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    I've never used I2C + My BME280 is an SPI only version.
    I use Max7219 quite often hence my preference for SPI

    Code attached. n.b. I've tried all manner of permutations of Shout and Shin for the PT&H data.
    The weird thing is everthing works fine for all other registers but not the sensor data.
    Attached Files Attached Files

  10. #10
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    451
    Thumbs Up
    Received: 39
    Given: 17
    Total Downloaded
    1.39 GB

    0 Not allowed!

    Default Re: BME280 Humidity/Pressure/Temperature code

    Hi superdat,

    I've just been looking at the BME280 datasheet. It would seem that you may be able to use your BME280 with I2C. SDI becomes SDA (I2C) and SCK becomes SCL (I2C). Depending on your board, you may need a couple of 4.7k (ish) pull-up resistors on SDA and SCL. CS remains unconnected.


    If I get the chance I'll have a play to see if I can get my program going with SPI.

    Bob

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 2
    Last Post: 6th December 2014, 15:58
  2. Replies: 2
    Last Post: 6th December 2014, 15:58
  3. [SOLVED !] Temperature & Humidity Data Recorder Project
    By oldspring in forum The Lounge
    Replies: 0
    Last Post: 9th July 2008, 18:02
  4. I2C Sensors - Humidity and Barometric Pressure
    By picnaut in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 1st November 2007, 22:57

Members who have read this thread : 55

Actions :  (Set Date)

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

Tags for this Thread

Posting Permissions

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