ESP32 - Code Structure

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

Basic Structure

ESP32 code (also called ESP32 sketch) includes two main parts: setup code and loop code.

Setup Code

  • Is code in setup() function.
  • Executed right after power-up or reset
  • Executed only one time.
  • Used to initialize variables, pin modes, start using libraries,

Loop Code

  • Is code in loop() function.
  • Executed right after setup code.
  • Executed repeatedly (infinitely).
  • 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 setup code"); } void loop() { // put your main code here, to run repeatedly: Serial.println("This is loop code"); delay(1000); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Open Serial Monitor on Arduino IDE
  • How to open serial monitor on Arduino IDE
  • See the output on Serial Monitor
  • COM6
    Send
    This is setup code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

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

※ NOTE THAT:

setup() and loop() functions MUST be in ESP32 code. If not, an error is generated.

Optional Parts

Apart from setup and loop code, an 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 ... ESP32 will ignore this part.
  • Libraries inclusion: is used to include libraries into the sketch.
  • Constant definition: used to define constant
  • Global variables declaration
/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/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 setup code"); } void loop() { loop_count++; Serial.print("This is 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); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Open Serial Monitor on Arduino IDE
  • How to open serial monitor on Arduino IDE
  • See the output on Serial Monitor
  • COM6
    Send
    This is setup code This is loop code, count: 1 This is loop code, count: 2 This is loop code, count: 3 This is loop code, count: 4 This is loop code, count: 5 This is loop code, count: 6 This is 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.

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere