Deep Dive: I/O Pins
In the example above we hooked up an LED to pin 2 and a switch to pin 3. But why did we use these pins? Could we have used different pins? How do we hook up more components? There are many I/O pins that can be used on your Arduino, but, confusingly, they are not all interchangeable. When you are connecting components to your Arduino you must select pins that support the functions you want to use with them.
When we connected an LED to our Arduino we set the pin to OUTPUT
mode and used digitalWrite()
to turn it on and off. We used pin 2, but if we look at the table below we can see that digitalWrite()
is supported by pins 2 through 12 and A0 through A5.
Similarly, when we connected the switch we set the pin to INPUT_PULLUP
mode and used digitalRead()
to read its state. We used pin 3, but if we look at the table below we can see that digitalRead()
is supported by the same pins as digitalWrite()
.
If we wanted to hook up five LEDs and two switches we could connect the LEDs to pins 2 through 6 and the switches to pins 7 and 8. Or we could connect the switches to pins 2 and 3 and the LEDs to pins 4 through 8. Or we could connect the LEDs to pins 2, 4, 6, 8, and 10 and the switches to pins A0 and A5. Any of these configurations are valid, as long as you make sure you use the corresponding pin name in your program when calling pinMode()
, digitalRead()
, and digitalWrite()
.
In later labs we will use the analogRead()
and analogWrite()
functions, which are much more restrictive about which pins they can be used with. Consult the table below to ensure you are using the right pins for the job!
If you do not see a pin listed here (like 3.3V, VIN, IOREF, and AREF) you should not use it.
Pin | digitalRead() |
digitalWrite() |
analogRead() |
analogWrite() |
Description |
---|---|---|---|---|---|
5V | Provides power for components (5 volts) | ||||
GND | Provides ground for components | ||||
A0 | X | X | X | ||
A1 | X | X | X | ||
A2 | X | X | X | ||
A3 | X | X | X | ||
A4 | X | X | X | Also used for “SDA” connection on LCD | |
A5 | X | X | X | Also used for “SCL” connection on LCD | |
0 | Used by the USB serial connection | ||||
1 | Used by the USB serial connection | ||||
2 | X | X | |||
3 | X | X | X | ||
4 | X | X | |||
5 | X | X | X | ||
6 | X | X | X | ||
7 | X | X | |||
8 | X | X | |||
9 | X | X | X | ||
10 | X | X | X | ||
11 | X | X | X | ||
12 | X | X | |||
13 | Used by an LED soldered to the Arduino |