What are the fastest alternatives to calling the Arduino functions digitalread()
and digitalwrite()
? AVR-specific or chip-specific solutions are acceptable.

- 10,419
- 27
- 68
- 95
-
4Reading this and your last question, it seems like you want to do something pretty quickly. I think giving details on what it is and how fast might be useful. – Oli Glaser Nov 22 '11 at 01:16
-
@Oli, good idea, thanks. Current project is a tuning system for RC transmitters. It works well, but I would like to make the RC PWM signal decoding efficient so that I can use the same framework for other projects. Here's a writeup on what I have so far: http://eastbay-rc.blogspot.com/2011/11/arduino-sketch-for-transmitter-tuning.html – Mark Harrison Nov 27 '11 at 02:17
5 Answers
Access the digital ports directly!
The 3 methods I tested were
- digitalWrite(pin, LOW); digitalWrite(pin, HIGH);
- CLR(PORTB, 0) ; SET(PORTB, 0);
- PORTB |= _BV(0); PORTB &= ~(_BV(0));
[...]
As you can see, digitalWrite takes around 56 cycles to complete, while direct Port addressing takes 2 cycles. That’s a big difference in time for programs that have lot’s of IO operations!
-
You can use preprocessor macros to translate the pin to port and bit: `*portOutputRegister(digitalPinToPort(pin)) |= digitalPinToBitMask(pin)` // from https://github.com/arduino/ArduinoCore-avr/blob/9f8d27f09f3bbd1da1374b5549a82bda55d45d44/cores/arduino/Arduino.h#L178 – Dave X Jan 31 '22 at 23:25
Folks at Arduino.SE have already discussed and tested this. As it turns out, digitalWriteFast()
is not much of improvement over regular digitalWrite()
. Direct port access, however, is about 35-40 times faster than digitalWrite()
.

- 25,576
- 5
- 45
- 106
As suggested above, access the digital ports directly. But with style!
By writing hard-coded values directly into the hardware registers, you loose in readability and portability.
I've published on Github a tool I called HWA that lets you use an object-oriented interface to the hardware that does not require a C++ compiler and produces high efficiency binary code.
HWA is there: https://github.com/duparq/hwa

- 21
- 2
-
1"high efficiency binary code" would sound much more authoritative along with actual measurements. – Dmitry Grigoryev Feb 15 '18 at 08:49
Use the ChipKit Uno32. It's much faster than the AVR-based Arduinos. It will also deal with your timing problems.

- 38,774
- 2
- 60
- 96
-
1I don't think buying faster hardware is the best answer here, at least not until you've reached the bounds of your current hardware and decided that you need something faster. – Jon L Nov 22 '11 at 00:42
-
1Why the downvotes? He wanted the *fastest* solution, not just a faster one, which I've provided. Can anyone suggest a faster Arduino-based solution? – Leon Heller Nov 22 '11 at 01:51
-
12Because the ChipKit is not an arduino, but an arduino compatible platform. This means that although port switching is faster , there is a big likelihood of a rewrite of some libraries. Ethernet, XBee, SD card libraries may not work at all out of the box. Furthermore, Mark specifically asks for alternative function calls of digitalRead/Write, not for a new platform (doh). – Hans Nov 22 '11 at 08:43
-
No, it's a fine answer... I didn't realize such a thing existed, and at such a price point. Downvoters, follow my lead and upvote... Thanks Leon! – Mark Harrison Nov 22 '11 at 18:17
-
-
2Changing hardware is never a good solution. At the very least you miss out on some good learning and will never learn what would be efficient use of the hardware. – Rick_2047 Nov 25 '11 at 13:39
-
1@Rick_2047 I wouldn't say "never" - if the requirement is new or expanded or the quantity increases, it can make sense to look at alternatives. But I agree that blindly throwing hardware at gross software inefficiencies is unsophisticated - sometimes it doesn't even work as higher performance systems can add overhead of their own and sometimes end up slower than simpler ones for some tasks. There's really no substitute for learning about the platform you use or contemplate using. – Chris Stratton Nov 25 '11 at 15:30
-
Upvoted. Reason is that 'Arduino' isn't just the hardware-series, but also the application environment. The Arduino hardware actually has specific names s.a. 'Duemillanove', 'Neo', 'LilyPad', 'Nano', 'NanoPro' etc. Quite often people searching for answers on 'Arduino' are approaching it from a software compatibility (and it's ease) standpoint, which the ChipKit solution does seem address. The icing on the cake seems to be the compatibility with many shields. Like Mark, I wasn't aware of this, and glad I found it here. – bdutta74 Nov 27 '11 at 08:15