Measure Power Consumption with INA219 and STM32 for IoT Devices

Using the INA219 Current Sensor with an STM32 to Measure Power Consumption in Real-Time for Battery-Powered IoT Devices

The INA219 is a high-side current shunt and power monitor that communicates via I2C. This guide will walk you through the steps needed to integrate the INA219 current sensor with an STM32 microcontroller to monitor power consumption in real-time, which is particularly useful for battery-powered IoT devices.

Prerequisites

  • Basic knowledge of C/C++ programming
  • Familiarity with STM32 microcontrollers
  • Arduino IDE or STM32CubeIDE installed
  • Basic electronics knowledge
  • Access to an oscilloscope or multimeter (optional)

Parts/Tools

  • INA219 Current Sensor Module
  • STM32 Microcontroller (e.g., STM32F103C8T6)
  • Breadboard and jumper wires
  • USB to serial converter (if needed)
  • Power supply (battery or DC source)

Steps

  1. Wiring the INA219 to STM32
    • Connect the VCC pin of INA219 to the 5V or 3.3V pin on STM32.
    • Connect the GND pin of INA219 to the ground (GND) on STM32.
    • Connect the SDA (Data line) of INA219 to the SDA pin on STM32.
    • Connect the SCL (Clock line) of INA219 to the SCL pin on STM32.
    • Connect the shunt resistor across the load you wish to measure.
  2. Setting Up the Development Environment
    • Open the Arduino IDE or STM32CubeIDE.
    • Install the required libraries:
      #include 
      #include 
  3. Initialize the INA219

    In the setup function of your code, initialize the INA219 sensor:

    Adafruit_INA219 ina219;
    
    void setup() {
        Serial.begin(115200);
        ina219.begin();
    }
  4. Read Power Consumption

    In the loop function, read the voltage, current, and power:

    void loop() {
        float shuntVoltage = ina219.getShuntVoltage_mV();
        float busVoltage = ina219.getBusVoltage_V();
        float current = ina219.getCurrent_mA();
        float power = ina219.getPower_mW();
    
        Serial.print("Shunt Voltage: "); Serial.print(shuntVoltage); Serial.println(" mV");
        Serial.print("Bus Voltage: "); Serial.print(busVoltage); Serial.println(" V");
        Serial.print("Current: "); Serial.print(current); Serial.println(" mA");
        Serial.print("Power: "); Serial.print(power); Serial.println(" mW");
        
        delay(2000); // Delay for readability
    }
  5. Upload and Test the Code

    Upload your code to the STM32 board and open the Serial Monitor to view real-time power consumption data.

Troubleshooting

  • INA219 Not Responding: Check your wiring connections, ensuring SDA and SCL are connected to the correct pins.
  • Incorrect Readings: Verify the shunt resistor value and ensure it’s correctly placed in the circuit.
  • Communication Errors: Make sure that the I2C address is correct. Use a scanner to check for devices on the bus.
  • Power Supply Issues: Ensure your power supply voltage matches the INA219 specifications.

Conclusion

Integrating the INA219 current sensor with an STM32 microcontroller allows you to effectively measure power consumption in real-time for your IoT projects. This setup is invaluable for optimizing battery usage in battery-powered devices. With the steps outlined above, you can quickly get your system up and running.

Leave a Comment

Your email address will not be published. Required fields are marked *