Depending on your Arduino board, you might have to search on Google for “Arduino X pin mapping”. Here is the pin mapping for Atmega168 and Atmega328 Arduino boards. Instead of using the digitalWrite() function, we’ll directly manipulate the port to change a pin’s state. Let’s now try to optimize this function so it takes much less time. You can skip all the steps without any problem. ![]() In the following we’ll basically skip all those steps and directly change the corresponding port to the state we want.Īs long as you don’t use PWM on digital pins, using digitalWrite() is kind of overkill. That’s a lot of steps for just changing the pin’s state. Check the current pin’s state, and change the state accordingly.Find the port associated to the pin number.Check if there is a PWM running for this pin.Check if the pin exist, and if not, return.Really quickly, the digitalWrite() will (not necessarily in the code’s order): If you want to check the full code, it’s available on Github. The digitalWrite() function does a lot of things. micros() can only be precise up to 4 microseconds, so that’s another good reason to compute an average, so we can have a much better precision.ģ.40 microseconds can be quite a long time to just change one pin’s state. Note: here we used micros() to get the time. We have the answer: a digitalWrite() will take 3.40 microseconds to execute on an Arduino Uno board. So, if you run this code, and open the Serial Monitor, you’ll get something like this (on Arduino Uno): Total duration: 3396 µs To get more details and explanations about this code, check out How to compute a duration with Arduino. This code will print the total duration required to execute 1000 digitalWrite(), and then print the average duration for only one digitalWrite(). Serial.print("Duration for one digitalWrite(): ") Unsigned long totalDuration = timeEnd - timeBegin ĭouble duration = (double)totalDuration / 1000.0 It’s often better to take a large sample instead of just measuring only one action. In order to get an accurate measurement, we’ll execute digitalWrite() 1000 times and then divide the result by 1000. Then we’ll be able to improve it, and measure it again to see how much time we save. ![]() So, let’s see how much time the digitalWrite() takes. Optimizing stuff is cool, but if you don’t know what you’re optimizing, and by how much, then there’s no point. ![]() Why Arduino digitalWrite() is not fast Measuring digitalWrite() speed Just use the tips when you really need them. Applying the tips on all your Arduino projects though is not something I would recommend. Note that even if you don’t need to optimize your code, reading this tutorial can still be useful to understand a few things about Arduino. I only recommend products that I personally use.) (disclaimer: I earn a commission if you make a puchase using this link. So, be sure you actually need to optimize before you do.Ĭlick here to get the recommended Arduino Kit to start with this tutorial. If your application consists of switching a digital pin every 100 ms, and if you don’t have any real time constraints, then don’t bother optimizing, you’ll just waste your time.Īs Donald Knuth said: “Premature optimization is the root of all evil.” When you should optimize (and when you shouldn’t)īefore going any further, I want to emphasize this: don’t try to optimize a code too soon, especially if there’s nothing wrong with it.
0 Comments
Leave a Reply. |