I need to sample the INA219 current sensor on my Dahlia/Verdin setup at a high rate (~2khz or better). The INA219 datasheet claims the following conversion rates:
12-bit: 586uS (1.706 KHz)
11-bit: 304uS (3.289 KHz)
So at 11 bit resolution the chip should be up to the task.
The problem seems to be SW. When I read the sensor via
cat /sys/class/hwmon/hwmon3/power1_input >> /tmp/data
in a “while true” loop (from a shell prompt) I only get about 300 samples per second, so I’m off by at least a factor of 10.
Is there some software support or adjustment needed to get the sample rate of the INA219 closer to what the data sheet says is possible?
Here is the tdx-info output of my setup:
Software summary
Bootloader: U-Boot
Kernel version: 5.15.148-6.7.0+git.bfdbfb2c85fb #1-TorizonCore SMP PRE EMPT Thu Jun 20 15:59:41 UTC 2024
Kernel command line: root=LABEL=otaroot rootfstype=ext4 quiet logo.nologo v t.global_cursor_default=0 plymouth.ignore-serial-consoles splash fbcon=map:3 ost ree=/ostree/boot.1/torizon/e81878773da7cb84df22dfc3108fa98703a54183a50b88c0eeb46 705d5d934b7/0
Distro name: NAME=“TorizonCore”
Distro version: VERSION_ID=6.7.0-build.18
Distro variant: VARIANT=“Docker”
Hostname: verdin-imx8mp-15364539
Hardware info
HW model: Toradex Verdin iMX8M Plus on Verdin Development Board
Toradex version: 0063 V1.1B
Serial number: 15364539
Processor arch: aarch64
Hi, I’m the Toradex AI Assistant. I’m in early beta, and I would appreciate your feedback.
The Toradex Support team will follow up with you shortly!
Did you like my answer?
To achieve a sampling rate of ~2kHz or better with the INA219 current sensor on your Dahlia/Verdin setup, you’ll need to consider both the hardware capabilities and the software configuration. The INA219 sensor supports different conversion times for voltage and current measurements, which can be configured to optimize for higher sampling rates.
Hardware Considerations
Ensure that your hardware setup, including the Dahlia carrier board and Verdin iMX8M Plus module, is correctly wired and configured to communicate with the INA219 sensor. The I2C interface is typically used for this purpose.
Software Configuration
-
I2C Communication: The Linux kernel on your Verdin iMX8M Plus (version 5.15.148-6.7.0) includes support for I2C, which you can use to communicate with the INA219 sensor. You might need to ensure the I2C bus is enabled and accessible in your device tree or through kernel configuration.
-
INA219 Driver Configuration: The INA219 driver in the Linux kernel allows for configuring the conversion times for both voltage and current measurements. To achieve higher sampling rates, you should configure the INA219 to use the shortest conversion time possible. According to the datasheet, the shortest conversion time for both voltage and current measurements is 140µs, which theoretically allows for a sampling rate higher than 2kHz.
-
Adjusting the INA219 Configuration:
- Access the INA219 configuration through the sysfs interface or by writing to the appropriate I2C registers directly from your application.
- Set the
Configuration Register
to use the shortest conversion time (140µs) for both voltage and current measurements.
- Ensure that continuous measurement mode is enabled for the highest possible sampling rate.
-
Software Sampling:
- Implement a software loop or use a timer to read the measurement values from the INA219 at your desired rate.
- Consider the overhead of I2C communication and any processing you need to perform on the measurements, which might affect the achievable sampling rate.
-
Real-time Considerations: Since you’re aiming for a high sampling rate, ensure that your application’s timing requirements are met. The PREEMPT_RT patch or configuring your application with real-time priorities might help reduce jitter and ensure more consistent sampling intervals.
-
Monitoring and Optimization: Use tools like htop
or iostat
to monitor CPU and I/O performance. Optimize your application and system settings as needed
Please, also check if these links can help you solve your problem:
Operating Toradex Wi-Fi/BT Capable Modules Using Dual and Single Antenna Configurations | Toradex Developer Center.
Unfortunately, sampling the INA219 at a 2kHz rate is currently not feasible. The INA219 driver has a hardcoded configuration for 12-bit resolution, which corresponds to a 532ms sampling time. Additionally, the driver is not optimized for high-rate reading from user space.
Could you please explain the reason for requiring such high-speed current consumption measurement? Perhaps we can offer an alternative solution. If this high rate is essential, you may need to write your own driver for the INA219, optimized for high-rate reading and data transfer from kernel space.
Please also consider that the Dahlia board has many other I2C devices on the same I2C bus, such as the RTC, USB hub, PCI board (if inserted), DSI adapter (if installed), audio codec, EEPROM, and temperature sensor. Flooding the I2C bus with a large number of INA219 transactions may affect the performance of these other devices.