Arduino Nano ESP32 - Code Structure

To learn how to program for ESP32, you need to learn the structure of Arduino Nano ESP32 code. This tutorial provides and explains the structure of Arduino Nano ESP32 code.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Basic Code Structure

Arduino Nano ESP32 code (also called Arduino Nano ESP32 sketch) has the same structure as Arduino Code. It includes two main parts: setup code and loop code.

Setup Code

  • Setup code is code in setup() function.
  • Setup code is executed right after power-up or reset
  • Setup code is executed only one time.
  • Setup code is used to initialize variables, pin modes, start using libraries,

Loop Code

  • Loop code is code in loop() function.
  • Loop code is executed right after setup code.
  • Loop code is executed repeatedly (infinitely).
  • Loop code is used to do the main task of application

Example

void setup() { // put your setup code here, to executed once: Serial.begin(9600); Serial.println("This is Arduino Nano ESP32 setup code"); } void loop() { // put your main code here, to run repeatedly: Serial.println("This is Arduino Nano ESP32 loop code"); delay(1000); }

Detailed Instructions

How to open serial monitor on Arduino IDE
  • See the output on Serial Monitor
COM6
Send
This is Arduino Nano ESP32 setup code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code This is Arduino Nano ESP32 loop code
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

As you can see on Serial Monitor, “This is Arduino Nano ESP32 the setup code” is printed once, but “This is Arduino Nano ESP32 loop code” is printed many times. It means the Arduino Nano ESP32 setup code is executed once, the Arduino Nano ESP32 loop code is executed repeatedly. The setup code is executed first.

※ NOTE THAT:

setup() and loop() functions MUST be used in Arduino Nano ESP32 code. If not, it generates an error.

Other Parts

Apart from setup and loop code, an Arduino Nano ESP32 sketch can include some of the following parts:

  • Block comment: usually used to write some information about the author, the wiring instruction, the license ... Arduino Nano ESP32 will ignore this part.
  • Libraries inclusion: is used to include libraries into the sketch.
  • Constant definition: used to define constant
  • Global variables declaration

For example:

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-code-structure */ #include <Servo.h> #include <LiquidCrystal.h> #define MAX_COUNT 180 Servo servo; LiquidCrystal lcd(3, 4, 5, 6, 7, 8); int loop_count = 0; void setup() { Serial.begin(9600); lcd.begin(16, 2); servo.attach(9); Serial.println("This is Arduino Nano ESP32 setup code"); } void loop() { loop_count++; Serial.print("This is Arduino Nano ESP32 loop code, count: "); Serial.println(loop_count); lcd.print("Hello World!"); servo.write(loop_count); if(loop_count >= MAX_COUNT) loop_count = 0; delay(1000); }

Detailed Instructions

How to open serial monitor on Arduino IDE
  • See the output on Serial Monitor
COM6
Send
This is Arduino Nano ESP32 setup code This is Arduino Nano ESP32 loop code, count: 1 This is Arduino Nano ESP32 loop code, count: 2 This is Arduino Nano ESP32 loop code, count: 3 This is Arduino Nano ESP32 loop code, count: 4 This is Arduino Nano ESP32 loop code, count: 5 This is Arduino Nano ESP32 loop code, count: 6 This is Arduino Nano ESP32 loop code, count: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

We do NOT need to understand code line by line now. We just need to know about code structure. The line-by-line code will be explained in the next tutorials.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!