Thingy:53 is a compact nrf5340 based IoT development device from Nordic semiconductor with various kind of onboard sensors. In order to monitor the bidirectional current and supply voltage of external system we can use INA219.
Here we will learn how to create a basic thingy:53 application to read the high side current and voltage using INA219 attached through expansion port.
Before moving forward to creating a new application, we need to know the nRF5340 GPIO pins/communication interface which will be used to communicate with INA219 module. On thingy:53 module there is an expansion connector to interface external modules. GPIO pins available at expansion connector P1 also have I2C communication functionality (for details consult nRF5340 datasheet).
We can also use the Debug board connected at current measurement and debugging interface of thingy:53 device because it also contains the same nRF5340 GPIO which are connected at expansion connector P1.
For the easy of connection, we will use the debug and current measurement board connected at card edge connector of thingy:53 device.
From thingy:53 schematic we can find out that expansion interface is connected at P0.05 and P0.04 GPIO of nRF5340 soc.
P0.05 and P0.04 shown in above image represent GPIO port 0 pin 5 and pin 4 respectively and these pins can be used for I2C interfacing with external sensors. These two GPIO pins are also capable of analog inputs but in case of more than two analog signals or to interface external sensor module, we cannot use these GPIO pins as analog input only.
We will not be able to find the configuration of above mentioned GPIO pins and the second I2C interface in default pin control file, available in nRF connect SDK (its full path along with file name is given below).
C:\ncs\v2.1.0\zephyr\boards\arm\thingy53_nrf5340\thingy53_nrf5340_common-pinctrl.dtsi
In above mentioned file we won't see any configuration for P0.04 and P0.05. Therefore we will create an overlay file for the configuration of these GPIO pins, second I2C interface and INA219 external ADC module.
Now knowing all the basic stuff about external ADC module interfacing, we can create a new application for our thingy53 and build its configuration file.
After creating a simple hello world application and selecting its build configuration we will change the k-config of our application as shown below, for easy debugging and console monitoring. Also enable the "SENSORDRIVERS" in k-config config menu. As we are using analog sensor therefore we need to enable fraction value printing in console by enabling the float for printf function as shown below;
After making the above mentioned changes in k-config, we need to create an overlay file for thingy:53 in order to enable and communicate with the INA219 external current monitoring module. After creating the new overlay file add the following code to configure the GPIO pins as I2C interface.
&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;
};
};
};
And also add the following line in overlay file to enable the second I2C interface along with INA219 module.
&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>;
};
};
Now change the main.c code of hello_world application with the main.c code attached below. Then we can successfully build and flash it to thingy53 using external SEGGER J-Tag probe. When thingy53 firmware programming is finished, we can connect it through RTT interface, available under CONNECTED DEVICES panel, to read the INA219 supply parameters.
Note:
1)main.cand overlay files are attached below.
2) thingy:53 pinout details link: here
3) thingy:53 debug and current measurements board pinout details link: here
Comments
Please log in or sign up to comment.