samples: sensor: added sample app for INA219
This adds an example application for the TI INA219 Zero-Drift, Bidirectional Current/Power Monitor with I2C Interface Signed-off-by: Leonard Pollak <leonardp@tr-host.de>
This commit is contained in:
parent
ab60f8b16b
commit
e8d34bdcdf
6 changed files with 140 additions and 0 deletions
8
samples/sensor/ina219/CMakeLists.txt
Normal file
8
samples/sensor/ina219/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(ina219)
|
||||
|
||||
FILE(GLOB app_sources src/*.c)
|
||||
target_sources(app PRIVATE ${app_sources})
|
46
samples/sensor/ina219/README.rst
Normal file
46
samples/sensor/ina219/README.rst
Normal file
|
@ -0,0 +1,46 @@
|
|||
.. _ina219:
|
||||
|
||||
INA219 Bidirectional Power/Current Monitor
|
||||
##########################################
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
This sample application measures shunt voltage, bus voltage, power and current
|
||||
every 2 seconds and prints them to console.
|
||||
The calibration/configuration parameters can be set in the devicetree file.
|
||||
|
||||
References
|
||||
**********
|
||||
|
||||
- `INA219 sensor <https://www.ti.com/product/INA219>`_
|
||||
|
||||
Wiring
|
||||
******
|
||||
|
||||
The supply voltage of the INA219 can be in the 3V to 5.5V range.
|
||||
The common mode voltage of the measured bus can be in the 0V to 26V range.
|
||||
|
||||
Building and Running
|
||||
********************
|
||||
|
||||
.. zephyr-app-commands::
|
||||
:zephyr-app: samples/sensor/ina219
|
||||
:board: blackpill_f411ce
|
||||
:goals: build flash
|
||||
|
||||
Sample Output
|
||||
=============
|
||||
When monitoring a 3.3 V bus with a 0.1 Ohm shunt restistor
|
||||
you should get a similar output as below, repeated every 2 seconds:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
Shunt: 0.001570 [V] -- Bus: 3.224000 [V] -- Power: 0.504000 [W] -- Current: 0.157000 [A]
|
||||
|
||||
|
||||
A negative sign indicates current flowing in reverse direction:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
Shunt: -0.001560 [V] -- Bus: 3.224000 [V] -- Power: 0.502000 [W] -- Current: -0.156000 [A]
|
25
samples/sensor/ina219/boards/blackpill_f411ce.overlay
Normal file
25
samples/sensor/ina219/boards/blackpill_f411ce.overlay
Normal file
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Leonard Pollak
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
&i2c1 {
|
||||
status = "okay";
|
||||
compatible = "st,stm32-i2c-v1";
|
||||
clock-frequency = <I2C_BITRATE_FAST>;
|
||||
label = "I2C_0";
|
||||
|
||||
ina219@40 {
|
||||
status = "okay";
|
||||
compatible = "ti,ina219";
|
||||
reg = <0x40>;
|
||||
label = "INA219";
|
||||
brng = <0>;
|
||||
pg = <0>;
|
||||
sadc = <13>;
|
||||
badc = <13>;
|
||||
shunt-milliohm = <100>;
|
||||
lsb-microamp = <10>;
|
||||
};
|
||||
};
|
5
samples/sensor/ina219/prj.conf
Normal file
5
samples/sensor/ina219/prj.conf
Normal file
|
@ -0,0 +1,5 @@
|
|||
CONFIG_STDOUT_CONSOLE=y
|
||||
CONFIG_CBPRINTF_FP_SUPPORT=y
|
||||
CONFIG_I2C=y
|
||||
CONFIG_SENSOR=y
|
||||
CONFIG_INA219=y
|
14
samples/sensor/ina219/sample.yaml
Normal file
14
samples/sensor/ina219/sample.yaml
Normal file
|
@ -0,0 +1,14 @@
|
|||
#
|
||||
# Copyright (c) 2021 Leonard Pollak
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
|
||||
sample:
|
||||
name: TI INA219 power/current monitor
|
||||
description: Demonstration of the INA219 I2C power/current monitor
|
||||
tests:
|
||||
sample.drivers.ina219:
|
||||
build_only: true
|
||||
platform_allow: blackpill_f411ce
|
||||
tags: sensors
|
42
samples/sensor/ina219/src/main.c
Normal file
42
samples/sensor/ina219/src/main.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Leonard Pollak
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <stdio.h>
|
||||
#include <drivers/sensor.h>
|
||||
|
||||
|
||||
void main(void)
|
||||
{
|
||||
const struct device *ina = DEVICE_DT_GET_ONE(ti_ina219);
|
||||
struct sensor_value v_bus, power, current;
|
||||
int rc;
|
||||
|
||||
if (!device_is_ready(ina)) {
|
||||
printf("Device %s is not ready.\n", ina->name);
|
||||
return;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
rc = sensor_sample_fetch(ina);
|
||||
if (rc) {
|
||||
printf("Could not fetch sensor 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, ¤t);
|
||||
|
||||
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(¤t));
|
||||
k_sleep(K_MSEC(2000));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue