Mahmood ul Hassan
Published © GPL3+

How to interface Nordic Thingy:53 with INA219 current sensor

Learn how to interface Nordic thingy:53 with INA219 high side current sensor to monitor the bidirectional current and supply voltage.

IntermediateProtip30 minutes527
How to interface Nordic Thingy:53 with INA219 current sensor

Things used in this project

Hardware components

Nordic Thingy:53
Nordic Semiconductor Nordic Thingy:53
×1
Adafruit INA219 High Side DC Current Sensor Breakout
×1
Breadboard (generic)
Breadboard (generic)
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×1

Software apps and online services

nRF Connect SDK
Nordic Semiconductor nRF Connect SDK

Story

Read more

Code

thingy53_nrf5340_cpuapp_ns.overlay

C/C++
// 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

&pinctrl {
	i2c2_default: i2c2_default {
		group1 {
			psels = <NRF_PSEL(TWIM_SDA, 0, 4)>,
				<NRF_PSEL(TWIM_SCL, 0, 5)>;
		};
	};

	i2c2_sleep: i2c2_sleep {
		group1 {
			psels = <NRF_PSEL(TWIM_SDA, 0, 4)>,
				<NRF_PSEL(TWIM_SCL, 0, 5)>;
			low-power-enable;
		};
	};
};

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

	pinctrl-0 = <&i2c2_default>;
	pinctrl-1 = <&i2c2_sleep>;
	pinctrl-names = "default", "sleep";

	ina219: ina219@41 {
		status = "okay";
		compatible = "ti,ina219";
		reg = <0x41>;
		brng = <0>;
		pg = <0>;
		sadc = <13>;
		badc = <13>;
		shunt-milliohm = <100>;
		lsb-microamp = <10>;
	};
};

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>

static const struct device *ina = DEVICE_DT_GET_ONE(ti_ina219);
static struct sensor_value v_bus, power, current;

void main(void)
{
	int ret;

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

	if (!device_is_ready(ina)) {
		printk("Device %s is not ready.\n", ina->name);
		return;
	}
	printk("Device %p name is %s\n", ina, ina->name);

	while(1){
		ret = sensor_sample_fetch(ina);
		if (ret) {
			printk("Could not fetch ina219 data.\n");
			return;
		}
		sensor_channel_get(ina, SENSOR_CHAN_VOLTAGE, &v_bus);
		sensor_channel_get(ina, SENSOR_CHAN_POWER, &power);
		sensor_channel_get(ina, SENSOR_CHAN_CURRENT, &current);

		printf("Bus: %f [V] | Power: %f [W] | Current: %f [A]\n", \
				sensor_value_to_double(&v_bus), \
				sensor_value_to_double(&power), \
				sensor_value_to_double(&current));
		k_sleep(K_MSEC(2000));
	}
}

Credits

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

Comments

Please log in or sign up to comment.