APS

Subscription Management to Save Your Time and Money!

Arduino Uno and Nano I/O pin CheatSheet

Written By: Mel Lester Jr. - Jan• 02•16

Arduino compatible Uno and Nano micro controllers are my go-to development boards for electronic projects.  Except for the Nano’s smaller form factor, they have very similar specifications including 14 Digital I/O pins. The Nano, despite being more compact, has 8 Analog Input pins,  two more than the Uno’s 6.

Digital vs. “Analog”

All of the pins (D0-D13 and A0-A7) may be used for Digital I/O.  What differentiates the so called Analog pins from their Digital counterparts is that the Analog pins are connected to a 10 bit Analog to Digital Converter (ADC) that maps a 0 to 5V DC input voltage to an Integer between 0 and  1023.  INPUT is the default mode for Arduino pins so while there is no requirement for an Arduino program to have pinMode(pin, INPUT) in the setup() code block, your Sketch tends to be more self documenting if you explicitly declare all of your INPUT as well as OUTPUT pin assignments.

As a matter of convention, I tend to not connect Analog pin 0 (A0) to a circuit, but use it to seed the Random Number Generator (RNG).  The voltage of an unattached Analog pin should float and an arbitrary call to randomSeed(analogRead(A0)) in the setup() code block does the job nicely.

I also try to reserve Analog pins A4 and A5 for connecting I²C peripherals.  Multiple devices can be daisy chained using this two-wire protocol and there are quite a number of interesting and inexpensive sensors and products like Real Time Clocks (RTC) to choose from.

Pseudo Analog output using Digital PWM

There is no corresponding Arduino Digital to Analog Converter (DAC) for analog output on the Uno or Nano.  However calling analogWrite(pin, byte) with Digital pins 3,5,6,9,10, or 11 will produce what appears to be a graduated 0 to 5V output through the magic of Pulse Width Modulation (PWM).  PWM will send a square wave at a 0 to 100% duty cycle using byte values from 0 to 255 to the appropriate pin until another instance of analogWrite(), digitalRead() or digitalWrite() is made on the same pin.  Unlike digitalWrite(), it is not necessary to initialize a PWM pin for OUTPUT by making a call to pinMode() before using analogWrite().

PWM Considerations

The six PWM pins have significantly different properties, making them suitable for a variety of specialized tasks.  Pin 3, for example, is capable of handling hardware interrupts.  Because there is only one other pin that can be used for interrupt handling, pin 3 should be selected for PWM output only as a last resort.

PWM pins 5 and 6 run at about 1kHz, which is about twice the frequency of the four other PWM pins, 3 and 9-11.  The higher frequency of PWM pins 5 and 6 make them most suitable for controlling the speed of brushed DC gear motors to run quieter and smoother.  However, at near stall speed, even pins 5 and 6 will audibly whine at their 1kHz frequency, which is well within the threshold of human hearing.  While it is possible to raise the frequency of any PWM pin to over 20kHz, which should be inaudible, doing so is an advanced topic and raises the possibility of unexpected timing related side effects elsewhere that could be difficult to diagnose and resolve.

Even though PWM pins 9 and 10 operate at a lower frequency, they can be used to control the speeds of DC brushed gear motors, albeit the audible whining may be more noticeable than when using PWM pins 5 and 6.  Again, the frequency can be adjusted, but with the caveat that timing related side effects may be introduced into your Sketch.  In Sketches where the Arduino servo library is used, PWM is disabled on pins 9 and 10 even if they are not connected to a servo.

Inductive loads, like those associated with motor and relay circuits, should be avoided when assigning workloads to PWM 11.  While Electric and Magnetic Fields (EMF), cross-talk and other forms of signal interference are a concern for all electronic projects, pin 11 could be especially susceptible when the design includes an ISCP header.

Non PWM Digital I/O pins

Serial Communications

Digital pins 0 and 1 should be reserved for Serial RX and TX communications.  This is used for programming Arduino Uno and Nano compatible micro controller boards, communicating with PCs and other Serial I/O devices and as a tool for debugging Sketches.

Hardware Interrupts

As mentioned earlier in the PWM discussion, the only two pins that support hardware interrupts are Digital pins 2 and 3, which also has PWM attributes.  As Sketches become more complicated, the need for utilizing these interrupts will arise.

Primary Digital I/O pins

Digital pins 4, 7 and 8 are the go to pins for Digital I/O as they have no other significant roles or limitations. Pin 12, with the exception of being unsuitable for low impedance loads when using an active ICSP header, is also a good choice for Digital I/O

Lucky 13

Last but not least, Digital pin 13 is the extrovert of the Digital I/O pins, sporting a connection to an on board LED through a serial resistor.  While this is handy for testing the functionality of a Uno or Nano without any external circuitry as demonstrated by the Basic Blink Sketch, pin 13 should be limited to Digital Output in production Sketches.  Trying to enable the internal 20k pull-up resistor for input will usually result in an observed voltage of around 1.7V, or Digital LOW, as opposed to the expected 5V, or Digital HIGH, due to the on-board LED and it’s resistor dropping the voltage.  If you have no other option than trying to use pin 13 for Digital Input use pinMode(13, INPUT) and an external, pull-down resistor in your circuit.  Also, like Digital pins 11 and 12, avoid using Digital pin 13 with low impedance loads when employing an active ICSP header.

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.