Controlling Arduino’s Pins

Print Friendly, PDF & Email

The Arduino Nano has a variety of analog and digital pins that are used to control other devices and circuits.

  • Analog input pins are A0, A1, A2, A3, A4, A5, A6, and A7. They provide 10 bits of resolution (0 to 1023) and measure the voltage applied to them in the range of 0 to 5 volts. The upper end of this range can be changed by applying a different voltage to the AREF pin.
  • Digital pins may be inputs or outputs. There are 14 of them in all. They operate between 0 and 5 volts and can provide or absorb a maximum current of 40 mA. Each pin has an internal pull-up resistor (disconnected by default) of somewhere around 20-50 KΩ. Some of the digital pins have special purposes:
    • Pins 0 (RX) and 1 (TX) are used to receive serial data and are connected to the USB to serial converter chip on the board.
    • Pins 2 and 3 can be configured to trigger an interrupt on a low value, rising or falling edge, or a change in value. Look at the attachInterrupt() function for details.
    • Pins 3, 5, 6, 9, 10, and 11 can be used to provide a PWM (pulse width modulation) output using the analogWrite() function.
    • Pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK) support SPI communication.
    • Pin 13 has a built-in LED connected to it that is on when the pin is high and off when the pin is low.

Based on what you are trying to read or control, choose a pin or pins with the right properties. Then, what happens at these pins is controlled by the software that is running in the Arduino.

Digital Output Example

To control something with a digital output pin, your program needs to include these statements:

  1. Within the setup() routine, specify the pin mode. the syntax is pinMode(pin, OUTPUT), where pin is the number of the digital pin.
  2. Then in your program, you can set the output high or low with the function digitalWrite(HIGH) or digitalWrite(LOW).

Suppose you want to control an LED attached (through a resistor) to pin 5, making it blink on and off twice per second:

void setup() {
pinMode(5, OUTPUT);
}
void loop() {
digitalWrite(5, HIGH);
delay(250);
digitalWrite(5, LOW);
delay(250);
}

Analog Output Example

Suppose you wish instead to make the LED fade on and off rather than blink. The analogWrite() function can be used for this. When using analogWrite() to a digital pin, the output of the pin is rapidly switched from the HIGH to LOW state at least 500 times per second. The fraction of time spent HIGH vs LOW is determined by the value passed to the routine from 0 to 255. If 0, the output pin is LOW all the time. If 255, the output pin is HIGH all the time. If 128, the output pin is HIGH 50% of the time. So the LED will actually be blinking on and off at a high rate of speed, but your eye sees a varying brightness.

int led = 11;
void setup() {
pinMode(led, OUTPUT); // not actually necessary for analogWrite
}
void loop() {
   // We will fade the LED on in steps of 5 over 3 seconds
   for (int i = 0; i <= 255; i += 5) {
      analogWrite(led,i);
      delay(60);
   }
   // Then fade the LED off in steps of 5 over 3 seconds
   for (int i = 255; i >= 0; i -= 5) {
      analogWrite(led,i);
      delay(60);
   }
}