Overview
This article highlights that Arduino UNO R4 WiFi digital IO read and write operations are slow using Arduino programming’s built in native digitalWrite() and digitalRead() functions. In addition, a solution is provided that allows faster reading of Digital I/O read/writing.
Arduino Digital I/O Read/Write Functions
In Arduino programming, the digital I/O (input/output) functions are used to interface with digital pins on an Arduino board. These pins can be configured to either read signals from external devices (input) or write signals to external devices (output). The digitalWrite(pin, value) function is used to set the state of a digital pin to either HIGH or LOW. The digitalRead(pin) function is used to read the current state (HIGH or LOW) of a digital pin configured as an input.
Slow Arduino UNO R4 WiFi Digital I/O Read/Writes
We found that for Arduino UNO R4 WiFi, the Arduino programming’s native functions for Digital I/O read/write are extremely slow. Both digitalRead() and digitalWrite() functions are blocking, therefore, the execution of the program pauses for until read/write operation is completed.
Function | Execution time |
digitalRead() | 1170ns |
digitalWrite(pin#, LOW) | 752ns |
digitalWrite(pin#, HIGH) | 877ns |
Fast Arduino UNO R4 WiFi Digital I/O Read/Writes
On Arduino UNO R4 WiFi you can directly read/write digital I/O’s on pin registers. We found reading/writing directly to pin registers method to be in general extremely faster than Arduino programming’s native digitalRead() and digitalWrite() functions.
Function | Execution time |
Reg pin read operation | 208ns |
Reg pin LOW write operation | 83ns |
Reg pin HIGH write operation | 83ns |
Note that register based digital IO read/writes doesn’t require pin to be configured as input or output.
Source Code
Register based reading writing example source code