title title title title

Arduino Tips

Not so obvious features...


The digital pins have built-in pull-ups resistors that you can use for items like buttons that you use to provide two-state inputs for your code. If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal 20K pull-up resistor ( during set up). Writing LOW will disable the pull-up. With this large a value for the pull-up, an attached LED, for instance, will only have about 200microamps of current available to light the LED. So this won't get the job done using the pin to monitor the input with the internal pull-ups applied. For that sort of circuit, use an external series resistor and set the pinMode as an OUTPUT on a separate pin. Also, there is a subtile issue with pin 13, since it has an internal pull-up and the onboard LED attached by default. This is not a good pin to use as an input. The analog pins have this pull-up feature as well. Now, the kicker is the Arduino has another mode for the pinMode function called INPUT_PULLUP that sets the internal pull-ups for said pin all in one statement. No digitalWrite to an input pin needed, which to my way of thinking, avoids all the confusion of the previous statements!

Also, note that when you use pinMode to set an OUTPUT, you leave the Arduino vulnerable to damage if you short out the pin or drive a load requiring greater than 40ma. The Arduino is a totem-pole output circuit, so it can source a maximum current of 40ma or sink a maximum of 40ma. To ensure or protect outputs that drive, say a transistor, it is advised to place a 470~1000 ohm resistor in series (base-emitter current needs to be limited). If you design the attached circuit to use up to 40ma, that's okay. To light an LED or similar load, just make sure you have a current limiting resistor in series so the LED gets its 20ma and the Arduino stays inside its ability to provide it.

Oh, this gets better! The Arduino automatically sets all pins to be OUTPUT by default, so you don't even have to call it out in the pinMode function, but don't do that. Always call out the mode of a pin or you will really confuse yourself and everyone else that reads your code. Saying that, analog pins are always inputs going to an A/D converter, so it's not necessary to define them with the pinMode() as INPUT.

If you call out a pin as an INPUT and fail to connect it, you could cause a problem, becasue the input impedance is extremely high, which is great when attaching a capacitive sensor for example, but left open, can pick up external noise. If your circuit is in an electrically noisy location, and you find the Arduino acting strange, you might have to pull-up unused input pins or designate them as OUTPUT. Most hobbyist are in a quiet room, so this is rarely an issue, but it's there nonetheless. The analog pins are almost guarenteed to go nuts if left open, especially with a long wire going to nothing.

Delay Function

The delay function has many good uses, but has one subtile issue - it pauses your program. Better known as "code blocking." This can cause an issue with other time dependent code that depends on continued looping. To eliminate the problem, you can use the millis() function and assign it to a variable and then watch when a threshold time is reached to do some action, but the program continues to run in the meantime.

Return to: Electronics Page

bottom strip