This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BME680 Zephyr example not working with the nRF52840-DK

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

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@77 {
		compatible = "bosch,bme680";
		reg = <0x77>;
		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);    
	}
}

Parents
  • Hi,

    nrf52840dk_nrf52840.overlay

    Try to add this to i2c0 as well:

    	compatible = "nordic,nrf-twim";
    	status = "okay";
    	sda-pin = <11>;
    	scl-pin = <12>;
    	clock-frequency = <I2C_BITRATE_FAST>;

    Similar to how it's done for the Thingy:91 here. If you get the same error code after adding that, try to use bme680@76 and reg = <0x76>; instead of 77.

  • I tried you addition to overlay and looked at the Thingy:91 example, 

    I see the BME680 node via the following

    #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

    However, when I when I run “device_get_binding(BME680_LABEL)” the device is NULL.

Reply
  • I tried you addition to overlay and looked at the Thingy:91 example, 

    I see the BME680 node via the following

    #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

    However, when I when I run “device_get_binding(BME680_LABEL)” the device is NULL.

Children
  • I assume that DT_NODE_HAS_STATUS(BME680, okay) evalute to true ?

    What pins are you using for sda-pin and scl-pin ? 

    What voltage are you powering the sensor with ? 3.0 V?

  • Correct, DT_NODE_HAS_STATUS(BME680, okay) evalutes to true.

    I am using the pins for i2c0 that are given in the nrf52840dk_nrf52840.dts device tree. They are 

    arduino_i2c: &i2c0 {
        compatible = "nordic,nrf-twi";
        status = "okay";
        sda-pin = <26>;
        scl-pin = <27>;
    };
    I am connecting them to P0.26 and P0.27 on the nRF52840-DK.
    I am powering the sensor with 3.0V.
    I have also tried the scanner example in the nRF5 SDK peripheral folder?

    …\nRF5_SDK_17.0.0_9d13099\examples\peripheral\twi_scanner

    The twi_scanner with SES and it sees the device at 0x77, giving the following output.

    app: TWI scanner started.
    app: TWI device detected at address 0x77.

    This verifies the HW and wiring.

Related