> For the complete documentation index, see [llms.txt](https://monasheng.gitbook.io/zerotohero/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://monasheng.gitbook.io/zerotohero/arduino-zero-to-hero/core-skills/input-output-pins/digital-pins.md).

# Digital Pins

{% embed url="<https://www.youtube.com/watch?v=6PgfEO8RyaE&list=PLykjT-pf1P9yW_mVNREiExbah6LhwJttZ&index=12&t=1s>" %}

{% hint style="info" %}
:man\_mage: **Important:** Most MCUs possess a number of digital pins, capable of measuring and/or authoring digital (binary) voltages.

In this context, the word ***digital*** essentially means ***binary*** - i.e. `ON` or `OFF`, `HIGH` or `LOW`, `1` or `0`. We will use `HIGH` and `LOW` to describe the two binary states that MCU digital pins can take in this module.
{% endhint %}

## Logic Levels

MCUs vary in the way they assign binary states (i.e. `HIGH` or `LOW`) to voltages, depending on their built-in hardware. While there is much variety in this space, most MCU digital pins operate at one of two so-called 'logic levels':

1. Defining `LOW` as `GND` (i.e. $$0;\text{V}$$), and `HIGH` as $$3.3;\text{V}$$, or,
2. Defining `LOW` as `GND` (i.e. $$0;\text{V}$$), and `HIGH` as $$5;\text{V}$$.

It is important to know what logic level your MCU operates at - not least because you might be using the pin to drive other circuits. In terms of inputs, feeding in voltages higher than the MCU tolerance can lead to damage to a pin or the entire board!

{% hint style="info" %}
:man\_mage: **Important:** The Arduino Mega boards we use for this module utilise $$5;\text{V}$$ logic - i.e. they can accept up to $$5;\text{V}$$ as input and can produce $$5;\text{V}$$ signals as outputs.&#x20;

Many other boards, including some Arduino products, the Teensy line of MCUs, and many industrial-grade MCUs (e.g. STM32), utilise $$3.3;\text{V}$$ logic.
{% endhint %}

## Hardware

This module uses an [Arduino Mega](https://store.arduino.cc/products/arduino-mega-2560-rev3), which provides:

* 22 digital-only pins (labelled `D0-D21`) on the long-edge of the board with the button,
* 32 digital-only pins (labelled `D22-D53`) along the short-edge of the board without the USB, and,
* 16 'digital' pin functionalities (`D54-D69`, labelled `A0-A15`) via the multi-purpose analog pins on the long-edge of the board without the button,

for a total of 70 individual digital I/O pin functionalities.

Note that (at least with Arduino), there is no hardware difference between digital *inputs* and digital *outputs* - any digital pin is capable of reading or writing digital signals.

<figure><img src="/files/WslcZCsP4Db5ccoF0qV3" alt=""><figcaption><p>Arduino digital pins. Remixed from <a href="https://content.arduino.cc/assets/Pinout-Mega2560rev3_latest.pdf">https://content.arduino.cc/assets/Pinout-Mega2560rev3_latest.pdf</a> (CC-BY-SA).</p></figcaption></figure>

## Practical Considerations

### Input/Output Currents

MCU digital pins are only equipped to deliver/absorb (sometimes called *source* and *sink*) very small currents - sufficient to drive small LEDs, or utilise communication protocols.

The Arduino Mega, for instance, is capable of sourcing or sinking$$20;\text{mA}$$ per pin continuously. Many MCUs are only capable of $$10;\text{mA}$$ per pin.

{% hint style="danger" %}
:zap:**Warning:** Trying to drive large loads (e.g. motors, solenoids, contractors, etc., that can draw $$>1000;\text{mA}$$ of current) from an MCU digital pin will cause damage to pin, and likely to the board and it's power supply also.&#x20;

<mark style="background-color:red;">**DO NOT try to drive large currents directly from your Arduino, especially if it is being powered by your computer via USB. You might kill the board, your laptop, or both!**</mark>
{% endhint %}

It *is* possible to control large loads using an MCU - for such heavy-duty applications, see [Relays](/zerotohero/arduino-zero-to-hero/advanced-skills/relays.md).&#x20;

### Special Pins

Note that some digital pins have additional special/reserved duties - typically for communication protocols. These duties do not prohibit these pins from being used as 'normal' digital pins, but note that not every digital pin can perform every function - you may want to be strategic about which pins you use for which applications.

For instance, on Arduino, `PIN 1` and `PIN 0` are used for serial `TX` and `RX` communications, and can exhibit *weird* behaviour if you e.g. plan to use the USB serial monitor at the same time as reading or writing these pin states!
