Here is what I get with a Teensy4.1. There is quite a variability in the digitalRead. Even more so at the begining of the reading sequence.
===> All possible serial ports: /dev/cu.Bluetooth-Incoming-Port /dev/cu.usbmodem124098601 /dev/tty.Bluetooth-Incoming-Port /dev/tty.usbmodem124098601
===> Your specified port /dev/cu.usbmodem124098601 is present
===> Your specified port /dev/cu.usbmodem124098601 is available
===> It took 0.016 secs to establish response: 0...
===> Basic Analog and Digital I/O (adio.ino) sketch detected !
===> Arduino successfully connected to port: /dev/cu.usbmodem124098601!
>> tic;val=s.digitalRead(2);disp(['Value was: ' num2str(val)]);toc
Value was: 0
Elapsed time is 0.011491 seconds.
>> tic;val=s.digitalRead(2);disp(['Value was: ' num2str(val)]);toc
Value was: 0
Elapsed time is 0.002888 seconds.
>> tic;val=s.digitalRead(2);disp(['Value was: ' num2str(val)]);toc
Value was: 0
Elapsed time is 0.010729 seconds.
>> tic;val=s.digitalRead(2);disp(['Value was: ' num2str(val)]);toc
Value was: 0
Elapsed time is 0.005121 seconds.
For 10000 iterations of digitalRead:
b = [];
pause(1)
for i = 1:10000
tic;
s.digitalRead(2);
b(i) = toc;
end
>> mean(b)
ans =
0.001013402959300
However, digitalWrite is way faster:
a = [];
pause(1)
for i = 1:10000
tic;
s.digitalWrite(14,mod(i,2));
a(i) = toc;
end
>> sum(a)
ans =
0.432772365000000
>> mean(a)
ans =
4.327723649999997e-05
Even then, though, the first writes of the sequence are orders of magnitude slower (ie, the first write is 6.074 ms).
With an Arduino Uno:
===> All possible serial ports: /dev/cu.Bluetooth-Incoming-Port /dev/cu.usbmodem101 /dev/tty.Bluetooth-Incoming-Port /dev/tty.usbmodem101
===> Your specified port /dev/cu.usbmodem101 is present
===> Your specified port /dev/cu.usbmodem101 is available
===> It took 0.531 secs to establish response: 0...
===> Basic Analog and Digital I/O (adio.ino) sketch detected !
===> Arduino successfully connected to port: /dev/cu.usbmodem101!
For 10000 iterations of digitalRead, we can see there is indeed, a problem.
b = [];
pause(1)
for i = 1:10000
tic;
s.digitalRead(2);
b(i) = toc;
end
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
Warning: arduinoIOPort.digitalRead() was empty
> In arduinoIOPort/digitalRead (line 237)
>> sum(b)
ans =
8.888205034199996e+02
(cannot post the plot, limited by the forum)
However, digitalWrite is still way faster:
>> a = [];
pause(1)
for i = 1:10000
tic;
s.digitalWrite(3,mod(i,2));
a(i) = toc;
end
>> mean(a)
ans =
2.542173692000001e-04
Although, it is not pretty either.
This is with the original USB-B to USB-A long cable provided with Arduino Uno with the USB-A to C adapter so, this might be a cause.
Of note, these tests are conducted on my own personnal machine (MacBook Pro M1 Max 2021) and not the one I plan to use for the experiment. So, it runs Matlab through Rosetta. The timings are thus, of course, not too good nor reliable. Nevertheless, the question of why the digitalReads are so inconsistent remains. Maybe there is something wrong with my Uno since the Teensy reading are so much better. However, the reason why the first readings of the sequence are slow eludes me. It is as if the digitalRead should be initialized or something.