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 Reply Children
No Data
Related