Controlling Node MCU's in-built LED - The first Node MCU code.!

We are aware of Node MCU and Arduino IDE if you have followed us from Blog 1. Today, we will use Node MCU and learn how to code it using the Arduino IDE.


To upload the code in Node MCU we need a USB cable (the same cable with which we charge our mobile phone), Insert one end to the PC, and other to the Node MCU.


To check whether your USB cable is working properly, follow these steps:

  • Right-click on "This PC" or "My computer."
  • Select Manage.
  • Go to the Device Manager.
  • Remove USB from your PC.
  • Insert it again. If the screen refreshes, then your USB cable is working fine, else you need to change your USB cable.

Now, scroll down to ports and see the port name here. It will be something like COM3 or COM5 or any other number. Remember this port number as we will need this further. For better understanding, watch this video.



Now, we will learn how to operate the in-built LED of Node MCU. There are two LEDs, one near the D0 pin which is connected to pin D4 and other at the bottom of the Node MCU which is connected to pin D0.


We will code the led located near the D0 pin. It is connected with a not gate to the D4 pin. This means if the pin is set to low the led will glow and if the pin is set to High the led will not glow


Let's understand the code.

#define led D4 //controlling led connected to pin D4

void setup() {

 pinMode(led,OUTPUT); // declaring pinmode

} void loop() {

 digitalWrite(led,0); //turns led on

 delay(1000); // 1000 milliseconds or 1 second delay

 digitalWrite(led,1); //turns led off

 delay(1000); // 1 second delay

}


Code link - https://github.com/TechStormers/built-in-node-mcu-led



The first line is a macro where 'led' is the variable and D4 is the value. Macro is very useful when the code is lengthy.


Suppose, there are 50 appearances of the pin D4 in your code, and suddenly you need to change the pin from D4 to D0. So just by changing the value of macro from D4 to D0 ur work is done. It saves you time and effort.


Now, in the void setup, we have set the pinMode. Pinmode is used to declare the pin and whether the pin will give an output signal to another hardware or receive input signals from other hardware.

Here we have set the pinMode to Output because we will send an output signal from the D4 pin to the led and control it.

In the case of receiving signals from a sensor, we set pinMode to input.

We have written this code in void setup because one pin will either sent an output signal or receive an input signal at a time. The same pin cannot do both the tasks together. So we need to declare pinMode only once and hence we have written it in void setup.


Please read the blog where I have explained Arduino IDE for a better understanding.


Next, we come to the void loop where we write the code which controls Node MCU's action.

Digital write is the command to send digital signals from Node MCU to other devices.

Delay gives us a small amount of time between the execution of two consecutive lines of code. This means the led will be turned on first then after one second, it will be turned off.

There is a command called digital read which is used to read digital signals from sensors and other devices. We will learn about it in the future.


So let's upload our first code on Node MCU and see what happens.

To upload the code we need to set the port. Go to tools->select port and set the port number. I have explained previously how to see your port number. Now click on upload.


Its getting uploaded and see the led blinks.


Congrats your first step towards making innovative projects is taken successfully.

Try making changes to code such as changing pin from D4 to D0 or increasing the delay etc to get familiar to it.


We will be posting amazing projects here. Keep following us - 


YouTube - youtube.com/techStormers
Facebook - facebook.com/techstormers
Instagram - instagram.com/techstormers


Comments

Popular posts from this blog

Connecting LED with Node MCU

Introduction to Node MCU

The Rise of Functional Programming