Funtoo Power Monitor

From Funtoo
Jump to navigation Jump to search

Making a Funtoo Based DC Voltage/Current Monitoring System Using a Raspberry Pi and INA219 Breakout Board

N.B.: This page is being fleshed out more as I have more time to devote to it. —Ed

Most guides on the internet for doing things like power monitoring on a Raspberry Pi are Debian distro based, and, as such, lack essential things that will make them work on a distro like Funtoo. There is no reason that Funtoo cannot be a first class player in this space, and guides such as these are aimed at alleviating this lack of coverage for Funtoo-based platforms.

This guide focuses on creating a DC voltage and DC current monitoring system utilizing a Raspberry Pi and the INA219 Current Sensor Breakout board. The INA219 can measure DC voltages up to +32V and DC current up ±3.2A (it can measure higher currents with a different sense resistor). Since dedicated hardware for this kind of monitoring and logging is quite expensive, this combination makes an attractive alternative—not to mention the extreme customizability of this solution.

   Note

This guide is geared towards Raspberry Pi models that have a 40-pin GPIO header and at least 512MB of RAM.

Basic Outline

  • Acquire parts (RPi, INA219, misc. hookup wires, etc)
  • Connect parts
  • Install & configure software on the RPi

Acquire and Prepare Parts

Obviously, we'll need to acquire a Raspberry Pi, an INA219 breakout board, and various wires to connecting things up. The INA219 comes with a 6 pin header and a 2 pin terminal block that are not soldered to the board; it's typically easier to deal with hooking things up with those parts soldered in.

Connect Parts

Once we have acquired the INA219 and soldered the headers on it, we can now hook it up to our Raspberry Pi. The basic connections are (pins on the INA219 going from left to right):

  • Vcc goes to pin 4 on the GPIO header
  • Gnd goes to pin 6 on the GPIO header
  • SCL goes to pin 5 on the GPIO header
  • SDA goes to pin 3 on the GPIO header
  • A ground line to the RPi will be needed, so connect a wire to pin 9 on the GPIO header

Connect a wire to Vin+ on the INA219 terminal connector. If you intend to measure current, connect another wire to Vin- on the terminal connector.

Install & Configure Software

Out of the box, the Raspberry Pi disables the I²C bus, so we have to enable it. First, add the following lines to /boot/config.txt:

   /boot/config.txt - Raspberry Pi configuration
dtparam=i2c1=on
dtparam=i2c_arm=on

We'll also need to have a module loaded at boot time as well. Add the following to /etc/conf.d/modules:

   /etc/conf.d/modules - I²C module configuration
modules="i2c_dev"

Next, we need to install a Python module to read the smbus. The python USE flag should be enabled:

root # emerge -avq i2c-tools

Here's where things get messy. We need four more packages (pi_ina219, RPi.GPIO, Adafruit_GPIO, and Adafruit_PureIO) but they aren't in the tree yet. Hopefully by the time this is revised there will be ebuilds for these packages. For now, it's enough to download and install them using their installation methods.

Testing

With i2c-tools installed, we can check to see if the RPi can see the INA219 board (use -y 0 for a 256MB RPi):

root # i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

Assuming you didn't change the address of the board (up to four can be connected to the same I²C bus), you should see a 40 in the output. Also, assuming that the i2c_dev module was loaded on boot, you should see an i2c device in /dev:

root # ls /dev/i2c*
/dev/i2c-1

For measuring straight DC voltages, all that is needed is to connect the Vin+ wire to the battery positive, and connect the RPi ground (the wire connected to pin 9 on the GPIO header) to the battery negative. If you intend to measure the current consumption of a circuit while powered by the battery being monitored, you will want to connect the Vin- wire to the positive side of the circuit, and RPi ground to the negative side of the circuit.

   Note

There should be no arcing or sparking anywhere! If there is, immediately disconnect the offending part, and double check all your connections to make sure that they are correct, and that nothing is shorting out.

Once your circuit is hooked up, you can use the example program in the pi_ina219 package to test that things are working as they should:

root # ./example.py
Bus Voltage    : 6.384 V
Bus Current    : 0.100 mA
Supply Voltage : 6.384 V
Shunt voltage  : 0.010 mV
Power          : 0.000 mW