Mahmood ul Hassan
Published © GPL3+

How to read Nordic Thingy:53 onboard BH1749 light sensor

Nordic thingy:53 has bunch of onboard sensors and BH1749 light sensor is one of them. Here we will learn how to access and read its values.

IntermediateProtip30 minutes473
How to read Nordic Thingy:53 onboard BH1749 light sensor

Things used in this project

Story

Read more

Schematics

thingy:53 schematic available on Nordic semiconductor website

Code

thingy53_nrf5340_cpuapp_ns.overlay

C/C++
Create overlay file in order to use onboard bh1749 sensor
// To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.

// You can also use the buttons in the sidebar to perform actions on nodes.
// Actions currently available include:

// * Enabling / disabling the node
// * Adding the bus to a bus
// * Removing the node
// * Connecting ADC channels

// For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
// You can also visit the nRF DeviceTree extension documentation at https://nrfconnect.github.io/vscode-nrf-connect/devicetree/nrfdevicetree.html


&i2c1 {
	compatible = "nordic,nrf-twim";
	status = "okay";
	clock-frequency = <I2C_BITRATE_STANDARD>;

	pinctrl-0 = <&i2c1_default>;
	pinctrl-1 = <&i2c1_sleep>;
	pinctrl-names = "default", "sleep";

  bh1749: bh1749@38 {
		compatible = "rohm,bh1749";
        label = "BH1749";
		reg = <0x38>;
		int-gpios = <&gpio1 5 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
	};
};

main.c

C/C++
/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/zephyr.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/i2c.h>

#define BH1749_SYSTEM_CONTROL                           0x40
#define BH1749_MODE_CONTROL1                            0x41
#define BH1749_MODE_CONTROL2                            0x42
#define BH1749_RED_DATA_LSB                             0x50
#define BH1749_IR_DATA_LSB                             	0x58

#define BH1749_MODE_CONTROL2_RGB_EN_ENABLE              BIT(4)
#define BH1749_MODE_CONTROL1_DEFAULTS                   0x2A

#define BH_NODE DT_NODELABEL(bh1749)
static const struct i2c_dt_spec bh_i2c = I2C_DT_SPEC_GET(BH_NODE);

uint8_t init_bh1749(void)
{
	int ret;

	if (!device_is_ready(bh_i2c.bus)) {
		printk("I2C bus %s is not ready!\n\r",bh_i2c.bus->name);
		return 0;
	}
	printk("device is %p, name is %s\n", bh_i2c.bus, bh_i2c.bus->name); 

	char buff1[] = {BH1749_MODE_CONTROL1,BH1749_MODE_CONTROL1_DEFAULTS};
	ret = i2c_write_dt(&bh_i2c,buff1,sizeof(buff1));
	if(ret != 0){
		printk("1- Failed to write to I2C device address 0x%c at Reg. 0x%c\n",bh_i2c.addr,BH1749_MODE_CONTROL1);
	}
	
	char buff2[] = {BH1749_MODE_CONTROL2,BH1749_MODE_CONTROL2_RGB_EN_ENABLE};
	ret = i2c_write_dt(&bh_i2c,buff2,sizeof(buff2));
	if(ret != 0){
		printk("2- Failed to write to I2C device address 0x%c at Reg. 0x%c\n",bh_i2c.addr,BH1749_MODE_CONTROL2);
	}
	return 1;
}

void main(void)
{
	int ret;
	uint8_t bh_value[10]= {0};

	printk("BH1749 Example Thingy:53! %s\n", CONFIG_BOARD);

	init_bh1749();

	while (1) {
		k_sleep(K_MSEC(1000));
		ret = i2c_burst_read_dt(&bh_i2c, BH1749_RED_DATA_LSB,bh_value,sizeof(bh_value));
		if(ret != 0){
			printk("3-Failed to read to I2C device address 0x%c at Reg. 0x%c\n",bh_i2c.addr,BH1749_RED_DATA_LSB);
		}
		else{
			printk("Red Value:\t %d | ", bh_value[0] | bh_value[1] << 8);
			printk("Green Value:\t %d | ", bh_value[2] | bh_value[3] << 8);
			printk("Blue Value:\t %d | ", bh_value[4] | bh_value[5] << 8);
			printk("IR Value:\t %d\n", bh_value[8] | bh_value[9] << 8);
		}

	}

}

Credits

Mahmood ul Hassan

Mahmood ul Hassan

13 projects • 18 followers
Electronics Engineer with more than 13 years of experience in reverse engineering and test & measurement equipment designing

Comments