Let's make the LED blink!

In this chapter we 'll try the Blink program, programming the NodeMCU to flash a LED with a delay that we 'll have already defined.




We connect our NodeMCU and our LED as above on the breadboard, taking care of connecting the shorter of the two legs of the LED to GND of the NodeMCU (through a 220 Ohm resistor) and the longer leg to the pin D7.

The resistor is an electronic component which is used to limit the current flow through a circuit. In our case we use it in order to protect the LED from burning or stress.

Pin D7 (Digital pin 7) corresponds to Pin 13 on Arduino IDE. So we copy the following code into the Arduino IDE code editor:

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

and we hit the Upload button. Then we enjoy our first Blinking program.

Let's talk about the details of our program.

There are two sections: void setup() and void loop().
Anything that belongs to void setup() section (inside curly brackets {} ) is running once, when the program begins.

pinMode(13, OUTPUT);  prepares the pin 13 (D7-Digital 7) of the NodeMCU to accept output commands like "turn off" or "turn on", 0 or 1, in binary logic.

Anything that belongs to void loop() section (inside curly brackets {} ) is running again and again, until we plug the NodeMCU off.

digitalWrite(13, HIGH); commands the pin 13 (D7-Digital 7) to turn on and digitalWrite(13, LOW); commands the pin 13 (D7-Digital 7) to turn off

Between the above two commands there are two delay(1000); that each tells the program to wait for 1000 ms (1 sec) before executing the following command.

Now, try to change the program in order to send an SOS message via the NodeMCU and the LED.

Don't be afraid of the failure. Success is coming after our last failure if we keep trying!