Skip to content

Commit 836e6f9

Browse files
authored
Merge pull request #1311 from arduino/karlsoderby/esp32-pin-tutorial
[Nano ESP32] Pin Configuration
2 parents 5a84641 + fc7f9f5 commit 836e6f9

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
240 KB
Loading
960 KB
Loading
513 KB
Loading
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: Nano ESP32 Selecting Pin Configuration
3+
description: Learn how to switch between default & ESP32 pin configurations when programming your board.
4+
author: Karl Söderby
5+
hardware:
6+
- hardware/03.nano/boards/nano-esp32
7+
tags: [ESP32, Pin Configuration]
8+
---
9+
10+
## Overview
11+
12+
The [Arduino Nano ESP32](https://store.arduino.cc/nano-esp32) is a Nano form factor board based on an ESP32-S3 SoC. This board is part of the [Arduino Nano Family](https://store.arduino.cc/pages/nano-family), and follows the same pinout as all Nano boards. This is very convenient if you want to port a project from another Nano board, as you can preserve the same wiring and pin numbers in the code.
13+
14+
This means that if you want to control a pin you can simply use the pin number that is printed on the board itself:
15+
16+
```arduino
17+
// This will enable the pin marked with "D8" on the board:
18+
digitalWrite(8, HIGH);
19+
```
20+
21+
### Compatibility Mode
22+
23+
However, some libraries previously written for ESP32 boards (to name a few: OneWire, FastLED, ESP32Servo) don't support the pin numbers printed on the board and require you to refer to the internal microcontroller's GPIO number instead. For instance, to refer to the pin labelled "D8" on the board such libraries expect you to write `17` in your code as that's the GPIO number corresponding to that pin.
24+
25+
For this purpose, we added a compatibility mode so that you can choose which numbering scheme you want to use:
26+
27+
* when porting an existing Arduino project from another Nano board, it's convenient to use the "Arduino pin" scheme which reflects the numbers printed on the board
28+
* when using third-party libraries for ESP32, you should use the "GPIO number" scheme which corresponds to the internal numbers
29+
30+
To change this configuration, simply connect your board, go to **Tools > Pin Numbering** and then select your option. More details are available in this tutorial.
31+
32+
## Software & Hardware Needed
33+
34+
- [Arduino Nano ESP32](https://store.arduino.cc/nano-esp32)
35+
- [Arduino IDE](https://www.arduino.cc/en/software)
36+
- [Arduino ESP32 Core](https://github.com/arduino/arduino-esp32) (version 2.0.12 and above). Can be installed directly in the Arduino IDE.
37+
38+
## Different Pin Configurations
39+
40+
So why does the Nano ESP32 pins not match the ESP32 (MCU) pins? The Nano ESP32 was designed using the **Nano form factor**, a favorable form factor for many, which has consistently kept its pins for many years.
41+
42+
This makes it possible to migrate an older Nano board, to a newer generation Nano (like the Nano ESP32), without having to change your hardware setup.
43+
44+
This of course brings a separate issue, which is that this numbering does not match the ESP32's native GPIO assignment. An example of this is:
45+
- Pin `2` is actually GPIO `5`
46+
- Pin `5` is actually GPIO `8`
47+
- and so on (see the full [pin map](#nano-esp32-pin-map)).
48+
49+
It is common in a board's design that the actual microcontroller's pins don't match the header pins.
50+
51+
### Default & Legacy Options
52+
53+
Luckily, we have two configurations available to choose between:
54+
- `By Arduino pin (default)`
55+
- `By GPIO number (legacy)`.
56+
57+
So, let's say we are configuring a pin in a sketch, using the `Arduino pin (default)` option:
58+
59+
```arduino
60+
// with default configuration, this enables pin 2 as an output
61+
pinMode(2, OUTPUT);
62+
```
63+
64+
But, when using the `GPIO number (legacy)` option, we would need to configure it like this:
65+
66+
```arduino
67+
// with ESP32 configuration, this enables pin 2 as an output
68+
pinMode(5, OUTPUT);
69+
```
70+
71+
### Pin Labels
72+
73+
You can also control pins using labels such as `D0`, `D1`, `D2`. These labels are predefined in the core, and are adjusted based on what configuration you make.
74+
75+
For example, the following code will access the same pin, regardless of what configuration you use:
76+
77+
```arduino
78+
/*
79+
This will configure the "D2" physical pin on your board
80+
(but will internally configure GPIO 5 on the ESP32)
81+
*/
82+
pinMode(D2, OUTPUT);
83+
```
84+
85+
## Change Pin Configuration
86+
87+
To change the pin configuration, open the Arduino IDE, and navigate to **Tools > Pin Numbering.** Here you can select between the default (Nano) and legacy (ESP32) options.
88+
89+
![Change pin configuration in the Arduino IDE.](assets/change-config.png)
90+
91+
You can now upload a sketch, and the configuration will change.
92+
93+
## Nano ESP32 Pin Map
94+
95+
To understand how the Nano ESP32 board's pins correlates with the ESP32-S3 SoC pins, have a look at the pin map below:
96+
97+
| Nano | ESP32 |
98+
| ----- | ------ |
99+
| D0 | GPIO44 |
100+
| D1 | GPIO43 |
101+
| D2 | GPIO5 |
102+
| D3 | GPIO6 |
103+
| D4 | GPIO7 |
104+
| D5 | GPIO8 |
105+
| D6 | GPIO9 |
106+
| D7 | GPIO10 |
107+
| D8 | GPIO17 |
108+
| D9 | GPIO18 |
109+
| D10 | GPIO21 |
110+
| D11 | GPIO38 |
111+
| D12 | GPIO47 |
112+
| D13 | GPIO48 |
113+
| A0 | GPIO1 |
114+
| A1 | GPIO2 |
115+
| A2 | GPIO3 |
116+
| A3 | GPIO4 |
117+
| A4 | GPIO11 |
118+
| A5 | GPIO12 |
119+
| A6 | GPIO13 |
120+
| A7 | GPIO14 |
121+
| BOOT0 | GPIO46 |
122+
| BOOT1 | GPIO0 |
123+
124+
See the pinout below for a better visual translation:
125+
126+
![Nano / ESP32 pinout](assets/esp-pinout.png)
127+
128+
## Summary
129+
130+
In this tutorial, we've covered how the Nano ESP32's pinout differ from the ESP32-S3 SoC pinout. We've also had a look at how to change the configuration, and provided a pin map that can be used as a reference when making your next project.

0 commit comments

Comments
 (0)