PlatformIO Community

BME680 Zephyr example not working with the nRF52840-DK

I am keep trying to get the BME680 example to work with the nRF52840-DK.

I check and successfully find the node for the BME680, but “device_get_binding()” for the BME680 device returns NULL

What do I need to do to get the BME680 to work with the nRF52840-DK via I2C?

I get the following output via the serial monitor.

Hello! I'm running Zephyr 2.3.0 on nrf52840dk_nrf52840, a arm board.

No device "BME680" found; did initialization fail?

Following is are my files.

nrf52840dk_nrf52840.overlay

&i2c0 {
    	bme680@76 {
    		compatible = "bosch,bme680";
    		reg = <0x76>;
    		label = "BME680";
    	};
    };

prj.conf

CONFIG_PRINTK=y
CONFIG_STDOUT_CONSOLE=y
CONFIG_PWM=y
CONFIG_LOG=y
CONFIG_PWM_LOG_LEVEL_DBG=y
CONFIG_I2C=y
CONFIG_BME680=y

main.c

#include <zephyr.h>
#include <device.h>
#include <devicetree.h>

#include <drivers/gpio.h>
#include <sys/printk.h>

#include <sys/util.h>
#include <version.h>
#include <stdio.h>
#include <drivers/sensor.h>



// The devicetree node identifier for the "led0" alias. 
#define LED0_NODE   DT_ALIAS(led0)

#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
#define LED0	    DT_GPIO_LABEL(LED0_NODE, gpios)
#define PIN	        DT_GPIO_PIN(LED0_NODE, gpios)
#if DT_PHA_HAS_CELL(LED0_NODE, gpios, flags)
#define FLAGS	    DT_GPIO_FLAGS(LED0_NODE, gpios)
#endif
#else
// A build error here means your board isn't set up to blink an LED. 
#error "Unsupported board: led0 devicetree alias is not defined"
#define LED0	""
#define PIN	0
#endif

#ifndef FLAGS
#define FLAGS	0
#endif




#define BME680          DT_INST(0, bosch_bme680)

#if DT_NODE_HAS_STATUS(BME680, okay)
#define BME680_LABEL    DT_LABEL(BME680)
#else
#error "Your device tree has no enabled nodes with compatible "bosch,bme680""
#define BME680_LABEL    "<none>"
#endif



void startDelayAndVersion(void) 
{
    /// Sleep for 1 second to make sure all messages get output to the serial monitor.
    k_sleep(K_SECONDS(1));
    printk("Hello! I'm running Zephyr %s on %s, a %s board.\n\n ", KERNEL_VERSION_STRING, CONFIG_BOARD, CONFIG_ARCH);
};



void main(void)
{
    startDelayAndVersion();

	struct device   *blueLedDevice;
    bool            led_is_on = true;
	int             returnValue = 0;

	blueLedDevice = device_get_binding(LED0);
	if (blueLedDevice == NULL) {
		return;
	}

	returnValue = gpio_pin_configure(blueLedDevice, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
	if (returnValue < 0) {
		return;
	}

    struct device *bme680Device = device_get_binding(BME680_LABEL);
	struct sensor_value temp, press, humidity, gas_res;

    if (bme680Device == NULL) {
		printk("No device \"%s\" found; did initialization fail?\n", BME680_LABEL);
		return;
	} else {
		printk("Found device \"%s\"\n", BME680_LABEL);
	}

	printf("Device %p name is %s\n", bme680Device, bme680Device->name);

	while (1) {
        /// Blink blue LED.
		gpio_pin_set(blueLedDevice, PIN, (int)led_is_on);
        led_is_on = !led_is_on;

        k_sleep(K_MSEC(3000));

        sensor_sample_fetch(bme680Device);
		sensor_channel_get(bme680Device, SENSOR_CHAN_AMBIENT_TEMP, &temp);
		sensor_channel_get(bme680Device, SENSOR_CHAN_PRESS, &press);
		sensor_channel_get(bme680Device, SENSOR_CHAN_HUMIDITY, &humidity);
		sensor_channel_get(bme680Device, SENSOR_CHAN_GAS_RES, &gas_res);

		printf("T: %d.%06d; P: %d.%06d; H: %d.%06d; G: %d.%06d\n",
				temp.val1, temp.val2, press.val1, press.val2,
				humidity.val1, humidity.val2, gas_res.val1,
				gas_res.val2);    
	}
}

According to this it works over I2C. Can you start a seperate project with Arduino as the firmware and upload the I2C scanner sketch on it to see if the device is seen at all?

The sample you mentioned is the one I am using. I keep getting an NRFX_ERROR_INTERNAL error.
" i2c_nrfx_twim: Error 195887105 occurred" no matter what I do.

I tried the Arduino I2C scanner, and though I didn’t get the serial port to work, I did see the address of the BME680 was found if I stepped through with debugging. So I know the hardware is functional.

I think I have some problem with my Zephyr setup but haven’t been able to find it.

edit: This error was with my custom board not the nRF52840-DK. Now posting results from the nRF52840-DK.

Is 0xbad0001 and as seen in e.g. https://devzone.nordicsemi.com/f/nordic-q-a/62585/i2c_nrfx_twim-error-195952641 this error code means

…which isn’t really helpfull beoyond stating “Internal error”.

I suggest you try using a native Zephyr environment without PlatformIO and see if it also occurs with version 2.3.0 (or latest) and the same project configuration in code. If it does, the people at Zephyr can help you.

With the Arduino I2C scanner, on the nRF52840-DK. I see the address of the BME680 was found if I stepped through with debugging. So I know the hardware is functional.

Using the example code I posted above, on the nRF52840-DK, I see the BME680 node. However, when I when I run “device_get_binding(BME680_LABEL)” the device is NULL.

I have also successfully run the Nordic twi scanner example in the nRF5 SDK peripheral folder “…\nRF5_SDK_17.0.0_9d13099\examples\peripheral\twi_scanner” as well.

Has anyone successfully used the nRF52840-DK and Zephyr with the BME680 sample, or the nRF52840-DK and Zephyr with any I2C sensors?

I figured it out. Somehow I didn’t include CONFIG_SENSOR=y in my proj.conf file. Now the example works