How to read analog data instantaneously from Arduino to MATLAB?

15 views (last 30 days)
I have been developing a code where i want to read analog values from ARDUINO to MATLAB instantaneously and i want to create a heat map where the analog values should fill the matrix and display the suitable color for the matrix..I have been trying to develop a 4*4 grid.
The issue i'm facing here is cannot read analog value instantaneously from arduino to matlab instant im getting only one value and in heat map i cannot read values...any suggestions or guidance if i could read analog value and fill the analog value into the matrix which reflects the particular color for the obtained value. Thank you!.
Here is my code which i have developed in MATLAB
% code for analog value read
clc; clear all; close all;
a = arduino;
readVoltage(a,'A0');
% code for heatmap
rand('seed',10000);
A=rand(4,4);
imagesc(A);
axis equal tight.

Accepted Answer

Dhruv
Dhruv on 20 Feb 2023
Making use of loop can help continuously read analog values from Arduino to MATLAB. Below mentioned is an example code to continuously read analog values from one pin (A0) of Arduino and store them in a MATLAB matrix:
% Create an arduino object
a = arduino();
% Define the pin to read from
pin = 'A0';
% Define the number of readings to store e.g. 100
numReadings = 100;
% Define the delay between readings (in seconds)
delay = 0.1;
% Initialize the data matrix
dataMatrix = zeros(numReadings, 1);
% Start reading analog values and storing them in the data matrix
for i = 1:numReadings
% Read the analog value from the pin
analogValue = readVoltage(a, pin);
% Store the analog value in the data matrix
dataMatrix(i) = analogValue;
% Pause for the specified delay
pause(delay);
end

More Answers (0)

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!