Jutronix Embedded

esp-12e-NodeMCU-Pinout

Introducing the ESP8266

In this ESP8266 tutorial, we will be using Arduino IDE. ESP8266 Wi-Fi is a microcontroller chip from Espressif with on-board Wi-Fi. It can be used as a standalone device, or as a UART to Wi-Fi adaptor to allow other microcontrollers to connect to a Wi-Fi network. For example, you can connect an ESP8266 to an Arduino to add Wi-Fi capabilities to your Arduino board. With the ESP8266, you can control inputs and outputs as you would do with an Arduino, but with Wi-Fi capabilities. This means that you can bring your projects online, which is great for home automation and internet of things applications.

The ESP8266 Boards

ESP-01

ESP-01

ESP-02

esp-03

ESP-03

esp-04

ESP-04

esp-05

ESP-05

ESP-06

ESP-06

ESP-07

ESP-08

ESP-08

ESP-09

ESP-09

ESP-10

ESP-10

ESP-11

ESP-12E NodeMCU

ESP-12E NodeMCU

WEMOS D1 MINI

WeMos D1 Mini

Installing the Arduino IDE

In this ESP8266 tutorials, we will download, install and prepare the Arduino IDE to work with the ESP8266. 

To download the Arduino IDE, visit the following URL:   https://www.arduino.cc/en/software 

Select your operating system, download the software and install it.

Installing the ESP8266 add-on

To install the ESP8266 add-on in your Arduino IDE, follow these next instructions: 

1) Open the preferences window in the Arduino IDE. Go to File > Preferences.

2) Enter “http://arduino.esp8266.com/stable/package_esp8266com_index.json” (without the double quotation mark)

into Additional Board Manager URLs field and press the “OK” button.

Arduino ESP8266 Addon

3) Go to Tools > Board > Boards Manager…

Arduino Board manager

4) Search for ESP8266 and press the INSTALL button for the “ESP8266 by ESP8266 Community“:

ESP8266 Board manager

5) After the installation, you can close the Boards Manager window.        

6) Open the Arduino Tools menu.  

7) Select Board > NodeMCU 1.0 (ESP-12E Module) 

Creating your first project for ESP8266 tutorial

Project 1: Blink LED

To build this first circuit for this esp8266 tutorial, we will use: Lolin NodeMCU V3 (or any ESP8266 board).

This first project is just to flash the built-in LED on the NodeMCU board.

The LED is connected to pin D4 which is GPIO2.

Lolin ESP8266 NodeMCU

Lolin NodeMCU front view

Lolin NodeMCU back view

The Arduino Sketch

/************************************************************************
 * 
 * by Jutronix Embedded
 * Complete project detail at http://www.jutronix.com/esp8266-tutorial/
 * 
 ***********************************************************************/

#define LEDpin D4   //set led pin

// the setup function runs once when you press reset or power the board
void setup() {
  
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LEDpin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LEDpin, HIGH);   // turn the LED on
  delay(1000);                  // wait for a second
  digitalWrite(LEDpin, LOW);    // turn the LED off 
  delay(1000);                  // wait for a second
}

Uploading Code to ESP8266

Uploading code to your ESP-12E NodeMCU Kit or any other ESP8266 board with built-in programmer is very simple. You just need to plug your board to your computer using an USB cable.

After connecting the EPS8266 board to a USB port in your computer, in your Arduino IDE, go to the Tools menu and select the “NodeMCU 1.0 (ESP-12E Module)” board.
Go to Tools > Port and select the COM Port the ESP8266 is connected to.
Click the “Upload Button” in the Arduino IDE and wait a few seconds until you see the message “Done uploading.” in the bottom left corner.

Project 2: Use button switch to control LED

Schematic

Here’s a list of the parts needed to build the circuit:

Assemble the circuit with a pushbutton and an LED as shown in the following schematic diagram:

  • LED: D5 (GPIO 14)
  • Pushbutton: D6 (GPIO 12)
switch led

Code

With the circuit ready, copy the code below to your Arduino IDE.

/**********************************************************************
 * by Jutronix Embedded
 * Complete project detail at http://www.jutronix.com/esp8266-tutorial/
 ***********************************************************************/

const int ledPin =  D5;       // the pin number of the LED pin
const int buttonPin = D6;     // the pin number of the pushbutton pin

// variable for storing the pushbutton status 
int buttonState = 0;

void setup() {
  Serial.begin(115200);  
  // initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off
    digitalWrite(ledPin, LOW);
  }
} 

How the code works

The following two lines, create variables to assign pins:

const int ledPin =  D5;       // the pin number of the LED pin
const int buttonPin = D6;     // the pin number of the pushbutton pin

The LED is connected to pin D5 (GPIO 14) and the button switch is connected to pin D5 (GPIO 12).  Next, create a variable to hold the button state.

int buttonState = 0;  // variable to hold button state

In the setup( ), initialize the button as an INPUT, and the LED as an OUTPUT. For that, use the  pinMode( ) function that accepts the pin you are referring to, and the state: either INPUT or OUTPUT.

pinMode(ledPin, OUTPUT); 
pinMode(buttonPin, INPUT);

In the loop( ) is where you read the button state and set the LED accordingly. The next line reads the button state and saves it in the buttonState variable. As we’ve seen previously, you use the digitalRead( ) function.

buttonState = digitalRead(buttonPin); 

The following if statement, checks whether the button state is HIGH. If it is, it turns the LED on using the digitalWrite( ) function that accepts as arguments the ledPin, and the state HIGH.

if (buttonState == HIGH) {
  // turn LED on
  digitalWrite(ledPin, HIGH);
}  

If the button state is not HIGH, you set the LED off, by writing LOW in the digitalWrite( ) function.

else {
  // turn LED off
  digitalWrite(ledPin, LOW);
} 
%d bloggers like this: