Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

INA219 protection for motor measurements
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

INA219 protection for motor measurements

by dsxoiniana on Wed Dec 11, 2019 3:20 am

I have the INA219 DC Current sensor and I would like to measure the power consumption of some LEGO motors I control with the Adafruit Motor Shield v1.0 --> https://forums.adafruit.com/viewtopic.php?f=8&t=159116. I read in the INA219 page that I should place a capacitor and a diode to protect the sensor from spikes. I have no great experience with electronics, could someone post a schematic on how the cabling should be? And also what characteristics should the capacitor and the diode have? Thanks in advance.

dsxoiniana
 
Posts: 51
Joined: Mon Nov 25, 2019 4:41 pm

Re: INA219 protection for motor measurements

by dsxoiniana on Wed Dec 11, 2019 7:58 am

Sorry I meant Motor Shield v2.0

dsxoiniana
 
Posts: 51
Joined: Mon Nov 25, 2019 4:41 pm

Re: INA219 protection for motor measurements

by adafruit_support_bill on Wed Dec 11, 2019 8:04 am

Do you need to measure each motor channel separately, or just overall current consumption through the shield?

The shield has built-in diode protection in the TB6612 H-Bridge chips. This protects against reverse inductive voltage spikes from the motor. Adding a capacitor between power and ground will help to suppress some of the brush noise. You may also want to do a bit of software noise filtering.

adafruit_support_bill
 
Posts: 78721
Joined: Sat Feb 07, 2009 10:11 am

Re: INA219 protection for motor measurements

by dsxoiniana on Wed Dec 11, 2019 9:31 am

adafruit_support_bill wrote:Do you need to measure each motor channel separately, or just overall current consumption through the shield?

The shield has built-in diode protection in the TB6612 H-Bridge chips. This protects against reverse inductive voltage spikes from the motor. Adding a capacitor between power and ground will help to suppress some of the brush noise. You may also want to do a bit of software noise filtering.
I would like to keep it simple at the beginning and measure the current through the shield. Here is my schematic so far

Image

When the shield is off I get measurements like these:

Code: Select all | TOGGLE FULL SIZE
Bus Voltage:   0.67 V
Shunt Voltage: -0.09 mV
Load Voltage:  0.67
Current:       -0.80
Power:         0.00

Bus Voltage:   0.67 V
Shunt Voltage: -0.06 mV
Load Voltage:  0.67
Current:       -0.90
Power:         0.00

Bus Voltage:   0.67 V
Shunt Voltage: -0.05 mV
Load Voltage:  0.67
Current:       -0.80
Power:         0.00


When shield is on and motor is running I get measurements like the ones below which seem wrong to me (6.5mA are too low I think).
Code: Select all | TOGGLE FULL SIZE
Bus Voltage:   8.54 V
Shunt Voltage: 0.61 mV
Load Voltage:  8.54
Current:       6.50
Power:         56.00

Bus Voltage:   8.53 V
Shunt Voltage: 0.61 mV
Load Voltage:  8.53
Current:       6.10
Power:         52.00

Bus Voltage:   8.52 V
Shunt Voltage: 0.64 mV
Load Voltage:  8.52
Current:       6.00
Power:         52.00

Bus Voltage:   8.50 V
Shunt Voltage: 0.60 mV
Load Voltage:  8.50
Current:       6.10
Power:         50.00

dsxoiniana
 
Posts: 51
Joined: Mon Nov 25, 2019 4:41 pm

Re: INA219 protection for motor measurements

by adafruit_support_bill on Wed Dec 11, 2019 9:57 am

Please post the code you are using.

adafruit_support_bill
 
Posts: 78721
Joined: Sat Feb 07, 2009 10:11 am

Re: INA219 protection for motor measurements

by dsxoiniana on Wed Dec 11, 2019 10:04 am

adafruit_support_bill wrote:Please post the code you are using.


Code: Select all | TOGGLE FULL SIZE
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2
---->   http://www.adafruit.com/products/1438
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

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

     uint32_t currentFrequency;
   
  Serial.println("Hello!");
 
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  ina219.begin();
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  //ina219.setCalibration_16V_400mA();
 
  AFMS.begin(1200);  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
 
  // Set the speed to start, from 0 (off) to 255 (max speed)
  //myMotor->setSpeed(150);
  //myMotor->run(FORWARD);
  // turn on motor
  myMotor->run(RELEASE);
  myMotor2->run(RELEASE);
}

void loop() {
  float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float 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("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" ");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" ");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" ");
 
  uint8_t i;
  myMotor->run(FORWARD);
  myMotor2->run(FORWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(200);
    myMotor2->setSpeed(200); 
    delay(10);
  }
//  for (i=255; i!=0; i--) {
//    myMotor->setSpeed(255);
//    myMotor2->setSpeed(200); 
//    delay(10);
//  }
  delay(1000);
 
  myMotor->run(BACKWARD);
  myMotor2->run(BACKWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(100);
    myMotor2->setSpeed(200); 
    delay(10);
  }
//  for (i=255; i!=0; i--) {
//    myMotor->setSpeed(150);
//    myMotor2->setSpeed(200); 
//    delay(10);
//  }

  myMotor->run(RELEASE);
  myMotor2->run(RELEASE);
  delay(1000);
}

dsxoiniana
 
Posts: 51
Joined: Mon Nov 25, 2019 4:41 pm

Re: INA219 protection for motor measurements

by adafruit_support_bill on Wed Dec 11, 2019 10:30 am

You are only reading the current at the top of the loop - before the motors start running.

adafruit_support_bill
 
Posts: 78721
Joined: Sat Feb 07, 2009 10:11 am

Re: INA219 protection for motor measurements

by dsxoiniana on Wed Dec 11, 2019 11:03 am

adafruit_support_bill wrote:You are only reading the current at the top of the loop - before the motors start running.
That might have affected the very first reading but then the motor runs constantly so that shouldn't be the case. I also moved the current reading part at the bottom of the code but no change. I started getting weird values after I tried to put some force to the motor (without stalling it) and see how this affects the current.

dsxoiniana
 
Posts: 51
Joined: Mon Nov 25, 2019 4:41 pm

Re: INA219 protection for motor measurements

by adafruit_support_bill on Wed Dec 11, 2019 12:15 pm

That might have affected the very first reading but then the motor runs constantly so that shouldn't be the case.

The last 3 lines of your loop are:

Code: Select all | TOGGLE FULL SIZE
   myMotor->run(RELEASE);
  myMotor2->run(RELEASE);
  delay(1000);

So the motor should not be running when the loop starts again.

adafruit_support_bill
 
Posts: 78721
Joined: Sat Feb 07, 2009 10:11 am

Re: INA219 protection for motor measurements

by dsxoiniana on Wed Dec 11, 2019 1:08 pm

Thanks, resolved. Next step will be to measure the current per channel. Any suggestions to protect INA219? I want to understand where to place capacitors/diodes.

dsxoiniana
 
Posts: 51
Joined: Mon Nov 25, 2019 4:41 pm

Re: INA219 protection for motor measurements

by adafruit_support_bill on Wed Dec 11, 2019 2:02 pm

External diodes would prevent you from reversing the motor. But the ones built into the shield bridge circuits should provide protection from the inductive spikes. Capacitor recommendations for brush noise can be found in the shield FAQ. Although you may not have access to the motor casing for optimal placement with your Lego motors.

adafruit_support_bill
 
Posts: 78721
Joined: Sat Feb 07, 2009 10:11 am

Re: INA219 protection for motor measurements

by dsxoiniana on Thu Feb 27, 2020 5:04 am

I'm reviving this thread since in the meantime my robot arm is almost finished. Currently @ the office so will post some better pics and code later on. For the time being a quick one with bad quality here:

Image

I'm controlling 6 LEGO motors via the mqtt libraries for Node MCU from a simple app in my phone. I've also included an INA219 DC current sensor in the setup to monitor the total power consumption for the shields/motors. I would like a confirmation from the community that my setup is correct. Here it is:

Image

The 2 shields are obviously stacked so I'm not showing all connections. What is important is to understand if the way the 2 shields are connected and the way the INA219 sensor is configured is correct to measure the full power consumption in both shields.

dsxoiniana
 
Posts: 51
Joined: Mon Nov 25, 2019 4:41 pm

Re: INA219 protection for motor measurements

by adafruit_support_bill on Thu Feb 27, 2020 7:01 am

Nice looking arm!
As connected, you would only be measuring the current on motor1.
And I don't see any ground path between the shields and the power supply & current sensor.

adafruit_support_bill
 
Posts: 78721
Joined: Sat Feb 07, 2009 10:11 am

Re: INA219 protection for motor measurements

by dsxoiniana on Thu Feb 27, 2020 7:27 am

adafruit_support_bill wrote:Nice looking arm!
As connected, you would only be measuring the current on motor1.
And I don't see any ground path between the shields and the power supply & current sensor.
Thanks Bill. There is a black ground line from the DC current sensor to the Node MCU and then the Node MCU shares a ground with the ground from the power supply. Isn't this sufficient?

dsxoiniana
 
Posts: 51
Joined: Mon Nov 25, 2019 4:41 pm

Re: INA219 protection for motor measurements

by adafruit_support_bill on Thu Feb 27, 2020 7:34 am

That is sufficient for the sensor. But you still need a common ground for a signal reference for the shields - ideally direct from the shields to the processor.
And you need a current return path for the motor power - ideally direct from the shields to the power supply.

adafruit_support_bill
 
Posts: 78721
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.