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
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
In later labs we will use the
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.
|5V||Provides power for components (5 volts)|
|GND||Provides ground for components|
|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|
|13||Used by an LED soldered to the Arduino|