Measuring Power and Energy Consumption Using PAC1934 Monitor with Linux®

 Objective

This tutorial shows how to setup and use the PAC1934 Quad DC Power Monitor in an application running on an embedded Linux® host to measure the power and energy consumption of a test load. The application is available in both C and Python and uses the PAC1934 Linux driver.

Overview

The tutorial makes use of MikroElektronika’s Click boards™ and Microchip’s SAMA5D27-SOM1-EK1 Board to simplify hardware integration.

The tutorial showcases the capability of the PAC1934 to provide measurements of instantaneous voltage, current, and power as well as the accumulated power. Together with the PAC1934 Linux driver, this tutorial shows you how to measure and display these parameters.

Microchip’s PAC1934 is a four-channel power and energy monitoring device. A high-voltage multiplexer sequentially connects the inputs to a bus voltage monitor and current sense amplifier that feeds high-resolution Analog-to-Digital Converters (ADCs). Digital circuitry performs power calculations and energy accumulation.

Energy monitoring intervals can range from 1 ms to 36+ hours. Bus voltage, sense resistor voltage, and accumulated proportional power are stored in registers for retrieval by the system master or embedded controller. The sampling rate and energy integration period can be controlled over System Management Bus (SMBus) or Inter-Integrated Circuit (I2C). Active channel selection, one-shot measurements, and other controls are also available.

The PAC1934 device uses real-time calibration to minimize offset and gain errors. No input filters are required for this device.

For more details about the features of the PAC1934 device family, refer to the PAC1931/PAC1932/PAC1933/PAC1934 Data Sheet.

The PAC1934 Linux Driver is an IIO class Linux driver. It provides all the information needed by a driver handling a device. The user will interact with PAC1934 through sysfs in which the driver exposes chip info and allows applications to set various configuration parameters.

This training makes use of the SAMA5D27-SOM1-EK1 Board , OLED W Click and the PAC1934 Click, which are available from Microchip Purchasing and Client Services.

C Application

The application initializes the peripherals and OLED driver needed by the application in APP_INIT state. It initializes the following:

  1. Serial Peripheral Interface (SPI) device – For transferring data to OLED W Click.
  2. General Purpose Input/Output (GPIO) – For controlling OLED W Click and for polling user button using non-blocking read.
  3. OLED Driver – For displaying data to OLED W Click.

The application communicates to these peripherals using Input/Output Control (IOCTL) commands and ensures that no other application can use the device while it is being used.

After APP_INIT state, the application goes through the each of the following states:

  1. APP_GET_IIO_DATA – State to get PAC1934 Data.
  2. APP_PRINT_TO_CONSOLE – State to display PAC1934 Data to serial console/terminal.
  3. APP_PRINT_TO_DISPLAY – State to display PAC1934 Data to OLED W Click.

The application reads any GPIO input events (GPIO_EVENT_TASKS) after each state. The state goes back to the initial state, APP_GET_IIO_DATA, after reaching the final state, APP_PRINT_TO_DISPLAY.

c-app-state-diagram-1a.png

Python Application

The application initializes the following class objects required by the demonstration:

  1. PAC193x Object – for interacting with the pac193x driver sysfs interface
  2. Demo Object – for interacting with the OLED driver that interfaces with the OLED W Click using the Microchip Peripheral I/O (MPIO) Python package
  3. Btn Object – for polling for GPIO events using Python selectors

The Python application communicates with the peripherals using functions defined within the class objects, which in turn use the MPIO package.

After initializing the class objects, the Python application cycles through the following distinct operations:

  1. Update PAC1934 values – reads the values from the exposed sysfs interface using Python File Input/Output (I/O)
  2. Update values on serial terminal – prints the updated values to the serial terminal interface
  3. Update values on OLED W Click - prints the updated values for the selected channel to the OLED Display

The Python application polls for GPIO events using Python selectors. If no event is detected within a 20 ms timeout, the selector returns, and the cycle starts with updating the PAC1934 values again.

Picture2.png

Lab Objectives

  • Connect the Hardware
  • Setup the Linux build environment
  • Configure and generate the Linux image
  • Implement the application either in C or in Python

 Materials

Hardware Tools

Tool About Purchase
board-50px.png
SAMA5D27-SOM1-EK1 Development Board
Development Board
board-50px.png
MikroElectronika PAC1934 Click
Development Board
board-50px.png
MikroElectronika OLED W Click
Development Board

ATSAMA5D27-SOM1-EK1

SOM1-EK1.png

The ATSAMA5D27-SOM1-EK1 is a fast prototyping and evaluation platform for the SAMA5D2-based System in Packages (SiPs) and the SAMA5D27-SOM1 (SAMA5D27 System on Module). The kit comprises a baseboard with a soldered ATSAMA5D27-SOM1 module. The board features a wide range of peripherals, as well as a user interface and expansion options, including two mikroBUS™ click interface headers to support MikroElektronika Click boards and one PMOD™ interface. Linux distribution and software package allows you to easily get started with your development.

MikroElectronika PAC1934 Click

mikroe_pac1934.png

The PAC1934 click board from MikroElectronika carries the PAC1934 four channel DC power/energy monitor from Microchip. The click is designed to run on either 3.3 V or 5 V power supply. It communicates with the target microcontroller over an I2C interface.

MikroElectronika OLED W Click

mikroe_oled.png

The OLED W click board from MikroElectronika carries a 96 x 39 px white monochrome passive matrix OLED display. The display is bright, has a wide viewing angle, and low power consumption.

Hardware Connection Setup

Apart from the hardware tools listed above, the following items are required:

  • PC Host build environment:
    • CPU – 2 or more cores
    • RAM – 4 GB or more
    • Virtual Disk Space – 50 GB or more
  • SD card for the Linux image – at least 2 GB capacity
  • USB Type-A male to Micro-B male cable for board power and serial communication
  • Ethernet cable for network interface
Picture6.png

To simulate a multiple channel load, use a 5V power supply and four 47Ω resistors. The power supply can be turned off at this time. Refer to the following diagram for connections to the PAC1934 Click:

Picture7.png

Connection Diagram

The SAMA5D2 connects to the OLED over SPI, PAC1934 over I2C, local network over the ethernet port and power and serial communications over the USB port.

Picture8.png

Software Tools

Note:
This project has been verified to work with the following versions of software tools:

  • PAC193x IIO class Linux driver (v0.1.0)
  • VirtualBox 6.1.26 (with Oracle VM VirtualBox Extension Pack)
  • Ubuntu 20.04.2.0 64-bit LTS
  • Balena Etcher App Image
  • Embedded Linux Build System
  • Linux4SAM 2021.04
  • Linux Kernel 5.10.x (+ LTS Maintenance updates up to v5.10.50)
  • AT91Bootstrap 4.0.0
  • U-Boot 2021.04
  • Buildroot – 2021.02.3 Release
  • Buildroot-external-microchip – same version as above
  • Python 3.9
  • File extraction utility such as 7-Zip that supports ZIP files

Because we regularly update our tools, occasionally you may discover an issue while using the newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.

Lab Source Files and Solutions

This ZIP file contains the completed solution project for this lab. It also contains the source files needed to perform the lab as per the following step-by-step instructions.
Instructions on how to unzip and use the file shall be outlined in the Lab Steps.

The contents of this ZIP file need to be placed in a folder of your choice.

Lab Index

Note: Steps 1 - 5 must be completed before you are ready to build, download, and run the application.

Step 1: Connect hardware

  • Step 1.1 - Attach PAC1934 Click board
  • Step 1.2 - Attach OLED W Click
  • Step 1.3 - Connect USB from ATSAMA5D27-SOM1-EK1 to PC host
  • Step 1.4 - Required jumper settings
  • Step 1.5 - Prepare SD card for the embedded Linux image
  • Step 1.6 - Connect a 5V power supply to the PAC1934 Click board
  • Step 1.7 - Attach four 47Ω resistors from PAC1934 Click board to the ground

Step 2: Setup Build Environment

  • Step 2.1 - Setup a Linux development system
  • Step 2.2 - Install Buildroot dependencies
  • Step 2.3 - Create a project directory
  • Step 2.4 - Clone Buildroot
  • Step 2.5 - Clone Buildroot-External-Microchip
  • Step 2.6 - Checkout repository versions

Step 3: Configure Buildroot

  • Step 3.1 - Create a BR2_EXTERNAL tree structure
  • Step 3.2 - Configure BR2_EXTERNAL
  • Step 3.3 - Add packages through menuconfig
  • Step 3.4 - Add target packages
  • Step 3.5 - Run Make

Step 4: Application-Specific Configuration

  • Step 4.1 - Download and extract project files
  • Step 4.2 - Add PAC193x and spidev Linux drivers
  • Step 4.3 - Configure the device tree
  • Step 4.4 - Modify the spidev driver
  • Step 4.5 - Rebuild the image
  • Step 4.6 - Flash the image to the SD Card using balenaEtcher
  • Step 4.7 - Verify if drivers have been loaded
  • Step 4.8 - Verify device tree changes
  • Step 4.9 - Configure network interface for deploying the application

Step 5: Configure PAC193x Sample Application
For Python:

  • Step 5.1 - Copy Python source files to the target

For C:

  • Step 5.1 - Build C sample application from Buildroot
  • Step 5.2 - Upload the compiled code to the target

Step 6: Test Application

  • Step 6.1 – Run PAC1934 sample applications

Appendix

  • Troubleshooting common issues
  • Buildroot errors
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.