ESP8266 - Code Structure

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

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.
Additionally, some of these links are for products from our own brand, DIYables.

Basic Structure

The ESP8266 code consists of two sections: the setup code and the loop code. The setup code is executed once, when the program starts. The loop code is executed continuously until the program is stopped.

Setup Code

  • Code in the setup() function is executed right after power-up or reset. It is only executed one time and is used for initializing variables, pin modes, and starting to use libraries, etc.

Loop Code

  • Code in the loop() function is executed right after the setup code. It is executed repeatedly, infinitely, and is used to do the main task of the 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); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to transfer the code to the ESP8266.
  • Open the Serial Monitor.
  • Check out the output on the 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  

You can observe that “This is the setup code” is printed only once, however “This is loop code” is printed multiple times. This implies that the setup code is run once, and the loop code is executed repeatedly. The setup code is executed first.

※ NOTE THAT:

The setup() and loop() functions MUST be included in ESP8266 code. Failure to do so will result in an error.

Optional Parts

: used to declare variables that can be used in all the sketch

Apart from setup and loop code, an ESP8266 sketch may comprise of the following sections:

  • Block comment: generally used to include details about the author, wiring instructions, license etc. which are ignored by ESP8266.
  • Libraries inclusion: used to include libraries in the sketch.
  • Constant definition: used to define constants.
  • Global variables declaration: used to declare variables which can be accessed throughout the sketch.
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-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); }

Detailed Instructions

  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to send the code to the ESP8266.
  • Open the Serial Monitor.
  • Check the output on the 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 require comprehension of code line-by-line at present. We just need to be familiar with the code structure. The line-by-line code will be elucidated in subsequent 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!