Real-time plotting of an input in MATLAB

Hi,

I control a PWM output pin of Arduino Uno, and I want to see the voltage change in real-time. I connected D11 and A1 for this purpose, and I used

writePWMVoltage(a,'D11',v);
readVoltage(a,'A0');

commands for writing and reading. I can obtain the voltage values, but I couldn't plot it.

Btw, I want to update the voltage value in every 1/128 seconds (128Hz), if possible.

Thanks in advance.

'D11' ? 'A0' ?

AWOL: 'D11' ? 'A0' ?

Yes, as PWM output I used D11, and A0 as input.

Well, there seems to be a whole lot of stuff missing here.

giving a 128Hz execution frequency to your loop() is easy:

1s/128 = 7.813ms = 7813us which is the period of your loop()

//give a 128Hz loop frequency
while(micros() - loop_timer < 7813);            //wait until 7813us are passed.
loop_timer = micros();                          //then reset the loop timer

Just insert this at the end of your loop() function.

For the plotting issue, we would need to see more code I think.

Matt

giving a 128Hz execution frequency to your loop() is easy:

...except that doesn't account for the time that analogRead or the serial I/O takes.

I'm still interested in 'D11' and 'A0'.

I don't know whether my approach is correct or not. :roll_eyes: I wanted to see the change of PWM output of my Arduino, which is 'D11' for this case, as we see on oscilloscopes. I change the PWM voltage randomly and I want to see this change. So, I connected 'D11' and 'A0', and by using readVoltage command I want to obtain the related voltage value.

I don't know whether my approach is correct or not.

Neither do we, because we can't see your code.

I would guess from the title that the OP is using MATLAB in conjunction with an Arduino, but unfortunately, he/she has not read the "How to use this forum" post, and thus has not yet learned how to ask a sensible question on this forum

Here is my code:

global a;
a = arduino;

i=1;
v=[];
readV= [];
while (1)
    v(i) = abs(randn);
    writePWMVoltage(a,'D11',v(i));
    readV(i) = readVoltage(a,'A0');
    pause(1/128);
    plot(readV)
    i=i+1;
    
end

Now, I can plot variable readV, but not correct.

The first 10 elements of v is:

v(1:10)

ans =

    0.5377    1.8339    2.2588    0.8622    0.3188    1.3077    0.4336    0.3426    3.5784    2.7694

and readV is:

readV(1:10)

ans =

         0         0         0         0         0         0         0         0    4.9902         0

I'm doing something similar.

figure
h1 = animatedline('Color','Black');
h2 = animatedline('Color','Blue');
ax = gca;
ax.XLabel.String = 'Time elapsed (s)';
ax.YLabel.String = 'Signal (V)';
% I'm plotting two things at once

j = 1;
te = 0; % time elapsed since starting
tic % start counting

% to stop taking data and save, press any key in the figure window
set(gcf,'currentchar',' ')

while get(gcf,'currentchar')==' '
    A0reading = readVoltage(a,'A0');
    
    te = toc; % time elapsed
    
    yourData(j,:) = [te,A0reading];
    j = j + 1;
    
    fprintf('At t = %.1f sec, A0 = %3.3f V\n',te,A0reading);
    
    addpoints(h1,te,A0);
    addpoints(h2,te,[]); % take this line out if you want to plot one thing
    drawnow
    
    pause(2)
end

AWOL: ...except that doesn't account for the time that analogRead or the serial I/O takes.

It doesn't need to account it, whatever happens in the loop doesn't affect that 'timer', the only thing to pay attention to is that the loop itself doesn't take more than the period time to be executed.

In that case, if his loop takes 7ms to be executed, then the time left will be spent by the two lines of code I posted and then the loop will start again.