Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BME 680 configuration on PIC32

    BME 680 configuration on PIC32

    Vilius
    New Poster

    Hello,

    I am an undergraduate university student developing my thesis project with PIC32. I would like to use BME 680 sensor with it. I hooked everything on a breadboard, wrote a working I2C driver  for the PIC32, I also incorporated 3.3 <-> 1.8 V logic translator between the PIC32 and BME 680. But I got stuck with the configuration of the sensor... I was following these instructions: https://github.com/boschsensortec/BME680_driver

    I have added both of the header files and the source file to my project, but still, I can not understand, how do I configure the sensor in the main program? The last section of the link that I provided gives a ,,Templates for function pointers" but I have already written the universal read and write functions for the I2C, so do I still have to write those again? (I though that the point of having a driver written for you is to avoid all those separate register reads and writes...) I may sound confusing, but I would really appreciate any help, explanations or observations. Could you give me an exact template for the main function, highlighting the variables or other parameters I have to change or write for myself? You could possibly save my project 🙂

    For now, I came up with the example code, but I am not sure, if I have to write any more functions, or change the parameters...

        #include <xc.h> 
        #include "configurations_bits.h"                                                                  //Including microcontroller`s configuration settings
               
        #include <stddef.h>                                                                               //Including all the standard C libraries
        #include <stdint.h>                    
        #include <stdbool.h>                    
        #include <stdlib.h> 
        #include "stdio.h" 
        #include <sys/attribs.h> 
     
        #include "delay.h"                                                                                //Including all the separate module header files
        #include "inter_integrated_circuit_protocol.h" 
        #include "bme680.h" 
    
                                                                                
        //---EXAMPLE PART ONE-------------------------------------------------------------
    
        struct bme680_dev gas_sensor {
    
            gas_sensor.dev_id = BME680_I2C_ADDR_PRIMARY;                            //SDO pin is grounded, so this address is correct in my case
            gas_sensor.intf = BME680_I2C_INTF;
            gas_sensor.read = Inter_Integrated_Circuit_Read();                      //I2C read function is defined in the I2C header and source files
            gas_sensor.write = Inter_Integrated_Circuit_Write();                    //I2C write function is defined in the I2C header and source files
            gas_sensor.delay_ms = delay_ms();                                       //delay function is defined in the separate header and source files
                                                                                    //SHOULD I ASSIGN THESE FUNCTIONS HERE?
            gas_sensor.amb_temp = 25;
        }
    
            int8_t rslt = BME680_OK;
            rslt = bme680_init(&gas_sensor);
            
            //---EXAMPLE PART ONE-------------------------------------------------------------
            
            
            
            int main(void) {                                                                              //Main program`s start
        
                Inter_Integrated_Circuit_Setup ();                                                        //Calling I2C configuration function
                Inter_Integrated_Circuit_Enable ();                                                       //Starting I2C module
    
               //---EXAMPLE PART TWO-------------------------------------------------------------
                
                uint8_t set_required_settings;
    
                    /* Set the temperature, pressure and humidity settings */
                    gas_sensor.tph_sett.os_hum = BME680_OS_2X;
                    gas_sensor.tph_sett.os_pres = BME680_OS_4X;
                    gas_sensor.tph_sett.os_temp = BME680_OS_8X;
                    gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3;
    
                    /* Set the remaining gas sensor settings and link the heating profile */
                    gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
                    /* Create a ramp heat waveform in 3 steps */
                    gas_sensor.gas_sett.heatr_temp = 320; /* degree Celsius */
                    gas_sensor.gas_sett.heatr_dur = 150; /* milliseconds */
    
                    /* Select the power mode */
                    /* Must be set before writing the sensor configuration */
                    gas_sensor.power_mode = BME680_FORCED_MODE; 
    
                    /* Set the required sensor settings needed */
                    set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL 
                        | BME680_GAS_SENSOR_SEL;
    
                    /* Set the desired sensor configuration */
                    rslt = bme680_set_sensor_settings(set_required_settings,&gas_sensor);
    
                    /* Set the power mode */
                    rslt = bme680_set_sensor_mode(&gas_sensor);
          
                //---EXAMPLE PART TWO-------------------------------------------------------------
                
                
                //---EXAMPLE PART THREE-------------------------------------------------------------     
                    
               uint16_t meas_period = 1000;    //Lets say 1000 millisecond? Is that the right value?
                    bme680_get_profile_dur(&meas_period, &gas_sensor);
    
                    struct bme680_field_data data;
    
                    while (true){                                                                         //Main program loop
                       
                       delay_ms(meas_period); /* Delay till the measurement is ready */
    
                            rslt = bme680_get_sensor_data(&data, &gas_sensor);
    
                            printf("T: %.2f degC, P: %.2f hPa, H %.2f %%rH ", data.temperature / 100.0f,
                                data.pressure / 100.0f, data.humidity / 1000.0f );
                            /* Avoid using measurements from an unstable heating setup */
                            if(data.status & BME680_GASM_VALID_MSK)
                                printf(", G: %d ohms", data.gas_resistance);
    
                            printf("\r\n");
    
                            /* Trigger the next measurement if you would like to read data out continuously */
                            if (gas_sensor.power_mode == BME680_FORCED_MODE) {
                                rslt = bme680_set_sensor_mode(&gas_sensor);
                            }
                            
                //---EXAMPLE PART THREE-------------------------------------------------------------     
                            
                            
                    }
                    
                return (EXIT_FAILURE);  
            }

     

    1 REPLY 1

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Vilius,

    You could refer BME68x driver code and example code from https://github.com/boschsensortec/BME68x-Sensor-API
    You don't need to rewrite driver code as it has been provided in the form of API, you only need to modify common.c according your host platform.

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist