3 NODEMCU 1st Lecture
3 NODEMCU 1st Lecture
Introduction
# NodeMCU is Lua based firmware of ESP8266. Generally, ESPlorer IDE is
referred for writing Lua scripts for NodeMCU. It requires to get familiar with
ESPlorer IDE and Lua scripting language.
12
What is a Breadboard?
A breadboard, sometimes called a proto-
board, is a reusable platform for temporarily
built electronic circuits.
13
How A Breadboard Works
• Electric component Top View
leads and the wire used
to connect them are
inserted into holes that
are arranged in a grid
pattern on the surface of
the breadboard. Cut-Away View
• A series of internal metal
strips serve as jumper
wires. They connect
specific rows of holes.
14
Breadboard Connections
15
16
17
18
19
20
21
What is the primary purpose of a breadboard in electronics prototyping?
A) To provide a stable base for the electronic components.
B) To supply power to the electronic circuit.
C) To interconnect and test electronic components without soldering.
D) To increase the circuit's resistance.
22
Why Breadboard?
1) It takes less time (and money) to
breadboard a circuit than to design and
fabricate a printed circuit board (PCB).
Because of the cost, a PCB should be
reserved for the final working design.
2) As a complement to circuit simulation,
breadboarding allows the designer to see
how, and if, the actual circuit functions.
23
Why Breadboard?
3) Breadboards give the designer the ability
to quickly change components during
development and testing, such as
swapping resistors or capacitors of
different values.
4) A breadboard allows the designer to
easily modify a circuit to facilitate
measurements of voltage, current, or
resistance.
24
Breadboard: Guidelines and Tips
• Use as few jumper wires as possible. The
breadboard should be used to make the
majority of the connections between the
components.
• Keep jumper wires as short as possible. A
jumble of wires is difficult to troubleshoot.
• Breadboard a circuit so that it looks as close as
possible to the layout of the schematic circuit.
This makes troubleshooting easier.
25
Breadboard: Guidelines and Tips
• Place IC chips in the middle of the breadboard.
• Work from a schematic and check off the
component and wires as they are implemented
on the breadboard.
• Cut component leads to manageable lengths.
Component leads that are too long may touch
and short each other out.
• Have someone check your circuit for errors.
26
• Now Your Arduino IDE is ready for NodeMCU
• Example
• Let’s see how to write simple serial print
sketch using Arduino IDE for NodeMCU.
• First connect NodeMCU Development Kit with
PC as shown in below figure.
Node-MCU pin details
• The NodeMCU ESP8266 development board comes with the
ESP-12E module containing ESP8266 chip having Tensilica
Xtensa 32-bit LX106 RISC microcontroller.
• This microcontroller supports RTOS and operates at 80MHz to
160 MHz adjustable clock frequency.
• NodeMCU has 128 KB RAM and 4MB of Flash memory to
store data and programs.
• Its high processing power with in-built Wi-Fi / Bluetooth and
Deep Sleep Operating features make it ideal for IoT projects.
• NodeMCU can be powered using Micro USB jack and VIN pin
(External Supply Pin). It supports UART, SPI, and I2C interface.
NodeMCU ESP8266 Specifications & Features
• Microcontroller: Tensilica 32-bit RISC CPU Xtensa LX106
• Operating Voltage: 3.3V
• Input Voltage: 7-12V
• Digital I/O Pins (DIO): 16
• Analog Input Pins (ADC): 1
• UARTs: 1
• SPIs (Serial Peripheral Interface): 1
• I2Cs (inter-integrated controller): 1
• Flash Memory: 4 MB
• SRAM: 64 KB
• Clock Speed: 80 MHz
• USB-TTL based on CP2102 is included onboard, Enabling Plug n Play
• PCB Antenna
• Small Sized module to fit smartly inside your IoT projects
NodeMCU Development Board Pinout Configuration
Control Pins EN, RST The pin and the button resets the
microcontroller
Analog Pin A0 Used to measure analog voltage in
the range of 0-3.3V
GPIO Pins GPIO1 to GPIO16 NodeMCU has 16 general purpose
input-output pins on its board
SPI Pins SD1, CMD, SD0, CLK NodeMCU has four pins available for
SPI communication.
UART Pins TXD0, RXD0, TXD2, RXD2 NodeMCU has two UART
interfaces, UART0 (RXD0 &
TXD0) and UART1 (RXD1 &
TXD1). UART1 is used to upload
the firmware/program.
I2C Pins NodeMCU has I2C functionality
support but due to the internal
functionality of these pins, you
have to find which pin is I2C.
PIN Mapping
On-board Switches & LED Indicator
Serial Communication
Serial Communication
• You can use the Arduino IDE environment’s
built-in serial monitor to communicate with an
ESP board.
• Click the Tools>>Serial monitor button in the
toolbar and select the same baud rate used in
the call to begin().
commonly used serial commands
• Serial.begin(9600);
• Serial.print(); and Serial.println(): Serial.print
prints text on same line. Seial.println puts
carriage return at the end of line.
• Serial.write();
• Serial.available();
• Serial.read();
• ESP8266 Wi-Fi Module:
• The NodeMCU board is centered around the
ESP8266 Wi-Fi module, which provides Wi-Fi
connectivity to your projects.
• You can connect to Wi-Fi networks, create a
web server, and communicate with cloud
services.
• GPIO Pins:
• The NodeMCU board has a few analog pins that can be used to
read analog sensor values, such as light intensity or temperature.
• I2C (Inter-Integrated Circuit):
• The NodeMCU has support for I2C communication through its GPIO pins,
allowing you to connect I2C devices like sensors and displays.
• SPI (Serial Peripheral Interface):
• Some of the GPIO pins on the NodeMCU can be configured for PWM output, which is useful for
controlling things like motor speed or LED brightness.
• USB-to-Serial Converter:
• The NodeMCU board typically includes a USB-to-Serial converter chip, making it easy to
connect the board to your computer for programming and debugging.
• Reset and Flash Buttons:
• The board often includes buttons for resetting the board and putting it into flash mode for
uploading firmware.
• Voltage Regulator:
• The NodeMCU board usually includes a voltage regulator that allows you to power the board
using a wider range of voltage inputs.
• Integrated LEDs:
• The NodeMCU board often includes built-in LEDs that can be controlled for various purposes.
Supported Peripheral
• D3 (GPIO0): General-purpose digital I/O pin. Also used for determining boot mode
(connected to ground for normal boot, disconnected for firmware upload).
• D5 (GPIO14): General-purpose digital I/O pin. Also used for SPI clock when the SPI
interface is enabled.
• D6 (GPIO12): General-purpose digital I/O pin. Also used for SPI MISO (Master In Slave Out)
when the SPI interface is enabled.
• D7 (GPIO13): General-purpose digital I/O pin. Also used for SPI MOSI (Master Out Slave In)
when the SPI interface is enabled.
• D8 (GPIO15): General-purpose digital I/O pin. Also used for SPI CS (Chip Select) when the SPI
interface is enabled.
• RX (GPIO3): Serial input pin (UART receive). Used for communication with other devices using
UART.
• TX (GPIO1): Serial output pin (UART transmit). Used for communication with other devices
using UART.
• A0 (ADC0): Analog input pin. Used for reading analog sensor values
• After setting up Arduino IDE for NodeMCU, open Arduino IDE and write
simple sketch of serial print as shown in below figure.
• Ensure that you have selected the correct board as shown in below
figure. Also make sure that you have selected the appropriate COM port.
• Now compile & upload the written sketch directly to the NodeMCU Dev
Kit by clicking on upload button.
Practice programs
• P1: Print Hello Word and Your name on Serial
monitor?
• P2: Print decimal numbers in BIN, OCTAL,
DECIMAL and HEX-DECIMAL number system?
• #define LED D1 // Led in NodeMCU at pin GPIO16 (D0).
• void setup() {
• pinMode(LED, OUTPUT); // set the digital pin as output.
• }
• void loop() {
• digitalWrite(LED, HIGH);// turn the LED off.(Note that LOW is the voltage
level but actually
• //the LED is on; this is because it is acive low on the ESP8266.
• delay(1000); // wait for 1 second.
• digitalWrite(LED, LOW); // turn the LED on.
• delay(1000); // wait for 1 second.
• }
Ans:
• Serial.print(78, BIN) gives “1001110”
• Serial.print(78, OCT) gives “116”
• Serial.print(78, DEC) gives “78”
• Serial.print(78, HEX) gives “4E”
• Serial.println(1.23456, 0) gives “1”
• Serial.println(1.23456, 2) gives “1.23”
• Serial.println(1.23456, 4) gives “1.2346”
Practice Program
• 1. LED blinking without/with Switch
• 2. LED blinking rate with Potentiometer
GPIO Input Example:
• int ledpin = 5; // D1(gpio5)
int button = 4; //D2(gpio4)
• int buttonState=0;
• void setup() {
• pinMode(ledpin, OUTPUT);
• pinMode(button, INPUT);
• }
• void loop() {
• buttonState=digitalRead(button); // put your main code here, to run repeatedly:
• if (buttonState == 1)
• {
• digitalWrite (ledpin, HIGH);
• delay(200);
• }
• if (buttonState==0)
• {
• digitalWrite(ledpin, LOW);
• delay(200);
• }
• }
Video link
• https://www.youtube.com/watch?v=9VEpZlkN
0r0
• https://www.youtube.com/watch?v=HBSH_x6
J1lY
GPIO Output Example:
• void setup() {
• pinMode(LED_BUILTIN, OUTPUT);
• }
• void loop() {
• digitalWrite(LED_BUILTIN, LOW);
• delay(1000); // Wait for a second
• digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making
the voltage HIGH
• delay(2000); // Wait for two seconds (to
demonstrate the active low LED)
• }
Code for potentiometer led blink
Experiment
• Programming NodeMCU for DHT11
DHT11
• Humidity is the measure of water vapour present in the air. The level of
humidity in air affects various physical, chemical and biological processes.
• In industrial applications, humidity can affect the business cost of the
products, health and safety of the employees. So, in semiconductor
industries and control system industries measurement of humidity is very
important.
• Humidity measurement determines the amount of moisture present in the
gas that can be a mixture of water vapour, nitrogen, argon or pure gas etc.
• DHT11 is a digital temperature and humidity sensor.
• DHT11 is a low-cost digital sensor for sensing temperature and humidity. This
sensor can be easily interfaced with any micro-controller such as Arduino,
Raspberry Pi etc… to measure humidity and temperature instantaneously
• Since it is known that sound travels through air at about 344 m/s
(1129 ft/s), you can take the time for the sound wave to return and
multiply it by 344 meters (or 1129 feet) to find the total round-trip
distance of the sound wave.
• Round-trip means that the sound wave traveled 2 times the distance
to the object before it was detected by the sensor; it includes the 'trip'
from the sonar sensor to the object AND the 'trip' from the object to
the Ultrasonic sensor (after the sound wave bounced off the object).
/*
* Ultrasonic Sensor HC-SR04 Tutorial
*/
// defines pins numbers
const int trigPin = 4;
const int echoPin = 5;
// defines variables
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
Arduino Sketch (Cont..)
void loop()
{ // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microsec.
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
delay(500);
}
Proteus design