Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 37 ยท Digital I/O

pinMode and digitalWrite

Master digital outputs in Arduino: pinMode, digitalWrite, HIGH and LOW, sinking versus sourcing current, driving LEDs and transistors, and building patterns with arrays.

Digital I/O is the foundation of everything physical. A digital pin is either high or low, and by switching that level you can light LEDs, click relays, step motors and send data. This lesson covers the output side: pinMode() and digitalWrite(), what "HIGH" really means electrically, and how to drive several outputs without repeating yourself.

pinMode: choose a direction

C++
pinMode(pin, mode);

mode is one of three constants:

Mode Meaning
OUTPUT The sketch drives the pin high or low
INPUT The pin listens; it floats if nothing is connected
INPUT_PULLUP A listening pin with an internal resistor to 5 V (see Pull-Up Resistors)

All pins start as inputs after reset. Call pinMode() once per pin, normally in setup().

digitalWrite: set the level

C++
digitalWrite(pin, HIGH);   // 5 V on an Uno, 3.3 V on 3.3 V boards
digitalWrite(pin, LOW);    // 0 V

HIGH is defined as 1 and LOW as 0, so digitalWrite(pin, 1) also works, but the names are clearer. A pin stays at the level you wrote until you write again; the board does not need to be told repeatedly.

You can also read back the state of an output with digitalRead(), and flip it:

C++
digitalWrite(ledPin, !digitalRead(ledPin));   // toggle

(On AVR boards, writing to the input register PINx also toggles, but that is a low-level trick for later.)

Sourcing and sinking

An output pin can do two jobs. It can source current: the pin is the positive supply (HIGH) and current flows out of it through the load to GND. Or it can sink current: the load connects to +5 V, and the pin provides the path to ground (LOW).

flowchart LR
    subgraph Sourcing
    A["Pin HIGH"] --> B["Resistor"] --> C["LED"] --> D["GND"]
    end
    subgraph Sinking
    E["+5 V"] --> F["Resistor"] --> G["LED"] --> H["Pin LOW"]
    end

Figure: sourcing lights the LED when the pin is HIGH; sinking lights it when the pin is LOW.

With sinking wiring the logic is inverted: LOW turns the LED on. The AVR chip handles both at similar strength. Remember the limit of about 20 mA per pin from Pinout and Power Pins.

C Stand-in: active-high versus active-low LEDs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Driving bigger loads

A pin can switch a transistor, and the transistor switches a bigger current from another supply. Typical read-only setup: pin through a 1 kฮฉ resistor to the base of an NPN transistor (such as a 2N2222 or BC547), emitter to GND, load between +V and the collector. For motors and relays add a flyback diode across the coil to absorb the voltage spike when it switches off. Logic-level MOSFETs (such as the IRLZ44N) are the modern alternative when currents reach several amperes.

C++
const int motorPin = 5;
void setup() { pinMode(motorPin, OUTPUT); }
void loop() {
  digitalWrite(motorPin, HIGH);  // transistor on, motor runs
  delay(2000);
  digitalWrite(motorPin, LOW);   // transistor off
  delay(2000);
}

Many outputs: arrays and loops

Repeating pinMode and digitalWrite for eight LEDs is tedious. Put the pins in an array and loop. This "chaser" lights one LED at a time, shown read-only:

C++
const byte leds[] = {2, 3, 4, 5, 6, 7, 8, 9};
const byte count = sizeof(leds) / sizeof(leds[0]);

void setup() {
  for (byte i = 0; i < count; i++) pinMode(leds[i], OUTPUT);
}

void loop() {
  for (byte i = 0; i < count; i++) {
    digitalWrite(leds[i], HIGH);
    delay(80);
    digitalWrite(leds[i], LOW);
  }
}

The stand-in prints the same pattern as text so you can see the logic:

C Stand-in: an 8-LED chaser, drawn as text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Going back and forth (a "Cylon" or "Knight Rider" sweep) needs the loop to run down again from count - 2 to 1. Try writing that as an exercise.

C Stand-in: a binary counter on 4 LEDs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The >> and & operators extract each bit; on hardware you would pass that bit to digitalWrite() for the matching pin.

Common mistakes

  • Forgetting pinMode(pin, OUTPUT): the LED glows faintly or not at all.
  • Wiring an LED without a resistor.
  • Using pins 0 and 1 while the Serial Monitor is open.
  • Mixing up the LED's polarity.
  • Writing = instead of == in conditions (if (x = HIGH) is always true).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the state of every pin right after reset?

    Pins start as inputs, so call pinMode(pin, OUTPUT) before driving a load.
  2. 2. An LED is connected between +5 V (through a resistor) and the pin. When does it light?

    This is sinking wiring: the pin must be LOW to let current flow to ground.
  3. 3. Why use a transistor between a pin and a motor?

    A pin supplies about 20 mA; the transistor switches a bigger current from a separate supply.
  4. 4. Which expression turns on the LED only for bit 2 of n?

    n & 4 is nonzero only when bit 2 (value 4) is set.

Summary

  • pinMode(pin, OUTPUT) makes a pin drive; digitalWrite(pin, HIGH/LOW) sets it.
  • LEDs can be wired to source or sink current; sinking inverts the logic.
  • A pin handles about 20 mA; use transistors or MOSFETs for larger loads.
  • Arrays and loops keep multi-output sketches short.

Next: digitalRead and Buttons