Hi all I am currently conducting a project utilising a L298N Motor Driver to control two motors independently. I wish to connect an Adafruit INA219 current sensor to read current drawn from Motor 1 and at certain conditions such as if Motor 1 is receiving no current to initiate the spin of Motor 2.
I exclusively power the Motor Driver (logic side as well) using a 12V external power supply. I have then connected my INA219 to the Arduino 5V supply and have grounded all components on the same rail. When reading current I am receiving extremely volatile readings spiking from 0 to 500ma and so on.
From my description could anyone inform me on what I'm doing wrong or how I should approach this better?
Thanks in advance.
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
//L293D
//Motor A
const int motorPin1 = 9; // IN4 of L293
const int motorPin2 = 10; // IN3 of L293
//Motor B
const int motorPin3 = 6; // IN2 of L293
const int motorPin4 = 5; // IN1 of L293
//This will run only one time.
void setup(void){
//Set pins as outputs
//pinMode(motorPin1, OUTPUT);
//pinMode(motorPin2, OUTPUT);
//pinMode(motorPin3, OUTPUT);
//pinMode(motorPin4, OUTPUT);
//Motor Control - Motor A: motorPin1,motorpin2 & Motor B: motorpin3,motorpin4
//This code will turn Motor A clockwise for 2 sec.
//delay(5000);
/*This code will turn Motor A counter-clockwise for 2 sec.
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 180);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 180);
delay(5000);
//This code will turn Motor B clockwise for 2 sec.
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 180);
analogWrite(motorPin3, 180);
analogWrite(motorPin4, 0);
delay(1000);
//This code will turn Motor B counter-clockwise for 2 sec.
analogWrite(motorPin1, 180);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 180);
delay(1000);
//And this code will stop motors
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 0); */
{Serial.begin(115200);
while (!Serial) {
// will pause Zero, Leonardo, etc until serial console opens
delay(1);
}
uint32_t currentFrequency;
ina219.begin();
}
}
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);
delay(1000);
Serial.println("");
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.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW");
Serial.println("");
if (current_mA > 10 || current_mA < -10)
{analogWrite(motorPin1, 100);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
analogWrite(motorPin4, 0);
delay(1000);}
if (-10 <current_mA < 10){
Serial.print("CONSTANT SIGNAL FAULT");
Serial.println("");
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 100);
analogWrite(motorPin3, 100);
analogWrite(motorPin4, 0);
delay(1000);}
if (current_mA>150)
{Serial.print("MAX CURRENT WARNING");
Serial.println("");
delay(1000);}
}