Highly Rated/Popular Library for fast digitalWrite()

Hi all,

I am looking for some recommendations for a well-maintained or popular library for replacing the default digitalWrite() and analogWrite() Arduino functions. My current implementation requires as fast as possible switching, without using direct port manipulations

Could anyone point me in the right direction?

Thank you!
-Isaac

IDE, Library Manager, digitalWriteFast. Using it, quite happy.
May or may not work/be available on your unstated MCU.

1 Like

My apologies for missing this piece of information! I am currently using an Arduino Nano Every, but am switching to a Arduino Due soon.

If you use MegaCoreX as the board package with the IDE instead of the megaavr package you can use the built in fast io functions.

For timing critical applications the standard digitalRead() and digitalWrite() functions may be too slow. To solve this, MegaCoreX also includes some improved variants that compiles down to a single instruction. Call digitalReadFast(myPin) or digitalWriteFast(mypin, state) to use these.
Note that the pin number and pin state has to be known at compile time!

You can run the board at 20MHz as well.

2 Likes

Sorry for the late reply Delta. I measured that the nano every runs the digitalWrite() functions at ~4 us, while the port set was ~0.44 us, so nearly a 10x slowdown to use digitalWrite(). I don't have a set number at the moment, but will be running a real time system, and so am hoping to optimise wherever I can!

Thank you! I was able to luckily get the system to run a workaround to still be able to utilise the port manipulations that were previously giving me errors. This was the next solution!

LOL no worries, this isn't an optimising a button type situation here :stuck_out_tongue:
For safety reasons for the system I am building, we have a separate system that will utilise my Arduino code as an API to interact with a large machine. In the case that something happens, we want to be able to control the system as fast as possible to limit as many potential situations as possible!
Thank you for your help, I appreciate the conversation we have had!

Hope you have a great week ahead,
-Isaac

Hey Delta,

I appreciate your passion about the speed of these functions here. I am not going to fight you on this, or try to explain my entire system to you, and why I may or may not need this level of speed. I appreciate your enthusiasm, but I do hope that you understand that some systems will require this level of speed, although not the large majority of users on this forum.
Lets leave this discussion here, as my question has been answered and I have found my solution.

Hope your day goes great,
-Isaac

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.