Skip to content

Task 2: Measure Something

This Week’s Task is to measure something:

  • add a sensor
  • to a microcontroller board that you have designed
  • read it.

Current Sensor

For my final project the critical component I would need for Spiral 1 is a current sensor. Why? My project will involve the usage of Fuel Cells to convert Hydrogen into a power supply for energy consumption. So I decided to do a little bit of research about current sensor.

Current sensor as the name suggests - measure electric current. There are different ways to measure current and there are different types of current sensors catered to read AC or DC. As such there is a wide variety of current sensing modules out there. This is a summary of the different sensors based on my research

Type Current Transformer Shunt Hall Effect Rogowski Coil
Current AC AC & DC AC & DC AC
How does it work? Converts high current to a lower current by dividing it. The stepped-down/ divided output is sent to ammeters and other instruments for monitoring purposes, as well as to relays and other systems for protection applications in power systems. Detect voltage drop across a precisely calibrated shunt resistor. A shunt resistor is designed with low-resistance resistor to measure electrical current by generating a small, proportional voltage drop. This ensures that the impact on the original circuit is minimal, and the measurement is accurate. Generation of a voltage difference when a current-carrying conductor is placed in a magnetic field. These sensors operate without physical contact, making them ideal where galvanic isolation between the current source and their output is crucial. Rogowski coil measures the rate of change of the magnetic field around a conductor to determine the alternating current. They do not apply to DC applications.
Accuracy High High Medium Low
Range High Low because it requires direct contact Medium Medium
Isolation Yes No Yes Yes
Cost Moderate to High Low to Moderate Moderate Moderate
Application Metering & Billing, Energy Management Systems, etc Ammeters, Battery Management systems, Power supplies etc General-purpose current sensing High-frequency AC current measurement, power quality analysis.

Sources:

Based on the research I think for my first spiral - the most appropriate would be the Shunt sensor. Fuel Cells operate in DC and I would like to an accurate reading as the Fuel Cell supplies power to be used - converted from the Hydrogen Gas.

INA219

I decided to try out a popular and inexpensive current shunt sensor - INA219. The INA219 is a current shunt and power monitor with an I2C- or SMBUS-compatible interface. It monitors:

  • Shunt voltage drop (voltage drop measured across a low-resistance shunt resistor, used to calculate the current flowing through a circuit.)
  • Bus supply voltage (electrical potential difference between the conductors or “buses” in an electrical power distribution system, serving as a common voltage level for multiple circuits and components to connect and operate.)
  • Direct readouts of current in amperes.
  • An additional multiplying register calculates power in watts.

Because it comes in I2C interface(SDA/SCL) straightforward to connect and communicate with microcontrollers. It can handle high side current measuring, up to +26VDC, even though it is powered with 3 or 5V and enables bi-directional current measurements of up to 3.2Amp.

Here is the data sheet

In general this is how the sensor works alt text

Programming

Original Code from here Appropriated for ILI9341 and Xiao ESP32S3 Display

For this program - we will be measuring an LED Diode with a power supply from Elaine-duino. This is the system diagram. alt text As you can see for Vin+ and Vin-, there should be a closed connection for us to fully measure the voltage, current, and power of the load. In the case with this LED Diode, 3V from the Elaine-duino is supplying power to the LED Diode and it closes with Ground Pin.

The complete system diagram with the ILI9341 display looks like this. alt text

#include <Wire.h>
#include <Adafruit_INA219.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>

Adafruit_INA219 ina219;

// Define pins for the ILI9341 display
#define TFT_RST A0
#define TFT_DC  A1
#define TFT_CS  D7  // SS
#define TFT_MOSI D10  // MOSI
#define TFT_MISO D9
#define TFT_CLK D8  // SCK
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;

void setup(void) {
  Serial.begin(115200);
  while (!Serial) {
    // will pause Zero, Leonardo, etc until serial console opens
    delay(1);
  }
  uint32_t currentFrequency;

  if (!ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    while (1) {
      delay(10);
    }
  }

  tft.begin();
  tft.setRotation(3); // Adjust based on your display orientation
  tft.fillScreen(ILI9341_BLACK);

  Serial.println("Measuring voltage and current with INA219 ...");
}

void loop(void) {
  float shuntvoltage = 0;
  float busvoltage = 0;
  current_mA = 0;
  loadvoltage = 0;
  power_mW = 0;

  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);

  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  Serial.println("");

  tft.fillScreen(ILI9341_BLACK);
  voltCurrent();
  delay(2000);
  tft.fillScreen(ILI9341_BLACK);
  powerr();
  delay(2000);
}

void voltCurrent() {
  tft.setTextSize(2); // Draw 2X-scale text
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(0, 0);            // Start at top-left corner
  tft.print(loadvoltage);
  tft.print("V");
  tft.setCursor(0, 40);            // Adjust cursor position based on text size
  tft.print(current_mA);
  tft.print("mA");
}

void powerr() {
  tft.setTextSize(2); // Draw 2X-scale text
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(0, 60);            // Adjust cursor position based on text size
  tft.print(power_mW);
  tft.print("mW");
}

Takeaways

  • As we can see the voltage reading is consistent at around ~3.24V
  • The current reading comes out in a negative value of -34.20 mA. This actually signifies the current direction. A positive value indicates current flowing from the INA219’s Vin+ to Vin-, while a negative value indicates the opposite - current flowing from Vin- to Vin+. However since we have a load in between Vin+ and Vin- (LED), the current will have a negative sign as it flows from the power source (Vin+) to the load (Vin-).
  • The Blue LED Diode consumes 112 mW of Power.