Blink an LED with NodeMCU ESP8266

Overview

In this project, we will learn how to blink an LED using the NodeMCU ESP8266 microcontroller. This simple project is a great way to get started with the NodeMCU and familiarize yourself with the Arduino IDE setup for this board.

What We Will Learn in This Section

  1. Setting up the Arduino IDE for NodeMCU ESP8266.
  2. Writing and uploading code to blink an LED.
  3. Understanding the basic GPIO functions of the NodeMCU.

Why Is This Lesson Important to You?

This lesson provides a foundational understanding of how to work with the NodeMCU ESP8266 and the Arduino IDE. Blinking an LED is a basic yet crucial step in learning microcontroller programming and understanding GPIO operations.

Components List

  1. NodeMCU ESP8266
  2. LED
  3. 200 ohm resistor
  4. Micro USB cable
  5. Breadboard
  6. Jumper Wires

Circuit Diagram (With Connection)

NodeMcu8266 to LED connection-mechatronicslabrpi


  1. Connect the anode (longer leg) of the LED to GPIO 3 (D3) of the NodeMCU.
  2. Connect the cathode (shorter leg) of the LED to one end of the 200 ohm resistor.
  3. Connect the other end of the resistor to the GND pin of the NodeMCU.

Code


/* Sarul Hassan Complete project details at http://mechatronicslabrpi.blogspot.com/ */ int pin = 3; // GPIO 3 void setup() { // initialize GPIO 3 as an output. pinMode(pin, OUTPUT); } void loop() { digitalWrite(pin, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(pin, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }

Code Explanation (Explanation Code Line by Line)

  1. int pin = 3; - This line declares a variable pin and assigns it the value 3, which corresponds to GPIO 3.
  2. void setup() { - This function is called once when the program starts. It initializes the settings.
  3. pinMode(pin, OUTPUT); - This line sets GPIO 3 as an output pin.
  4. void loop() { - This function runs repeatedly in a loop.
  5. digitalWrite(pin, HIGH); - This line sets GPIO 3 to a high voltage level, turning the LED on.
  6. delay(1000); - This line creates a 1-second delay.
  7. digitalWrite(pin, LOW); - This line sets GPIO 3 to a low voltage level, turning the LED off.
  8. delay(1000); - This line creates another 1-second delay.

Final Notes

Nodemcu board selaction-mechatronicslabrpi


  • Ensure you have the correct board and port selected in the Arduino IDE:
    • Go to Tools > Board > NodeMCU 0.9 (ESP-12 Module).
    • Go to Tools > Port and select the appropriate port for your NodeMCU.
  • Once your setup is complete, you can upload the code to your NodeMCU and see the LED blink on and off at 1-second intervals.


2 comments:

Theme images by Dizzo. Powered by Blogger.