INA219 Current Sensor Integration with MATLAB

26 Ansichten (letzte 30 Tage)
Jayden  Hasemann
Jayden Hasemann am 10 Sep. 2017
Beantwortet: David Fournier am 5 Nov. 2024 um 12:02
Hi There,
I am trying to use a INA219 current sensor to obtain the voltage and current and thus power from a Solar Panel. This chip works with an arduino. I have coded in arduino and got the values i require however when I try to read these values on MATLAB I cannot get it to work.
The Code that I have written is Here:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clc clear all
a = arduino;
INA219 = '0x40';
INA219_ADC_CH0 = '40'; % getBusVoltage_V
INA219_ADC_CH1 = '41'; % getVoltage_mV
INA219_ADC_CH3 = '43'; % getCurrent_mA
i2c = i2cdev(a,INA219);
disp(['getBusVoltage_V ', 'getVoltage_mV', 'getCurrent_mA']);
while 1
getBusVoltage_V = read_adc(i2c,hex2dec(INA219_ADC_CH0));
pause(0.5);
getVoltage_mV = read_adc(i2c,hex2dec(INA219_ADC_CH1));
pause(0.5);
getCurrent_mA = read_adc(i2c,hex2dec(INA219_ADC_CH3));
pause(0.5);
disp([getBusVoltage_V,getVoltage_mV,getCurrent_mA]);
end
function adc = read_adc(dev,config)
write(dev,config);
read(dev, 3);
out = read(dev, 3);
adc = out;
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Where the output is:
60 0 0
31 3 0
255 255 255.
I was expecting an output of roughly:
2.46 0.03 0.07
*obtained from my arduino code.
I cannot seem to find where I've gone wrong, perhaps my limited knowledge in the area. Any help you could provide to rectify this would be great.
Regards,
Jayden
  3 Kommentare
Raul Ballesteros
Raul Ballesteros am 28 Mai 2019
I have the same problem If find the solution Please share the vos es Thanks
Gongyan Du
Gongyan Du am 18 Mai 2020
I have the same problem

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Gongyan Du
Gongyan Du am 18 Mai 2020
Have you solved this problem, can you share it?

David Fournier
David Fournier am 5 Nov. 2024 um 12:02
My solution to this problem was to program the arduino to output the values from the INA219 in a comma separated format on the serial port, then just read them from the serial port in MATLAB. If anyone has the solution to just do this in MATLAB, please post!
Arduino code snippet
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getPower_mW();
loadvoltage = busvoltage + (shuntvoltage / 1000);
Serial.print(busvoltage); Serial.print(",");
Serial.print(shuntvoltage);Serial.print(",");
Serial.print(loadvoltage);Serial.print(",");
Serial.print(current_mA);Serial.print(",");
Serial.println(power_mW);
delay(1000);
MATLAB App code snippet
methods (Access = private)
function results = serial(app)
% Define the serial port and baud rate
port = "COM6"; % Change this to your Arduino's port
baudRate = 9600;
% Create a serialport object
app.arduinoObj = serialport(port, baudRate);
% Configure the terminator to match the Arduino's serial output
configureTerminator(app.arduinoObj, "LF");
% Flush any old data
flush(app.arduinoObj);
end
function results = plot(app)
% Read line of data
line = readline(app.arduinoObj);
% Split the line into multiple values
values = str2double(split(line, ','));
% Append data
app.data = [app.data; values']; % you definitely need the apostrophe here!
app.time = [app.time; datetime("now")];
disp (app.data); % This line just prints data to the workspace for troubleshooting
% Plots data to an Axes figure called UIAxes
plot (app.UIAxes, app.time, app.data);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by