http://forums.adafruit.com/viewtopic.php?f=19&t=42405
(thanks for that!)
However I'd also like to sense the VOLTAGE in either direction. I did a quick test with a DC motor just running continuously in one direction (code below), and this is the type of output I get for each direction. The only difference is in the M!->run(x); line in the code below, where x is FORWARD or BACKWARD.
Note: I did not make the 3rd connection called for in the INA219 tutorial where it says "connect a wire from the negative terminal of the power supply to GND". When I did this the motor would run backward fine, but not forward, and it didn't change the printed output in the backward case anyway.
BACKWARD
Current: 15.80 mA
Bus Voltage: 5.92 V
Shunt Voltage: 1.74 mV
Load Voltage: 5.93 V
FORWARD
Current: -13.20 mA
Bus Voltage: 0.00 V
Shunt Voltage: -1.79 mV
Load Voltage: -0.00 V
So I can see the current change direction nicely, and the shunt voltage flips too, but Bus and Load are 0? Is there a way to fix this?
Code: Select all
/*
Hardware: Adafruit Motor Shield v2
http://www.adafruit.com/products/1438
Adafruit INA219 High Side DC Current Sensor Breakout
http://www.adafruit.com/products/904
Arduino UNO
Wiring --------
DC motor:
1 lead to M1 top (nearest USB port)
1 lead to Vin- on breakout
breakout:
Vin+ to M1 bottom
Power --------
1) USB drives Arduino
2) External power from wall Wart-->
DC barrel jack http://www.adafruit.com/products/373 -->
through LM317 adjustable regulator (set to 6V) https://www.sparkfun.com/products/527 -->
Motor Shield power screw jacks
10/23/2013 test DC motor run from v2 shield with INA219 breakout
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Adafruit_INA219.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *M1 = AFMS.getMotor(1);
Adafruit_INA219 ina219;
void setup() {
uint32_t currentFrequency;
Serial.begin(115200);
Serial.println("DC Motor with Current Sensing test!");
AFMS.begin(); // create with the default frequency 1.6KHz
ina219.begin(); // initialize the shield
}
void loop() {
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
M1->setSpeed(255);
M1->run(FORWARD);
current_mA = ina219.getCurrent_mA();
busvoltage = ina219.getBusVoltage_V();
shuntvoltage = ina219.getShuntVoltage_mV();
loadvoltage = busvoltage + (shuntvoltage / 1000);
Serial.print("Current: ");
Serial.print(current_mA);
Serial.println(" mA");
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(" V");
Serial.println("");
delay(100);
}