Nano ESP32 blink
The Nano ESP32’s orange LED on D13 and its RGB LED, whose red, green and blue light in turn when their pins go LOW. The whole build, an Arduino Nano ESP32, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 5 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $26. On Mokxi they're free.
What you'll learn
- Built-in LEDs and their pin names
- Common-anode LEDs where LOW means on
- Looping over a list of pins
- Pin numbering by name or by GPIO number
- Difficulty: Beginner
- Time: About 5 minutes
- Board: Arduino Nano ESP32
- Parts: 2
How it works
The Arduino Nano ESP32 has an orange LED on D13 and an RGB LED. Nothing needs wiring. The orange LED lights when its pin is HIGH. The RGB LED is common anode, so each color lights when its pin is LOW.
The sketch turns all three colors off, then steps through red, green and blue. For each, it lights the orange LED, or the color, for 400 ms, then switches them the other way for 400 ms.
Parts list
2 parts, plus the jumper wires. They retail for about $26; on Mokxi they're free.
- 1 × Arduino Nano ESP32
- 1 × Full-size breadboard
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Blink: the orange LED on D13, then the RGB LED's three colors in turn.
//
// LED_BUILTIN is the orange LED on D13, which lights when the pin is HIGH.
// The RGB LED is common anode: each color lights when its pin is LOW, so
// HIGH turns it off.
//
// Mokxi builds this board with Tools, Pin Numbering, "By GPIO number
// (legacy)": D13 is GPIO 48. Write D13, not 13, and the sketch means the
// same pin under either setting.
const int COLORS[] = {LED_RED, LED_GREEN, LED_BLUE};
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
for (int pin : COLORS) {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH); // off
}
}
void loop() {
for (int pin : COLORS) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(pin, LOW); // this color on
delay(400);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(pin, HIGH); // and off
delay(400);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes LED_BUILTIN an output. Then it loops over COLORS, which holds LED_RED, LED_GREEN and LED_BLUE. Each is made an output and set HIGH, which turns that color off.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
for (int pin : COLORS) {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH); // off
}
}loop sketch.ino
For each color in COLORS, it sets LED_BUILTIN HIGH and the color LOW, so the orange LED and the color light together. After 400 ms it flips both and waits 400 ms. Then it moves to the next color.
void loop() {
for (int pin : COLORS) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(pin, LOW); // this color on
delay(400);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(pin, HIGH); // and off
delay(400);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The RGB LED is on when you expect it off. This project
It is common anode. HIGH is off and LOW is on, the reverse of the orange LED. - The pin numbers do not match what you expect. This project
Use the D names such as D13, not plain numbers. The comment explains D13 is GPIO 48. - A 5 V signal damages a pin. Arduino Nano ESP32
The ESP32-S3 uses 3.3 V logic and is not 5 V tolerant. Use a level shifter or divider for 5 V parts. Check whether you are using the D pin name or the GPIO number. - The RGB LED shows the wrong color, or the upload fails. Arduino Nano ESP32
The RGB LED lights when its pin goes LOW. If the upload fails, double-tap the reset button to enter the bootloader, then pick the Nano ESP32 board and port.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.