This algorithm is current as of April 24, 2024, operating system Mac OS.
Programming the ESP8266 module to control the on-board LED is quite simple if you use the following installation and configuration steps correctly.
The whole process may take no more than 30 minutes, depending on the Internet speed, computer settings and time that you spent connecting the module to your computer. We will use the Wi-Fi module NodeMCU V3 ESP8266 ESP-12 (CH340):
To get started, you need to download and install the Arduino IDE application, designed for creating and loading programs onto Arduino-compatible boards. To do this, download the Arduino IDE from the official website arduino.cc :
After installing the Arduino IDE, you need to download the required libraries for the ESP8266 microcontroller:
The libraries have been downloaded, now we can start flashing the board firmware, but first we need to establish a connection between the ESP8266 module and the computer port.
In this case, micro-USB to USB Type-C adapters were used, since the module has a connection socket with a micro-USB connector, and the computer only has USB Type-C. Communication kit:
Connection complete:
To communicate with a computer, you need to define a port (USB connector), which can be found empirically by plugging and unplugging the module from the computer's USB port, in my case 1410. Then you need to connect the board, in this case I used LOLIN(WEMOS) D1 ESP-WROOM-02
The board is connected, you can start creating the first sketch for flashing the module with the control code (blinking) of the built-in LED on the board.
To do this, open the example file file -> examples -> 01.Basics -> Blink
in the Arduino IDE menu.
This sketch looks like this, on the basis of which you can control the on and off time by changing the delay
parameter:
The code is ready, the ESP8266 is connected, you can click upload to flash the microcontroller firmware
We are waiting for the download result:
And looking at the board where the LED is blinking at 1 second intervals:
By changing the delay
, you can reconfigure the period when the diode is turned on, after which you need to repeat the upload, wait until the module is flashed and make sure that the diode is operating correctly.
Video instructions for changing the on/off period of the diode are available below:
Result of the module running with the new firmware:
We will need:
Install the ESP8266 on the breadboard:
Before programming the microcontroller, I suggest connecting an LED to the ESP8266 using a primitive wiring scheme as an example. To do this, we will need a resistor to reduce the current, since our LED is designed for a voltage of max 2.8V, then in order to prevent it from burning out, we will connect a resistor with a nominal value of 220 Ohms to it in series. To initially check the operation of the LED, let's connect it to the ESP8266 pin with label 3v output to the plus of the LED (long leg), then connect a resistor to the short leg of the LED, and second the resistor leg to the pin of the microcontroller with label G, in the end everything looks like this (the connection chain is shown in yellow):
To control an external LED, change the LED plus connection from pin with label 3v to pin with label D0. And let's also update sketch, in which we change the constants in the source code below, where in the first parameter of the function pinMode
we set the address D0
instead of LED_BUILTIN
.
Also edit the loop
function, where in both digitalWrite
functions we change the name of the first parameter LED_BUILTIN
to D0
Initial code:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Converted code:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(D0, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(D0, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(D0, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Click upload
And we wait for the result of loading sketch into the microcontroller. Congratulations, you have assembled and programmed your first device.
If the LED does not blink, then it may have burned out and we can check the functionality of this sketch using a multimeter.
Before we get started with the next block, let's learn how to send and read information in a serial monitor. The serial monitor is a built-in console that is included in the Arduino IDE. In the upper right corner you can see a button with a magnifying glass, it opens the console to us.
To start working with the port, you need to open a new sketch and launch the serial monitor using the setup
and the begin
method, where in brackets we specify the communication speed with Arduino (115200), so that it opens when launched:
void setup() {
Serial.begin(115200);
}
To send information to the port, for example, "Hello, world", let's use the println()
method and print this phrase to the console every 1 second using the following code:
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println("Hello World!");
delay(1000);
}
But that’s not all, after we’ve uploaded the sketch, open the port and select the speed that we specified in the begin
method:
After we have synchronized the speeds, we can see the result of the sketch in the console.
Based on this material, you now understand the minimum basics of using a serial monitor
The main feature of the ESP8266 microcontroller is the ability to connect and transfer data over Wi-Fi network. Let's go through the instructions for connecting the module to a Wi-Fi network. To do this, we will need a ready-made sketch, where we will need to enter the name of the network (ssid) to which we need to connect and the password for it. To work on a Wi-Fi network, you should use the built-in library ESP8266WiFi.h
#include "ESP8266WiFi.h"
// WiFi parameters to be configured
const char* ssid = "xxxx"; // youe name of ssid
const char* password = "xxxx"; // your password
void setup(void)
{
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
}
void loop() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("try to connect WiFi: ");
Serial.println(ssid);
}
Serial.print("WiFi connected to ssid: ");Serial.println(ssid);
// Print the IP address
// Serial.println(WiFi.localIP());
Serial.print("level WiFi signal: ");Serial.print(WiFi.RSSI());Serial.print(" dBm");
if (WiFi.RSSI()*-1 <= 60){
Serial.println(" (excellent signal level)");}
else if (WiFi.RSSI()*-1 > 60 && WiFi.RSSI()*-1 <= 70){
Serial.println(" (good signal level)");
}
else if (WiFi.RSSI()*-1 > 70 && WiFi.RSSI()*-1 <= 80){
Serial.println(" (low signal level)");
}
else if (WiFi.RSSI()*-1 > 80 && WiFi.RSSI()*-1 <= 90){
Serial.println(" (very low signal level)");
}
else if (WiFi.RSSI()*-1 > 90) {
Serial.println(" (critically low signal level)");
}
Serial.println("");
delay(5000);
}
If you correctly enter the network name (ssid) and password, approximately the following information will appear in your console (serial monitor):
The signal level value in serial monitor will inform you about the stability of your connection: